2016-01-19 52 views
8

我試圖導入我的app.ts中的自定義組件,但得到錯誤「TS2307:找不到模塊'MyComponent'」我已經看過它,找不到任何幫助我的東西。無法導入自定義組件 - Angular2和TypeScript

有人寫道:使用「moduleResolution」:「經典」,但如果我這樣做,那麼我得到的一切(所有angular2/...),除了爲MyComponent的TS2307錯誤。

其他人寫了「run tsc --declaration MyComponent.ts」 - 它會生成一個MyComponent.d.ts文件(同時在控制檯中引發錯誤,比如「除非提供了--module標誌,否則無法編譯」 - IS在tsconfig中作爲一個選項提供 - 或者「無法找到模塊'angular2/common'」用於我在MyComponent.ts中導入的任何東西 - 但它確實生成了.d.ts文件),但是當嘗試編譯app.ts時仍然給出相同的TS2307錯誤。 Nohting我發現工作。

這是我的項目結構:

| /app 
|  /resources 
|   /dev 
|    /ts 
|     app.ts 
|     MyComponent.ts 
|   /dist 
|    /js 
|     app.js   // transcompiled app.ts 
|     MyComponent.js // transcompiled MyComponent.ts 
|    /modules   // files copied from the /node_modules/**/ folders (because node_modules is outside versioning and also outside public root (public root is the /app folder) 
|     es6-shim.js 
|     angular2-polyfills.js 
|     system.src.js 
|     Rx.js 
|     http.dev.js 
|     angular2.dev.js 
|  index.html 
| 
| /node_modules 
|  /angular2   // 2.0.0-beta.1 
|  /es6-promise   // 3.0.2 
|  /es6-shim   // 0.33.3 
|  /rxjs    // 5.0.0-beta.0 
|  /reflect-metadata // 0.1.2 
|  /systemjs   // 0.19.6 
|  /zone.js    // 0.5.10 
| 

這是我tsconfig.json

{ 
    "compilerOptions": { 
     "target": "ES5", 
     "module": "system", 
     "moduleResolution": "node", 
     "sourceMap": true, 
     "emitDecoratorMetadata": true, 
     "experimentalDecorators": true, 
     "removeComments": true, 
     "noImplicitAny": false, 
     "outDir": "app/resources/dist/js" 
    }, 
    "files": [ 
     "app/resources/dev/ts/app.ts" 
    ] 
} 

app.ts

import { bootstrap } from "angular2/platform/browser"; 
import { Component, View } from "angular2/core"; 
import { HTTP_PROVIDERS, Http, Response } from "angular2/http"; 

//My Custom Components: 
import { MyComponent } from 'MyComponent'; 

/** 
* APP 
*/ 
@Component({ 
    selector: 'my-app' 
}) 
@View({ 
    directives: [MyComponent], 
    template: ` 
     <my-component></my-component> 
     plus other html 
    ` 
}) 
class MyApp { 
    public whatever; 

    constructor(public http: Http) { 
     // initialize this.whatever 
    } 

    makeRequest(): void { 
     this.http.request('/_http/response.json') 
      .subscribe((res:Response) => { 
       this.whatever = res.json(); 
      }); 
    } 
} 

bootstrap(MyApp, [ HTTP_PROVIDERS ]); 

這是我的我的Component.ts

import { Component } from 'angular2/core'; 
import { NgFor, NgIf } from 'angular2/common'; 

@Component({ 
    selector: 'my-component', 
    template: ` 
     <div>MY IMPORTED COMPONENT</div> 
    ` 
}) 
export class MyComponent { 

    constructor() {} 

    doSomething(): void { 
     console.log('Doing something'); 
    } 
} 

app.tsMyComponent.ts都在同一個文件夾中。無論如何,我也試過這樣的路徑:從「/ app/resources/dev/ts/MyComponent」導入{MyComponent}。 此外,我已經嘗試將它添加到tsconfig中的文件數組(有些人寫道,我應該這樣做)...仍然...沒有任何工作!我也檢查了js輸出,因爲有些人寫道,即使它拋出錯誤,它仍然可能被編譯...沒有運氣!

