本教學課程示範如何為你的 Angular 應用程式建立新的元件。
你將學到什麼
你的應用程式將會有一個新元件:Home。
Angular 元件概念預覽
Angular 應用程式圍繞著元件打造,元件是 Angular 的基本組成單位。 元件包含程式碼、HTML 版面配置與 CSS 樣式資訊,提供應用程式中某個元素的功能與外觀。 在 Angular 中,元件可以包含其他元件。應用程式的功能與外觀可以被拆分、劃分到不同的元件中。
在 Angular 中,元件有定義其屬性的中繼資料。
當你建立 Home 時,你會用到下列屬性:
selector:描述 Angular 在範本中如何參照這個元件。imports:描述元件的相依關係。template:描述元件的 HTML 標記與版面配置。styleUrl:指定元件所使用的 CSS 檔案 URL。
-
建立
Home在這個步驟中,你會為應用程式建立一個新元件。
在 IDE 的 Terminal 窗格中:
在你的專案目錄中,進入
first-app目錄。執行下列指令,建立新的
Homeng generate component home執行下列指令,建置並提供你的應用程式。
NOTE: 這個步驟只適用於你的本機環境!
ng serve開啟瀏覽器,前往
http://localhost:4200找到應用程式。確認應用程式建置時沒有錯誤。
HELPFUL: 畫面應該和上一課一樣,因為雖然你加入了新元件,但你還沒有把它放進應用程式的任何範本裡。
讓
ng serve繼續執行,同時進行接下來的步驟。
-
將新元件加入應用程式的版面配置
在這個步驟中,你會將新元件
Home加入應用程式的根元件App,讓它顯示在應用程式的版面配置中。在 IDE 的 Edit 窗格中:
在編輯器中開啟
app.ts。在
app.ts中,將這一行加入檔案層級的 imports,匯入Home。在 src/app/app.ts 中匯入 Home
import {Component} from '@angular/core'; import {Home} from './home/home'; @Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/public/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home /> </section> </main> `, styleUrl: './app.css', }) export class App { title = 'homes'; }在
app.ts的@Component中,更新imports陣列屬性,加入Home。取代 src/app/app.ts 中的內容
import {Component} from '@angular/core'; import {Home} from './home/home'; @Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/public/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home /> </section> </main> `, styleUrl: './app.css', }) export class App { title = 'homes'; }在
app.ts的@Component中,更新template屬性,加入下列 HTML 程式碼。取代 src/app/app.ts 中的內容
import {Component} from '@angular/core'; import {Home} from './home/home'; @Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/public/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home /> </section> </main> `, styleUrl: './app.css', }) export class App { title = 'homes'; }儲存你對
app.ts的變更。如果
ng serve正在執行,應用程式應該會自動更新。 如果ng serve沒有在執行,請重新啟動它。 應用程式中的 Hello world 應該會變成來自Home的 home works!。在瀏覽器中檢查執行中的應用程式,確認應用程式已經更新。
-
為
Home加入功能在這個步驟中,你會為
Home加入功能。在上一個步驟中,你把預設的
Home加入應用程式的範本,讓它的預設 HTML 出現在應用程式中。 在這個步驟中,你會加入一個搜尋篩選器與按鈕,之後的課程會用到它。 目前Home就只有這些內容。 請注意,這個步驟只是把搜尋元素加入版面配置,還沒有加入任何功能。如果你是從全新的 Angular 專案開始,而不是下載起始專案 (ng new):請將下列全域樣式加入
src/styles.css,讓搜尋按鈕與輸入框邊框能夠顯示::root { --primary-color: #605dc8; --secondary-color: #8b89e6; --accent-color: #e8e7fa; --shadow-color: #e8e8e8; } button.primary { padding: 10px; border: solid 1px var(--primary-color); background: var(--primary-color); color: white; border-radius: 8px; }在 IDE 的 Edit 窗格中:
在
first-app目錄中,在編輯器裡開啟home.ts。在
home.ts的@Component中,用下列程式碼更新template屬性。取代 src/app/home/home.ts 中的內容
import {Component} from '@angular/core'; @Component({ selector: 'app-home', template: ` <section> <form> <input type="text" placeholder="Filter by city" /> <button class="primary" type="button">Search</button> </form> </section> `, styleUrl: './home.css', }) export class Home {}接著,在編輯器中開啟
home.css,用這些樣式更新內容。NOTE: 在瀏覽器中,這些樣式可以放在
src/app/home/home.ts的styles陣列裡。取代 src/app/home/home.css 中的內容
.results { display: grid; column-gap: 14px; row-gap: 14px; grid-template-columns: repeat(auto-fill, minmax(400px, 400px)); margin-top: 50px; justify-content: space-around; } input[type="text"] { border: solid 1px var(--primary-color); padding: 10px; border-radius: 8px; margin-right: 4px; display: inline-block; width: 30%; } button { padding: 10px; border: solid 1px var(--primary-color); background: var(--primary-color); color: white; border-radius: 8px; } @media (min-width: 500px) and (max-width: 768px) { .results { grid-template-columns: repeat(2, 1fr); } input[type="text"] { width: 70%; } } @media (max-width: 499px) { .results { grid-template-columns: 1fr; } }確認應用程式建置時沒有錯誤。你應該能在應用程式中找到篩選查詢框與按鈕,而且它們都有套用樣式。繼續下一步之前,請先修正任何錯誤。
SUMMARY: 在這一課中,你為應用程式建立了一個新元件,並為它加上篩選編輯控制項與按鈕。
如需本課所涵蓋主題的更多資訊,請造訪: