2015-04-27 47 views
6

模板,我跟着Angular 2 Quickstart指南,但出現以下錯誤:錯誤:未找到MyAppComponent

Error during instantiation of Token(AppView)!. ORIGINAL ERROR: Error: No template found for MyAppComponent

這是相關代碼:

import {Component, View} from 'angular2/core'; 
import {bootstrap} from 'angular2/platform/browser' 
// Note I've changed Template to View here as I think it is a typo 

.... 

@View({ 
    template: '<h1>Hello {{ name }}</h1>' 
}) 

.... 

這裏繼鄧肯展位的建議http://blog.ionic.io/angular-2-series-introduction/我做出了以下更改和一切正常:

import {Component, Template} from 'angular2/core'; 
import {bootstrap} from 'angular2/platform/browser'; 

.... 

@Template({ 
    inline: "<h1>Hello {{ name }}</h1>" 
}) 

.... 

但是yesimahum在同一鏈接上的評論提到「查看是新的語法」,這似乎是,Angular 2 Step by Step Guide中所示的情況。

那麼爲什麼Quickstart代碼會拋出錯誤,什麼是正確的修復?

回答

1

我想你需要檢查了這一點

import {Component, Template, bootstrap} from 'angular2/angular2'; 

@Component({ 
    selector: 'my-app' 
}) 
@Template({ 
    inline: '<h1>Hello {{ name }}</h2>' 
}) 

class MyAppComponent { 
    constructor() { 
     this.name = 'Alice'; 
    } 
} 

bootstrap(MyAppComponent); 
+0

在我的問題中,我特意只顯示了相關代碼,並且我還說View和** not ** Template是新的語法。 –

+0

我完全理解你說的話,因爲這就是它在文檔中的內容....我不知道爲什麼它現在不工作 –

0

而只是爲了澄清:如果你需要包括一些指令,你應該這樣來做:

import {Component, Template, View, CSSClass, NgFor, bootstrap} from 'angular2/angular2'; 

// Component annotations .. 
@Component({ 
    selector: 'my-app' 
}) 
@Template({ 
    url: 'templates/layout.html' 
}) 
@View({ 
    directives: [CSSClass, NgFor] 
}) 

,然後在你可以使用[class],* ng-for等代碼:)