简体   繁体   English

Angular 2模块导入中断应用程序

[英]Angular 2 module import breaking app

I'm doing the quickstart tutorial and am encountering some error. 我正在做快速入门教程,遇到一些错误。 The following is my file structure: 以下是我的文件结构:

//## 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);

I am confused, as this is a pretty simple example. 我很困惑,因为这是一个非常简单的示例。 The Home module is causing the application to fail somehow. Home模块导致应用程序以某种方式失败。 It gives the following error: 它给出以下错误:

ZoneAwareErrormessage: "(SystemJS) Cannot set property 'stack' of undefined ... followed by hundreds of lines of the same. Does anyone have any ideas on how to debug errors like these? ZoneAwareErrormessage: "(SystemJS) Cannot set property 'stack' of undefined ...后跟数百行相同的ZoneAwareErrormessage: "(SystemJS) Cannot set property 'stack' of undefined ... 。有人对如何调试此类错误有任何想法吗?

如果HomeModule是功能模块,请将声明部分更改为declarations: [CommonModule, HomeComponent]

I get the same error,and it's cause the zone find some error in my template. 我遇到同样的错误,这是因为该区域在我的模板中发现了一些错误。 I'm in Angular 2.4.1 and zone 0.7.4,try upgrade. 我在Angular 2.4.1和0.7.4区域中,请尝试升级。

btw,I had set the template property to the url of my template,it's work,so,I think the zone may has a bug or something. 顺便说一句,我已经将template属性设置为模板的url,它可以正常工作,因此,我认为该区域可能存在错误或其他错误。

You must import CommonModule in the HomeModule. 您必须导入CommonModule在HomeModule。 Your HomeModule code will be like this: 您的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 { }

I coudn't fix this, but for anyone experiencing the same problem, avoid the quickstart tutorial and use angular-cli to generate things instead. 我无法解决此问题,但是对于遇到相同问题的任何人,请避免使用快速入门教程,而应使用angular-cli生成事物。 It seems much cleaner and easier to understand when starting out. 入门时似乎更简洁易懂。

ng new new-project --style=sass should sort you out. ng new new-project --style=sass应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM