W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
組件是 Angular 應(yīng)用的主要構(gòu)造塊。每個(gè)組件包括如下部分:
本主題描述如何創(chuàng)建和配置 Angular 組件。
要查看或下載本主題中使用的范例代碼,請(qǐng)參閱 現(xiàn)場(chǎng)演練 / 下載范例。
要?jiǎng)?chuàng)建一個(gè)組件,請(qǐng)先驗(yàn)證你是否滿足以下先決條件:
ng new <project-name>
? 創(chuàng)建一個(gè),其中 ?<project-name>
? 是你的 Angular 應(yīng)用的名字。Angular CLI 是用來創(chuàng)建組件的最簡(jiǎn)途徑。你也可以手動(dòng)創(chuàng)建一個(gè)組件。
使用 Angular CLI 創(chuàng)建一個(gè)組件:
ng generate component <component-name>
? 命令,其中 ?<component-name>
? 是新組件的名字。默認(rèn)情況下,該命令會(huì)創(chuàng)建以下內(nèi)容:
<component-name>.component.ts
?<component-name>.component.html
?<component-name>.component.css
?<component-name>.component.spec.ts
?其中 ?<component-name>
? 是組件的名稱。
你可以更改 ?
ng generate component
? 創(chuàng)建新組件的方式。
雖然 Angular CLI 是創(chuàng)建 Angular 組件的最佳途徑,但你也可以手動(dòng)創(chuàng)建一個(gè)組件。本節(jié)將介紹如何在現(xiàn)有的 Angular 項(xiàng)目中創(chuàng)建核心組件文件。
要手動(dòng)創(chuàng)建一個(gè)新組件:
<component-name>.component.ts
? 。import { Component } from '@angular/core';
import
?語句之后,添加一個(gè) ?@Component
? 裝飾器。@Component({
})
@Component({
selector: 'app-component-overview',
})
@Component({
selector: 'app-component-overview',
templateUrl: './component-overview.component.html',
})
@Component({
selector: 'app-component-overview',
templateUrl: './component-overview.component.html',
styleUrls: ['./component-overview.component.css']
})
class
?語句。export class ComponentOverviewComponent {
}
每個(gè)組件都需要一個(gè) CSS 選擇器。選擇器會(huì)告訴 Angular:當(dāng)在模板 HTML 中找到相應(yīng)的標(biāo)簽時(shí),就把該組件實(shí)例化在那里。例如,考慮一個(gè)組件 ?hello-world.component.ts
? ,它的選擇器定義為 ?app-hello-world
? 。 當(dāng) ?<app-hello-world>
? 出現(xiàn)在模板中時(shí),這個(gè)選擇器就會(huì)讓 Angular 實(shí)例化該組件。
在 ?@Component
? 裝飾器中添加一個(gè) ?selector
?語句來指定組件的選擇器。
@Component({
selector: 'app-component-overview',
})
模板是一段 HTML,它告訴 Angular 如何在應(yīng)用中渲染組件??梢酝ㄟ^以下兩種方式之一為組件定義模板:引用外部文件,或直接寫在組件內(nèi)部。
要把模板定義為外部文件,就要把 ?templateUrl
?添加到 ?@Component
? 裝飾器中。
@Component({
selector: 'app-component-overview',
templateUrl: './component-overview.component.html',
})
要在組件中定義模板,就要把一個(gè) ?template
?屬性添加到 ?@Component
? 中,該屬性的內(nèi)容是要使用的 HTML。
@Component({
selector: 'app-component-overview',
template: '<h1>Hello World!</h1>',
})
如果你想讓模板跨越多行,可以使用反引號(hào)( ?`
? )。例如:
@Component({
selector: 'app-component-overview',
template: `
<h1>Hello World!</h1>
<p>This template definition spans multiple lines.</p>
`
})
Angular 組件需要一個(gè)用 ?
template
?或 ?templateUrl
?定義的模板。但你不能在組件中同時(shí)擁有這兩個(gè)語句。
有兩種方式可以為組件的模板聲明樣式:引用一個(gè)外部文件,或直接寫在組件內(nèi)部。
要在單獨(dú)的文件中聲明組件的樣式,就要把 ?styleUrls
?屬性添加到 ?@Component
? 裝飾器中。
@Component({
selector: 'app-component-overview',
templateUrl: './component-overview.component.html',
styleUrls: ['./component-overview.component.css']
})
要想在組件內(nèi)部聲明樣式,就要把 ?styles
?屬性添加到 ?@Component
?,該屬性的內(nèi)容是你要用的樣式。
@Component({
selector: 'app-component-overview',
template: '<h1>Hello World!</h1>',
styles: ['h1 { font-weight: normal; }']
})
?styles
?屬性接受一個(gè)包含 CSS 規(guī)則的字符串?dāng)?shù)組。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: