2017-07-16 223 views
2
package.json 

{ "version": "1.0.0", 
    "name": "basic-app", 
    "devDependencies": { "typescript": "^2.4.1" }, 
    "dependencies": { 
    "@angular/core": "2.4.5", 
    "@angular/platform-browser": "2.4.5", 
    "@angular/platform-browser-dynamic": "2.4.5", 
    "@angular/common": "2.4.5", 
    "@angular/compiler": "2.4.5", 
    "@angular/http": "2.4.5", 
    "rxjs": "5.0.1", 
    "zone.js": "^0.7.2", 
    "systemjs": "0.19.40", 
    "core-js": "^2.4.1" 
}} 

tsconfig.json 

{ "compilerOptions": { 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "experimentalDecorators": true, 
    "emitDecoratorMetadata": false, // set to false 
    "lib": ["es2015", "dom"], 
    "noImplicitAny": true, 
    "suppressImplicitAnyIndexErrors": true 
}} 

index.html 

<!DOCTYPE html> 
<html> 
<head> 
<script src="node_modules/core-js/client/shim.min.js"></script> 
<script src="node_modules/zone.js/dist/zone.js"></script> 
<script src="node_modules/systemjs/dist/system.src.js"></script> 
<script src="src/systemjs.config.js"></script> 
<script> 
    System.import('main').catch(function(err) { 
     console.error(err); 
    }); 
</script> 
</head> 
<body> 
    <my-app>Loading</my-app> 
</body> 
</html> 

该代码似乎工作正常,当我在浏览器中运行它;我没有包含反映metadata.js,也没有在tsconfig.json中包含emitDecoratorMetadata属性;所以reflectmetadata.js和emitDecoratorMetadata在angular2中是可选的?请让我知道为什么上面的代码仍然在浏览器中工作。任何帮助是极大的赞赏。角度是否需要reflectmetadata.js和emitDecoratorMetadata:true?

https://embed.plnkr.co/?show=preview&show=app%2Fapp.component.ts角样本代码不包括反映metadata.js

回答

2

emitDecoratorMetadata

需要此设置如果使用类型被设置为作为true令牌是这样的:

constructor(dependency: ClassName) {} 

如果设置为true,编译器会生成以下代码:

AppComponent = __decorate([ 
    core_1.Component({ 
     selector: 'my-app', 
     ... 
    }), 
    __metadata("design:paramtypes", [core_1.Injector]) 
             ^^^ 
         here the type for token is referenced 

如果您只使用@Inject()修饰器,那么您不需要设置emitDecoratorMetadata

反映元

这个包本身通常不要求,因为它纳入core.js由角要求:

<script src="node_modules/core-js/client/shim.min.js"></script> 
+1

伟大的答案。没有意识到它正在使用_decorate()函数。以为它会使用Class.name或其他东西 – Ryan