====================

編輯:

好吧,作爲inoabrian第一意見建議...我想我試着一切,但似乎我沒有嘗試導入部分「./MyComponent」。

import { MyComponent } from './MyComponent'; 

現在,反編譯器是它的工作。謝謝inoabrian,但現在我有另一個問題。當我在瀏覽器中嘗試運行,檢查我的控制檯和兩個systemjs和angular2-pollyfills尖叫:

http://my-app/resources/dist/js/MyComponent 404 (Not Found) 

分別爲:

XHR error (404 Not Found) loading http://my-app/resources/dist/js/MyComponent(…) 

不要路徑(當比較揭去我的項目文件夾結構),當我訪問時我有本地服務器指向/ app http://my-app

這是我的索引。HTML與系統配置和所有包含js文件:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <!-- ... Meta tags & CSS assets --> 

    <script src="resources/dist/modules/es6-shim.js"></script> 
    <script src="resources/dist/modules/angular2-polyfills.js"></script> 
    <script src="resources/dist/modules/system.src.js"></script> 
    <script src="resources/dist/modules/Rx.js"></script> 
    <script src="resources/dist/modules/http.dev.js"></script> 
    <script src="resources/dist/modules/angular2.dev.js"></script> 
</head> 
<body> 
<script> 
    System.config({ 
     packages: { 
      app: { 
       format: 'register', 
       defaultExtension: 'js' 
      } 
     } 
    }); 
    System.import('resources/dist/js/app.js') 
      .then(null, console.error.bind(console)); 
</script> 

<my-app></my-app> 

</body> 
</html> 

我有我應該做的一件事與transcompiled文件「MyComponent.js」,但不知道什麼(我試過的感覺將其添加到System.import,希望它會接受一個數組......但它似乎沒有)。接下來我應該做什麼?

P.S.然而,這是令人失望的,我希望導入MyComponent到app.ts將是一切,並且我會找到MyComponent類+從MyComponent.ts +我的經過編譯的app.ts文件中的所有內容ALL IN ONE app.js文件,沒有另一個js文件:(

+0

你是否已將導入位置更改爲'./MyComponent'? – inoabrian

+0

我以爲我已經嘗試了一切,但似乎我沒有。它與「./MyComponent」一起工作......但現在事情引發了另一個問題,我編輯了我的問題,並在「編輯:」 – MrCroft

+0

之後在最後添加了該問題。現在,這部分似乎是您的服務器的目錄問題。 當您有嵌套的開發和dist的目錄時,可能會發生這種情況。 所以你可以發佈你的服務器端代碼的樣子..? 我跑斷精簡版服務器 的,我通過設置一系列送達我的目錄 [ ] – inoabrian

回答

8

我知道它早該過期,但也許其他人可能會從答案中受益。 我確實意識到最終的問題是什麼(除了我在原始問題中已經編輯過的內容)。這裏是: 我確實提到我有一個自定義文件夾結構,並不願意改變它。如果我有,它會通過簡單地跟隨角2快速啓動。但它不會讓我意識到我的具體情況中的問題,而且我也不會理解System.js是如何工作的。

問題出在我的System.config() - 包部分。 這是不好的代碼:

System.config({ 
    packages: { 
     app: { 
      format: 'register', 
      defaultExtension: 'js' 
     } 
    } 
}); 
System.import('my/path/to/app_main_file'); 

我的錯誤是以爲關鍵「應用程序」只是一個普通的鑰匙,從單詞「應用」正在添加。事實證明,在packages對象中,每個鍵實際上代表一個路徑(該程序包 - 它是文件在哪裏)駐留在您的應用程序中。因此,假設在我的transpiled文件夾將是「我/應用/公衆」和「app_main_file」是我的應用程序的主要js文件,然後我實際上已經有這樣的事情:

System.config({ 
    packages: { 
     'my/app/public': { 
      format: 'register', 
      defaultExtension: 'js' 
     } 
    } 
}); 
System.import('my/app/public/app_main_file') 
    .then(null, console.error.bind(console)); 

或者,你可以寫在配置部分,甚至更好,就像這樣:

System.config({ 
    packages: { 
     'my-app': { 
      format: 'register', 
      defaultExtension: 'js' 
     } 
    }, 
    map: { 
     'my-app': 'my/app/public' 
    } 
}); 

我認爲這是相當不言自明的是什麼「映射」功能以及如何使用它。

P.S.我知道在我的回答中,我沒有按照原來的問題完全一樣的文件夾結構,但我認爲像這樣解釋它更有意義。如果有問題,只需用我原來在問題「resources/dist/js /」中所取代的「my/app/public」替換,這是一回事。

+1

太棒了!它的工作,甚至我認爲應用程序作爲關鍵字。 – sudhir

0

什麼是有點奇怪的是,你沒有一個MyComponent.js同一文件夾內比app.js。對我來說,當我在tsconfig.json文件中添加outDir,我所有的打字稿文件。編譯成JavaScript此文件夾中

這裏是我的配置在我tsconfig.json文件:

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "system", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false, 
    "outDir": "app/resources/dist/js" 
}, 
    "exclude": [ 
    "node_modules" 
    ] 
} 

