2016-10-23 83 views
0

請幫助我,我不明白如何在TS中使用es6模塊。 例如:我不明白如何使用es6模塊打字稿2

我試着將ext.class導入到class1.ts。

class1.ts 
import ExtClass from "./ext.class"; 
class Class1 { 
    constructor(){ 
     console.log(new ExtClass().title) 
     console.log("Work") 
    } 
} 
new Class1(); 

其出口類

ext.class.ts 
class ExtClass { 
    public title: string = "ExtClass work"; 
} 
export default ExtClass 

package.json 
{ 
    "name": "ts_def", 
    "version": "0.0.0", 
    "license": "MIT", 
    "private": true, 
    "scripts": { 
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", 
    "lite": "lite-server" 
    }, 
    "dependencies": { 
    "core-js": "^2.4.1", 
    "ts-helpers": "^1.1.1" 
    }, 
    "devDependencies": { 
    "@types/jasmine": "^2.2.30", 
    "@types/node": "^6.0.42", 
    "codelyzer": "~0.0.26", 
    "ts-node": "1.2.1", 
    "tslint": "3.13.0", 
    "typescript": "2.0.2", 
    "concurrently": "^3.0.0", 
    "lite-server": "^2.2.2" 
    } 
} 
tsconfig.json 
{ 
    "compilerOptions": { 
    "declaration": false, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "lib": ["es6", "dom"], 
    "mapRoot": "./", 
    "module": "es6", 
    "moduleResolution": "node", 
    "outDir": "../dist/out-tsc", 
    "sourceMap": true, 
    "target": "es6", 
    "typeRoots": [ 
     "../node_modules/@types" 
    ] 
    } 
} 

在結果我收到的CLI和瀏覽器意外令牌進口。請告訴我,什麼是錯的?

回答

0

現在,您正在輸出來自typescript的es6代碼。現在,除非您打算進一步將其轉換爲ES5代碼,否則這並不有用。

通常,使用的模塊方式是將代碼轉換爲ES5,然後使用TSC將ES6模塊轉換爲ES5模塊定義(例如systemjs模塊),並在運行時使用模塊加載器(如SystemJS)加載它們。