2016-08-02 77 views
0

我在學習並使用Angular 2和Typescript創建一個Gulp構建過程。我沿着快速入門,一切正常。現在我試圖分支並嘗試做一些不同的事情。我想要做的是使用不同的文件夾結構,我遇到的問題與我的app.component.ts文件發現import { Component } from 'node_modules/@angular/core';。我已經將該文件更改爲import { Component } from '../../../manager/node_modules/@angular/core';,它可以正常工作,但我已經看到其他人有不同的文件夾結構,並且他們不使用長目錄路徑。Angular 2 Typescript Typings Path問題

目前我的文件夾結構如下:

build 
    - css 
    - js 
    // all compiled files end up here 
manager 
    - node_modules 
    - typings 
    gulpfile.ts 
    typings.json 
    package.json 
    tsconfig.json 
source 
    app.component.ts 
    main.ts 

app.component.ts

/// <reference path="../manager/typings/index.d.ts" /> 

import { Component } from '../manager/node_modules/@angular/core'; 
@Component({ 
    selector: 'my-app', 
    template: '<h1>My First Angular 2 App</h1>' 
}) 
export class AppComponent { } 

我的問題是:

  • 我可以引用@angular/core而無需編寫走出整條路?

  • 有沒有辦法可以省略<reference path="">

angular2-seed項目可以做到這一點,但我無法弄清楚他們是如何做到的。我確定他們有一個爲他們做文件參考的文件。

回答

2

嘗試使用以下文件夾結構:

build/ 
    css/ 
    js/ 
source/ 
    app.component.ts 
    main.ts 
node_modules/ 
typings/ 
gulpfile.ts 
typings.json 
package.json 
tsconfig.json 

說明:您tsconfig.json必須在根,所以打字稿編譯器將正確地解析@angular/core模塊。將node_modules文件夾和typings.json文件保存在根目錄中也是一個好主意。

要了解TypeScript如何解析模塊,請閱讀Module Resolution文檔。

+0

我搬我的文件,它的工作。謝謝! – Mike

+0

我的榮幸@Mike :) –