我使用以下命令運行編譯階段:tsc -w

希望它可以幫助你, 蒂埃裏

+0

看來我還沒有嘗試過所有的東西。我嘗試使用「./MyComponent」作爲app.ts中的導入部分,並且它工作,transcompiler完成了它的工作 - 它不是一個反編譯器問題,而是一個路徑問題(不知道我怎麼沒有嘗試過「 ./「已經)和YES,現在我確實有一個MyComponent.js文件...但是現在我有另一個問題,我編輯了我的問題,並在編輯完成後添加了問題: – MrCroft

+0

在您的項目截圖你有'MyComponent.ts'但不是瀏覽器正在尋找的'MyComponent.js',你有編譯它嗎? – Langley

+0

文件MyComponent.js在那裏,我只是沒有更新項目的文件夾/文件結構在我的問題。我現在做了。 – MrCroft

1

我相信你缺少系統配置適當的參數。 添加這樣的:

System.config({ 
    defaultJSExtensions: true 
}) 
+0

好吧,添加這個不會在MyComponent上拋出404(因爲現在它實際上是尋找MyComponent.js) - 我應該注意到了。 但它打破了所有的休息。現在我得到了404s,因爲它分別在/angular2/platform/browser.js的錯誤位置查找http.js和core.js(當然不在/ angular2中 - 它們實際上在公共根目錄之外)(/ app是我的公共根) - 但之前沒有404s,因爲它甚至沒有在添加defaultJSExtensions之前請求它們:true,並且一切正常)。爲什麼地球上會發生這種情況:| ? – MrCroft

+0

我建議通過快速入門或教程:https://angular.io/docs/ts/latest/quickstart.html,你也應該創建一個plunkr,因爲這似乎是一個無休止的故事...... :)您的其他文件無法訪問,可能是因爲您沒有在index.html中加載它們。我相信如果你檢查教程,它應該開始工作。還有一點需要注意的是,beta1存在一些問題,所以我現在建議使用beta0。 – eesdil

+0

另外,如果你有一個自定義文件夾結構,起初很難抓住它,所以我建議現在按照角度頁上的建議。 – eesdil

-4

添加.js擴展你的模塊導入。這很可能會拋出IDE錯誤。但是,當你刷新它的工作。這也是我面臨的問題。它昨天工作,今天我在家裏嘗試了另一個例子,它會給404錯誤。

import {MyComponent} from 'MyComponent.js' 

這爲我工作的另一種解決方法是: 我有在根文件夾中所有.ts文件。我創建了另一個子文件夾應用程序,並將它們移到那裏。從那裏開始,我可以在沒有擴展名的情況下引用它們。不知道爲什麼它不能在根文件夾中工作。但是將它們移動到另一個文件夾中卻有竅門希望它有幫助