Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

Angular 2在类中直接声明属性和在构造函数中声明有什么区别?为什么在构造函数中声明会造成如下错误?

直接声明,一切正常:

export class MapService {
  public map;
  constructor() { }
  initMap(mapId: string, mapCenter) {
    this.map = new T.Map(mapId);
    this.map.centerAndZoom(mapCenter, 4);
  }
}

在构造函数中声明,出现错误:

export class MapService {
  constructor(public map) { }
  initMap(mapId: string, mapCenter) {
    this.map = new T.Map(mapId);
    this.map.centerAndZoom(mapCenter, 4);
  }
}

错误提示如下

Uncaught Error: Can't resolve all parameters for MapService: (?).
    at syntaxError (http://localhost:4200/vendor.bundle.js:29377:34) [<root>]
    at CompileMetadataResolver._getDependenciesMetadata (http://localhost:4200/vendor.bundle.js:42225:35) [<root>]
    at CompileMetadataResolver._getTypeMetadata (http://localhost:4200/vendor.bundle.js:42093:26) [<root>]
    at CompileMetadataResolver._getInjectableMetadata (http://localhost:4200/vendor.bundle.js:42079:21) [<root>]
    at CompileMetadataResolver.getProviderMetadata (http://localhost:4200/vendor.bundle.js:42368:40) [<root>]
    at http://localhost:4200/vendor.bundle.js:42296:49 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://localhost:4200/vendor.bundle.js:42259:19) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/vendor.bundle.js:41915:50) [<root>]
    at CompileMetadataResolver.getNgModuleSummary (http://localhost:4200/vendor.bundle.js:41769:52) [<root>]
    at http://localhost:4200/vendor.bundle.js:41842:72 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/vendor.bundle.js:41827:49) [<root>]
    at JitCompiler._loadModules (http://localhost:4200/vendor.bundle.js:52952:64) [<root>]
ZoneAwareError @ zone.js:917
syntaxError @ compiler.es5.js:1503
CompileMetadataResolver._getDependenciesMetadata @ compiler.es5.js:14351
CompileMetadataResolver._getTypeMetadata @ compiler.es5.js:14219
CompileMetadataResolver._getInjectableMetadata @ compiler.es5.js:14205
CompileMetadataResolver.getProviderMetadata @ compiler.es5.js:14494
(anonymous) @ compiler.es5.js:14422
CompileMetadataResolver._getProvidersMetadata @ compiler.es5.js:14385
CompileMetadataResolver.getNgModuleMetadata @ compiler.es5.js:14041
CompileMetadataResolver.getNgModuleSummary @ compiler.es5.js:13895
(anonymous) @ compiler.es5.js:13968
CompileMetadataResolver.getNgModuleMetadata @ compiler.es5.js:13953
JitCompiler._loadModules @ compiler.es5.js:25078
JitCompiler._compileModuleAndComponents @ compiler.es5.js:25037
JitCompiler.compileModuleAsync @ compiler.es5.js:24999
PlatformRef_._bootstrapModuleWithZone @ core.es5.js:4786
PlatformRef_.bootstrapModule @ core.es5.js:4772
104 @ main.ts:11
__webpack_require__ @ bootstrap 9cad57b…:52
255 @ main.bundle.js:354
__webpack_require__ @ bootstrap 9cad57b…:52
webpackJsonpCallback @ bootstrap 9cad57b…:23
(anonymous) @ main.bundle.js:1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我只能告诉你,Angular2的类实体是由DI构建的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...