【发布时间】:2017-01-05 20:28:22
【问题描述】:
我正在使用 angular cli 接口来为 init 简单项目探索 angular2 的表面。
我成功创建了项目,但是当我使用 ng generate component test 添加新组件时
组件的创建方式类似于
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
但让我感到困惑的是,我无法在 <test>Loading</test> 选择器下渲染任何东西(加载除外),我在渲染 app-root 后立即放置了 index.html
<app-root>Loading...</app-root>
<test>Loading ...</test>
test.component.html
<p>
test works!
</p>
我在这里做错了什么?
更新: 主模块知道这个组件会导致 angularcli 添加它并且所有导入都会自动
@NgModule({
declarations: [
AppComponent, TestComponent
],
更新 2:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
【问题讨论】:
-
是否包含
app.module.ts中的所有组件?你喜欢什么文件,main.ts,app.module.ts,app.component.ts? -
是的,所有都包含在 angular cli 工具中。
-
您的主模块(基本上称为 app.module.ts)需要先了解每个组件,然后才能使用它们。所以正如@CharanCherry 已经提到的,你导入了吗?
-
你能告诉我
app-root组件代码吗? -
任何控制台错误?
标签: angular