W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
首先,使用 Angular CLI 來創(chuàng)建最初的應用程序。在本教程中,你將修改并擴展這個入門級的應用程序,以創(chuàng)建出《英雄之旅》應用。
在教程的這個部分,你將完成下列工作:
要查看本頁所講的范例程序,請參閱現(xiàn)場演練 / 下載范例。
要想搭建開發(fā)環(huán)境,請遵循搭建本地環(huán)境中的步驟進行操作。
Angular 的工作區(qū)就是你開發(fā)應用所在的上下文環(huán)境。一個工作區(qū)包含一個或多個項目所需的文件。每個項目都是一組由應用、庫或端到端(e2e)測試組成的文件集合。在本教程中,你將創(chuàng)建一個新的工作區(qū)。
要想創(chuàng)建一個新的工作區(qū)和一個初始應用項目,需要:
ng new
?,空間名請使用 ?angular-tour-of-heroes
?,如下所示:ng new angular-tour-of-heroes
ng new
? 命令會提示你輸入要在初始應用項目中包含哪些特性,請按 Enter 或 Return 鍵接受其默認值。Angular CLI 會安裝必要的 Angular ?npm
?包和其它依賴項。這可能需要幾分鐘。
它還會創(chuàng)建下列工作區(qū)和初始項目的文件:
angular-tour-of-heroes
?。src/app
? 子目錄下。初始應用項目是一個簡單的 "歡迎" 應用,隨時可以運行它。
進入工作區(qū)目錄,并啟動這個應用。
cd angular-tour-of-heroes
ng serve --open
?ng serve
? 命令會構建本應用、啟動開發(fā)服務器、監(jiān)聽源文件,并且當那些文件發(fā)生變化時重新構建本應用。
?--open
? 標志會打開瀏覽器,并訪問 ?http://localhost:4200/
?。
你會發(fā)現(xiàn)本應用正運行在瀏覽器中。
你所看到的這個頁面就是應用的外殼。這個外殼是被一個名叫 ?AppComponent
?的 Angular 組件控制的。
組件是 Angular 應用中的基本構造塊。它們在屏幕上顯示數(shù)據(jù),監(jiān)聽用戶輸入,并且根據(jù)這些輸入執(zhí)行相應的動作。
用你最喜歡的編輯器或 IDE 打開這個項目,并訪問 ?src/app
? 目錄,來對這個起始應用做一些修改。
你會在這里看到 ?AppComponent
?殼的三個實現(xiàn)文件:
文件 |
詳情 |
---|---|
app.component.ts
|
組件的類代碼,這是用 TypeScript 寫的。 |
app.component.html
|
組件的模板,這是用 HTML 寫的。 |
app.component.css
|
組件的私有 CSS 樣式。 |
打開組件的類文件 (?app.component.ts
?),并把 ?title
?屬性的值修改為 'Tour of Heroes'(英雄之旅)。
title = 'Tour of Heroes';
打開組件的模板文件 ?app.component.html
? 并清空 Angular CLI 自動生成的默認模板。改為下列 HTML 內容。
<h1>{{title}}</h1>
雙花括號語法是 Angular 的插值綁定語法。這個插值綁定的意思是把組件的 ?title
?屬性的值綁定到 HTML 中的 ?h1
?標記中。
瀏覽器自動刷新,并且顯示出了新的應用標題。
大多數(shù)應用都會努力讓整個應用保持一致的外觀。因此,CLI 會生成一個空白的 ?styles.css
? 文件。你可以把全應用級別的樣式放進去。
打開 ?src/styles.css
? 并把下列代碼添加到此文件中。
/* Application-wide Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[type="text"], button {
color: #333;
font-family: Cambria, Georgia, serif;
}
button {
background-color: #eee;
border: none;
border-radius: 4px;
cursor: pointer;
color: black;
font-size: 1.2rem;
padding: 1rem;
margin-right: 1rem;
margin-bottom: 1rem;
margin-top: 1rem;
}
button:hover {
background-color: black;
color: white;
}
button:disabled {
background-color: #eee;
color: #aaa;
cursor: auto;
}
/* everywhere else */
* {
font-family: Arial, Helvetica, sans-serif;
}
下面是本頁所提到的源代碼。
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Tour of Heroes';
}
<h1>{{title}}</h1>
/* Application-wide Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[type="text"], button {
color: #333;
font-family: Cambria, Georgia, serif;
}
button {
background-color: #eee;
border: none;
border-radius: 4px;
cursor: pointer;
color: black;
font-size: 1.2rem;
padding: 1rem;
margin-right: 1rem;
margin-bottom: 1rem;
margin-top: 1rem;
}
button:hover {
background-color: black;
color: white;
}
button:disabled {
background-color: #eee;
color: #aaa;
cursor: auto;
}
/* everywhere else */
* {
font-family: Arial, Helvetica, sans-serif;
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: