我正在Visual Studio 2015中使用gulp进行angular 4项目。我Visual Studio 2015 QuickStart
从这里得到的。
我的项目结构:
src
└──angular
└──common
└──models
└──items.ts
└──ui
└──components
└──items.component.ts
items.ts
export module items {
export const listOfItems = [1,2,3];
}
导入src文件夹中的所有内容时,我想使用非相对路径,即:
items.component.ts
import { items } from 'items';
这是我的tsconfig.json
文件(与处于同一级别src
):
{
"compileOnSave": false,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"node_modules/@types/"
]
}
}
我尝试将其添加到compilerOptions
:
"baseUrl": "." //it doesn't help.
之后:
"baseUrl": ".",
"paths": {
"*": [
"*",
"src/angular/*" // still nothing.
]
}
我也添加了,rootDirs
但仍然出现此错误:
Can't find module 'items'
首先,请确保已安装Visual Studio 2015的TypeScript-v2.5.2,之后:
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@models/*": [ "angular/common/models/*" ]
}
//..
}
}
用法:
import { items } from '@models/items';
所以,这里发生了什么:
*
是指向我们的文件的实际名称,所以在我们的情况下,后@models
有*
其指向items
,并且items
是里面的某个地方angular/common/models/*
。
另外,我们应该添加它,systemjs.config.js
以便可以解析路径:
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/',
"@models/*": ["rootDist/angular/common/models/*"]
^
where you keep your files, for example dist/
}
// other things..
});
另一个重要的事情是重新启动Visual Studio
我也尝试过卸载/重新加载项目,但这无济于事。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句