2017-01-12 45 views
0

我正在做快速入门教程并遇到一些错误。以下是我的文件结构:Angular 2模块导入破解应用程序

//## home/home.component.ts ################# 
import {Component} from '@angular/core'; 

@Component({ 
    selector:'home', 
    template: ` 
<div>I'm a home component.</div> 
` 
}) 

export class HomeComponent{} 

//## home/home.module.ts ################# 
import {NgModule} from "@angular/core"; 
import {HomeComponent} from "./home.component"; 

NgModule({ 
    declarations: [HomeComponent], 
    exports: [HomeComponent] 
}); 

export class HomeModule { 
} 

//## app.component.ts ################# 
import {Component} from "@angular/core"; 
@Component({ 
    selector: 'app', 
    template: ` 
    <div>I'm the App Component</div> 
    ` 
}) 
export class AppComponent {} 

//## app.module.ts ################# 
import {AppComponent} from "./app.component"; 
import {BrowserModule} from "@angular/platform-browser"; 
import {NgModule} from "@angular/core"; 
import {HomeModule} from "./home/home.module"; //this breaks it 

@NgModule({ 
    declarations: [AppComponent], 
    bootstrap: [AppComponent], 
    imports: [BrowserModule, HomeModule] //home module breaks it 
}) 

export class AppModule{} 

//## main.ts ################# 
import {platformBrowserDynamic} from "@angular/platform-browser-dynamic"; 
import {AppModule} from "./app.module"; 

platformBrowserDynamic().bootstrapModule(AppModule); 

我很困惑,因为这是一个非常简单的例子。 Home模块导致应用程序以某种方式失败。它给出了以下错误:

ZoneAwareErrormessage: "(SystemJS) Cannot set property 'stack' of undefined ...其次是数百行相同。有没有人有关于如何调试这些错误的任何想法?

+0

更改声明:[AppComponent],声明:[BrowserModule,HomeModule],改变了什么? –

+0

@SebastienDErrico无遗憾不是 – userqwert

回答

0

我coudn't解决这个问题,但任何人都遇到同样的问题,避免了快速入门教程和使用的角度,CLI生成的东西来代替。看起来更清晰,更易于理解。

ng new new-project --style=sass应该排序。

0

如果HomeModule是一个功能模块,改变你的声明部分是declarations: [CommonModule, HomeComponent]

0

我得到了同样的错误,这是造成该区域找到我的模板中的一些错误。我在Angular 2.4.1和0.7.4区域尝试升级。

顺便说一句,我已经将模板属性设置为我的模板的网址,它的工作,所以,我认为该区域可能有一个错误或什么的。

0

您必须在HomeModule中导入CommonModule。你HomeModule代码将是这样的:

import {NgModule} from "@angular/core"; 
import {CommonModule} from "@angular/common"; // Add this 
import {HomeComponent} from "./home.component"; 

NgModule({ 
    imports: [CommonModule], // Add this 
    declarations: [HomeComponent], 
    exports: [HomeComponent] 
}); 

export class HomeModule { } 
+0

这并不能解决我的问题。 CommonModule在这里做什么? – userqwert

+0

CommonModule是BrowserModule的子集(您在AppModule中使用的一个子集),您必须将其导入到所有功能模块中。有关[Angular2文档中的CommonModule]的更多信息](https://angular.io/docs/ts/latest/guide/ngmodule.html#!#imports) –

+0

@userqwert另请参阅有关Angular文档的此常见问题解答 - [我应该导入BrowserModule或CommonModule?](https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-browser-vs-common-module) –

相关问题