javascript - Angular 2在类中直接声明属性和在构造函数中声明有什么区别?为什么在构造函数中声明会造成如下错误?
ringa_lee
ringa_lee 2017-04-11 13:20:29
[JavaScript讨论组]

直接声明,一切正常:

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) []
    at CompileMetadataResolver._getDependenciesMetadata (http://localhost:4200/vendor.bundle.js:42225:35) []
    at CompileMetadataResolver._getTypeMetadata (http://localhost:4200/vendor.bundle.js:42093:26) []
    at CompileMetadataResolver._getInjectableMetadata (http://localhost:4200/vendor.bundle.js:42079:21) []
    at CompileMetadataResolver.getProviderMetadata (http://localhost:4200/vendor.bundle.js:42368:40) []
    at http://localhost:4200/vendor.bundle.js:42296:49 []
    at Array.forEach (native) []
    at CompileMetadataResolver._getProvidersMetadata (http://localhost:4200/vendor.bundle.js:42259:19) []
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/vendor.bundle.js:41915:50) []
    at CompileMetadataResolver.getNgModuleSummary (http://localhost:4200/vendor.bundle.js:41769:52) []
    at http://localhost:4200/vendor.bundle.js:41842:72 []
    at Array.forEach (native) []
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/vendor.bundle.js:41827:49) []
    at JitCompiler._loadModules (http://localhost:4200/vendor.bundle.js:52952:64) []
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
ringa_lee
ringa_lee

ringa_lee

全部回复(1)
高洛峰

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号