Typescript 找不到名称';模块';。使用commonjs时
我有以下代码Typescript 找不到名称';模块';。使用commonjs时,typescript,Typescript,我有以下代码 @Component({ module: module.id, selector: 'hero', templateUrl:'hero.component.html', styleUrls: ['hero.component.css'], directives: [HeroDetailComponent, MD_CARD_DIRECTIVES, MD_BUTTON_DIRECTIVES, MD_LIST_DIRECTIVES, MD_ICO
@Component({
module: module.id,
selector: 'hero',
templateUrl:'hero.component.html',
styleUrls: ['hero.component.css'],
directives: [HeroDetailComponent, MD_CARD_DIRECTIVES, MD_BUTTON_DIRECTIVES, MD_LIST_DIRECTIVES, MD_ICON_DIRECTIVES, MdToolbar, MD_INPUT_DIRECTIVES],
providers: [HeroService],
viewProviders: [MdIconRegistry]
})
export class HeroComponent implements OnInit{
...
}
//tsconfig.js
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"rootDir": "src/ng2",
"moduleResolution": "node",
"sourceMap": false,
"inlineSources": true,
"inlineSourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"outDir": "public/ng2",
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
但是当我跑的时候我得到
src/ng2/components/hero/hero.component.ts(15,13):错误TS2304:找不到名称“module”
更新
我试着按照你的想法做这些步骤
但我仍然明白
对象文字只能指定已知属性,并且类型中不存在“模块”
更新2在更新引导文件后,如下所示
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HeroComponent} from './components/hero/hero.component';
import { HTTP_PROVIDERS } from '@angular/http';
declare var module:any;
bootstrap(HeroComponent, [ HTTP_PROVIDERS ]);
我仍然明白
src/ng2/components/hero/hero.component.ts(15,5):错误TS2345:。
对象文字只能指定已知属性,并且类型“{selector?:string;inputs?:string[];outputs?:string[];properties?:string[];events?:strin…”中不存在“module”
以下是更新 好的,让我们将该行放在typescript定义文件中,并在引导文件中添加对该文件的引用
//
然后需要在引用的路径处创建一个文件。在该文件中,添加以下行:declare var module:any;
原始答案
在Angular启动脚本(调用Angular的引导
函数的脚本)中,在引导附近添加以下行:
声明var模块:任意;
我不确定为什么会发生此错误,因为我和你一样,按照建议配置了依赖项。因为我知道这不会造成任何问题,我只是添加了一行代码,以使编译器停止抱怨尝试了这一点,但我似乎仍然有问题。有什么想法吗?请参阅UpdateCheckout我编辑过的答案。也许这行代码必须是一个类型脚本de定义文件。我会试试看。我还能用systemJS作为我的传输工具吗?
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HeroComponent} from './components/hero/hero.component';
import { HTTP_PROVIDERS } from '@angular/http';
declare var module:any;
bootstrap(HeroComponent, [ HTTP_PROVIDERS ]);