2017-10-15 30 views
0

我与角2个初学者,并已开始与包括这些文件的一个小项目:<app-root>是没有得到填充

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 

import { MaterialModule } from './material/material.module'; 
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 

import { AppComponent } from './app.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    ], 
    imports: [ 
    BrowserModule, 
    MaterialModule, 
    BrowserAnimationsModule, 
    AppComponent, 
    NgModule 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

app.component。 TS

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
}) 
export class AppComponent { 
    title = 'App Title'; 
} 

app.component.html

<span>Welcome to {{title}}</span> 

而且,我的index.html看起来是这样的:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>My App</title> 
    <base href="/"> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="icon" type="image/x-icon" href="favicon.ico"> 
</head> 
<body> 
    <app-root></app-root> 
</body> 
</html> 

我遇到的问题是,即使一切编译罚款,该<app-root>标签停留在结果页面空我的模板HTML不会被添加。我已经通过更改模板URL来测试模板加载是否存在问题,但它无法找到我的模板文件并且编译失败。这意味着这不是问题。

有什么我在这里失踪?

回答

4

您已将NgModule修饰符和AppComponent类添加到模块的imports部分,这是错误的。从您的imports部分删除NgModule装饰器。同时从模块的imports中删除AppComponent

@NgModule({ 
    declarations: [ 
    AppComponent, 
    ], 
    imports: [ 
    BrowserModule, 
    MaterialModule, 
    BrowserAnimationsModule 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

imports属性应只包含装饰有NgModule类。