angular – 如何在Visual Studio 2015中从非相对路径导入模块

前端之家收集整理的这篇文章主要介绍了angular – 如何在Visual Studio 2015中从非相对路径导入模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Visual Studio 2015中使用gulp进行角度4项目.
我从 here获得了Visual Studio 2015 QuickStart.

> tsc版本:2.5.2

我的项目结构:

  1. src
  2. └──angular
  3. └──common
  4. └──models
  5. └──items.ts
  6. └──ui
  7. └──components
  8. └──items.component.ts

items.ts

  1. export module items {
  2. export const listOfItems = [1,2,3];
  3. }

我想在导入我的src文件夹中的所有内容时使用非相对路径,即:

items.component.ts

  1. import { items } from 'items';

这是我的tsconfig.json文件(与src处于同一级别):

  1. {
  2. "compileOnSave": false,"compilerOptions": {
  3. "target": "es5","module": "commonjs","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"lib": [ "es2015","dom" ],"noImplicitAny": true,"suppressImplicitAnyIndexErrors": true,"typeRoots": [
  4. "node_modules/@types/"
  5. ]
  6. }
  7. }

在这里,我尝试将其添加到compilerOptions中:

  1. "baseUrl": "." //it doesn't help.

之后:

  1. "baseUrl": ".","paths": {
  2. "*": [
  3. "*","src/agnular/*" // still nothing.
  4. ]
  5. }

我还添加了rootDirs,但我仍然收到此错误

  1. Cannot find module 'items'
首先,确保你已经安装了 TypeScript for Visual Studio 2015 – v2.5.2,之后:

tsconfig.json:

  1. {
  2. "compileOnSave": false,"compilerOptions": {
  3. "baseUrl": "./src","paths": {
  4. "@models/*": [ "angular/common/models/*" ]
  5. }
  6. //..
  7. }
  8. }

用法

  1. import { items } from '@models/items';

那么,这里发生了什么:

*指向我们文件的实际名称,因此在我们的情况下,在@models之后有*指向项目,而项目在angular / common / models / *内.

另外,我们应该将它添加到systemjs.config.js中,以便可以解析路径:

  1. System.config({
  2. paths: {
  3. // paths serve as alias
  4. 'npm:': 'node_modules/',"@models/*": ["rootDist/angular/common/models/*"]
  5. ^
  6. where you keep your files,for example dist/
  7. }
  8. // other things..
  9. });

另一个重要的事情是重新启动视觉工作室

我也试过卸载/重新加载项目,但这没有用.

猜你在找的Angularjs相关文章