最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

angular4自定義組件詳解

 更新時(shí)間:2017年09月28日 11:23:49   作者:不了無(wú)明  
這篇文章主要為大家詳細(xì)介紹了angular4自定義組件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在 Angular 中,我們可以使用 {{}} 插值語(yǔ)法實(shí)現(xiàn)數(shù)據(jù)綁定。

新建組件

$ ng generate component simple-form --inline-template --inline-style
# Or
$ ng g c simple-form -it -is # 表示新建組件,該組件使用內(nèi)聯(lián)模板和內(nèi)聯(lián)樣式
//會(huì)自動(dòng)為simple-form生成simple-form.component.ts文件,文件中的selector為:app-simple-form,自動(dòng)添加了app-前綴

輸出:

installing component
 create src/app/simple-form/simple-form.component.spec.ts // 用于單元測(cè)試
 create src/app/simple-form/simple-form.component.ts // 新建的組件
 update src/app/app.module.ts //Angular CLI 會(huì)自動(dòng)更新 app.module.ts 文件。把新建的組件添加到 NgModule 的 declarations 

數(shù)組中

app.module.ts更新后:

@NgModule({
 declarations: [
  AppComponent,
  SimpleFormComponent
 ],
 ...
})
export class AppModule { }

創(chuàng)建 UserComponent 組件

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

@Component({ //Component 裝飾器來(lái)定義組件的元信息
 selector: 'sl-user',
 template: `
  <h2>大家好,我是{{name}}</h2>
  <p>我來(lái)自<strong>{{address.province}}</strong>省,
   <strong>{{address.city}}</strong>市
  </p>
   <p>{{address | json}}</p>//Angular 內(nèi)置的 json 管道,來(lái)顯示對(duì)象信息
`, }) 

//定義組件類(lèi)

export class UserComponent { 
  name = 'name'; 
  address = { province: 'province', city: 'city' } 
}


//使用構(gòu)造函數(shù)初始化數(shù)據(jù)
export class UserComponent {
  name: string;
  address: any;
  constructor() {
    this.name = 'name';
    this.address = {
      province: 'province',
      city: 'city'
    }
  }
}

//接口使用
interface Address {
  province: string;
  city: string;
}
export class UserComponent {
  name: string;
  address: Address;
  constructor(){
    this.name = 'name';
    this.address = {
      province: 'province',
      city: 'city'
    }
  }
}

定義數(shù)據(jù)接口( TypeScript 中的接口是一個(gè)非常靈活的概念,除了可用于對(duì)類(lèi)的一部分行為進(jìn)行抽象以外,也常用于對(duì)「對(duì)象的形狀(Shape)」進(jìn)行描述。)

interface Person {
 name: string;
 age: number;
}

let semlinker: Person = {
 name: 'semlinker',
 age: 31
};

聲明 UserComponent 組件

// ...
import { UserComponent } from './user.component';//載入
@NgModule({
 imports:   [ BrowserModule ],
 declarations: [ AppComponent, UserComponent],//聲明
 bootstrap:  [ AppComponent ]
})
export class AppModule { }

在AppComponent中使用 UserComponent 組件

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

@Component({
 selector: 'my-app',
 template: `
  <sl-user></sl-user> //UserComponent 的 selector
 `,
})
export class AppComponent {}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

葫芦岛市| 德惠市| 金昌市| 临江市| 红桥区| 黄大仙区| 崇义县| 阳曲县| 营山县| 潜山县| 二手房| 上犹县| 新乡县| 成安县| 西平县| 高碑店市| 阿鲁科尔沁旗| 南汇区| 隆德县| 莱阳市| 麻栗坡县| 鞍山市| 南皮县| 库伦旗| 安达市| 平湖市| 哈尔滨市| 高台县| 玉门市| 虎林市| 绩溪县| 洮南市| 库车县| 沐川县| 高唐县| 衡水市| 惠安县| 内江市| 定襄县| 汉源县| 固阳县|