【angular2】(2)angular2自定义组件示例

来源:互联网 发布:搜狗输入法centos版 编辑:程序博客网 时间:2024/05/23 19:19

(1)用angular-cli搭建项目
(2)新建AComponent.ts

import {Component} from '@angular/core';@Component({  selector: 'app-a-component',  template: `<h2>测试A</h2>`})export class AComponent {  constructor() {    console.log('AAA');  }}

(3)在app.module.ts中引入

import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { FormsModule } from '@angular/forms';import { HttpModule } from '@angular/http';import { AppComponent } from './app.component';import {AComponent} from "./AComponent";@NgModule({  declarations: [    AComponent,    AppComponent  ],  imports: [    BrowserModule,    FormsModule,    HttpModule  ],  providers: [],  bootstrap: [AppComponent, AComponent]})export class AppModule {}

(4)在html中引用

<app-a-component></app-a-component>

PS:在我的项目中,在嵌套组件中,必须在调用该组件的最近一层的module注入,不知道是什么原因。。。还在摸索中。。。

0 0
原创粉丝点击