Angular 8 快速開發(fā)

目錄

  1. 項目結(jié)構(gòu)簡述衰抑。
  2. 創(chuàng)建一個 component矮湘。
  3. 創(chuàng)建一個 service褐着。
  4. http 請求數(shù)據(jù)逃顶。
  5. 附 GitHub 源碼

1. 項目結(jié)構(gòu)簡述

.
├── angular.json
├── e2e
├── node_modules
├── package.json
├── src
└── tsconfig.json
angular.json 是 ng 8 的配置文件脾歧。
src 幾乎我們所有的代碼相關(guān)操作都在這個里面
e2e 測試使用 其他的一般不會用到

2. 創(chuàng)建一個 component

2.1創(chuàng)建

創(chuàng)建一個組件的基本命令 ng g c 組件名甲捏。

在終端輸入命令(目錄在項目根目錄即可)

有一些默認屬性需要注意。

默認新建的組件涨椒、服務(wù)摊鸡、守衛(wèi)...都會放在 src/app 目錄下,為了演示蚕冬,我們把所有的操作都放在一個叫做 first-app 的目錄下吧免猾。

ng g c first-app/helloWorld

目錄不用提前創(chuàng)建

robinu@bey-pc:~/workspace/ng.apps/garden-sunflower$ ng g c first-app/helloWorld
CREATE src/app/first-app/hello-world/hello-world.component.sass (0 bytes)
CREATE src/app/first-app/hello-world/hello-world.component.html (30 bytes)
CREATE src/app/first-app/hello-world/hello-world.component.spec.ts (657 bytes)
CREATE src/app/first-app/hello-world/hello-world.component.ts (289 bytes)
UPDATE src/app/app.module.ts (699 bytes)

好了。
├── first-app
│ └── hello-world
│ ├── hello-world.component.html
│ ├── hello-world.component.sass
│ ├── hello-world.component.spec.ts
│ └── hello-world.component.ts
4 個創(chuàng)建 1 個更新囤热。

4 個創(chuàng)建分別是 樣式文件猎提、 html文件spec.ts測試文件旁蔼、.ts文件锨苏。

1 個更新是在當(dāng)前 module 自動引入了我們新建的組件,可以打開 app.module.ts 查看棺聊。

需要注意的是伞租,任何新建的組件想要在當(dāng)前 module 中被使用,都必須在當(dāng)前 module 中引入限佩。這個在操作多個 module 時葵诈,會有體會。

2.2 修改

2.2.1 在殼組件中添加 hello-world 組件

關(guān)于 app.component 組件
app.component 組件即殼組件祟同,他是應(yīng)用程序執(zhí)行后默認加載組件作喘。
殼組件同樣默認由 4 個文件組成。

將如下內(nèi)容寫入 app.component.html 中:

<app-hello-world></app-hello-world>

這個標(biāo)簽就是我們新建的 hello world 組件晕城,名字是從 hello-world.component.ts 文件的 @Component 裝飾器的 selector 選擇器的值中獲取的泞坦。app- 是 cli 默認添加的前綴

@Component({
  selector: 'app-hello-world',

2.2.2 修改 hello-world 組件

<h3>你好世界</h3>

2.2.3 查看效果

ng s -o

我這里就不貼圖了。

3. 創(chuàng)建一個 service

3.1 創(chuàng)建

ng g s first-app/hello-world/helloWorld

我們在 hello-world 文件夾創(chuàng)建了一個 hello world 組件的 service砖顷。

當(dāng)然贰锁,這只是我們主觀意識上的 component 與 service 的關(guān)系,實際上他們目前只能算是住在同一個屋子里的兩個互相不認識的人滤蝠,妙的是碰巧倆人都姓 hello-world豌熄。

關(guān)于 service
service 目前可以大致理解為處理業(yè)務(wù)的腳本文件。他們通常帶有可注入裝飾器(@Injectable())并以注入的形式被使用几睛。
component 和 service 都是 class房轿,只是通過不同的裝飾器獲得了不同的表現(xiàn)粤攒。

3.2 說明

我們來做一個多語言的 hello world 列表所森,數(shù)據(jù)由 service 提供囱持,由 component 展示,也順便做一個插入新的 hello world 到 service 中的 hello world 列表的業(yè)務(wù)焕济。

service

  1. service 需要一個 string[]纷妆,存放 hello world

component

  1. component 需要使用 ngFor 渲染 hello world
  2. component 需要提供 添加 hello world 的方式

3.3 修改 service

hello-world.service.ts

import { Injectable } from "@angular/core";

@Injectable({
  providedIn: "root"
})
export class HelloWorldService {
  /**
   * hello world 列表
   */
  public helloWorlds: string[] = [];

  constructor() {}

  /**
   * 初始化數(shù)據(jù)
   */
  public init(): void {
    this.helloWorlds = ["你好世界", "????? ???????", "Salut tout le monde"];
  }
}

3.4 修改 component

hello-world.component.ts

import { Component, OnInit } from "@angular/core";
import { HelloWorldService } from "./hello-world.service";

@Component({
  selector: "app-hello-world",
  templateUrl: "./hello-world.component.html",
  styleUrls: ["./hello-world.component.sass"]
})
export class HelloWorldComponent implements OnInit {
  public hd: string = "";
  constructor(public hwS: HelloWorldService) {}

  ngOnInit() {
    this.hwS.init();
  }

  public append(): void {
    if (this.hd.trim() === "") return;
    this.hwS.helloWorlds.push(this.hd);
    this.hd = "";
  }
}

hello-world.component.html

<ul>
  <li *ngFor="let item of hwS.helloWorlds">
    {{item}}
  </li>
</ul>

<input type="text" [(ngModel)]="hd">
<button (click)="append()">添加</button>

需要注意的是,這里使用了 雙向綁定 如果這里報錯

Can't bind to 'ngModel' since it isn't a known property of 'input'

那就需要在 app.module.ts 中引入 FormsModule

...
import { FormsModule } from "@angular/forms";
...
imports: [..., FormsModule, ...],
...

至此晴弃,就可以正常運行了掩幢。
如果你的 ng s 仍在運行,刷新頁面即可上鞠。

4. http 請求數(shù)據(jù)

4.1 說明

目前我們的數(shù)據(jù)是從 service.init 中裝載的际邻,現(xiàn)在我們要把他修改為從外部獲取數(shù)據(jù)。
由于目前沒有 api 可用(你也可以自己寫一個來實際嘗試)芍阎,因此我們采用請求 json 文件的形式來獲取數(shù)據(jù)世曾。當(dāng)然其效果是相同的。

4.2 新建 json 文件

src/assets 目錄下谴咸,新建一個 datasource.json 文件轮听。

{
  "helloWorlds": [
    "你好世界",
    "Hello World"
  ]
}

4.3 引入 HttpClientModule

app.module.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";

import { HttpClientModule } from "@angular/common/http"; /** here */

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { PlanComponent } from "./plan/plan.component";
import { SearchBarComponent } from "./search-bar/search-bar.component";
import { HelloWorldComponent } from "./first-app/hello-world/hello-world.component";

@NgModule({
  declarations: [
    AppComponent,
    PlanComponent,
    SearchBarComponent,
    HelloWorldComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpClientModule /** here */
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

4.4 修改 service

hello-world.service.ts

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";

@Injectable({
  providedIn: "root"
})
export class HelloWorldService {
  /**
   * hello world 列表
   */
  public helloWorlds: string[] = [];
  readonly url = "assets/datasource.json";

  constructor(private _http: HttpClient) {}

  /**
   * 初始化數(shù)據(jù)
   */
  public init(): void {
    this._http.get(this.url).subscribe((data: { helloWorlds: string[] }) => {
      this.helloWorlds = data.helloWorlds;
    });
  }
}

4.5 查看效果

至此
新建 component、新建 service岭佳、HttpClient Restfull 獲取數(shù)據(jù)血巍,都已經(jīng)有涉及到了,也提到了一點雙向綁定珊随,代碼可能會有不合理的地方述寡,歡迎大家來指正,也希望能幫到看過此篇的童鞋玫恳。

5. GitHub

項目源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辨赐,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子京办,更是在濱河造成了極大的恐慌掀序,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惭婿,死亡現(xiàn)場離奇詭異不恭,居然都是意外死亡,警方通過查閱死者的電腦和手機财饥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門换吧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人钥星,你說我怎么就攤上這事沾瓦。” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵贯莺,是天一觀的道長风喇。 經(jīng)常有香客問我,道長缕探,這世上最難降的妖魔是什么魂莫? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮爹耗,結(jié)果婚禮上耙考,老公的妹妹穿的比我還像新娘。我一直安慰自己潭兽,他們只是感情好倦始,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著山卦,像睡著了一般楣号。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上怒坯,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天炫狱,我揣著相機與錄音,去河邊找鬼剔猿。 笑死视译,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的归敬。 我是一名探鬼主播酷含,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼汪茧!你這毒婦竟也來了椅亚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤舱污,失蹤者是張志新(化名)和其女友劉穎呀舔,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扩灯,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡媚赖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了珠插。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惧磺。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖捻撑,靈堂內(nèi)的尸體忽然破棺而出磨隘,到底是詐尸還是另有隱情缤底,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布番捂,位于F島的核電站训堆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏白嘁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一膘流、第九天 我趴在偏房一處隱蔽的房頂上張望絮缅。 院中可真熱鬧,春花似錦呼股、人聲如沸耕魄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吸奴。三九已至,卻和暖如春缠局,著一層夾襖步出監(jiān)牢的瞬間则奥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工狭园, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留读处,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓唱矛,卻偏偏與公主長得像罚舱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子绎谦,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容

  • core package 概要:Core是所有其他包的基礎(chǔ)包.它提供了大部分功能包括metadata管闷,templa...
    LOVE小狼閱讀 2,579評論 0 3
  • Angular CLI終極參考指南 如果翻譯內(nèi)容對你產(chǎn)品困擾,可查看原文The Ultimate Angular ...
    琢磨先生lf閱讀 2,773評論 0 4
  • <1>輸入屬性 定義:組件的輸入屬性窃肠,是指被@input裝飾器注解的屬性包个,用來從父組件接收數(shù)據(jù) 實例1.新建ord...
    mumumuu閱讀 588評論 0 1
  • 詩情畫意絕對是感情糾結(jié)與集結(jié)的產(chǎn)物!
    冰雨9527閱讀 106評論 0 0
  • 本方法可以讓C語言指令進一步接近匯編指令的執(zhí)行效率冤留,提高單片機赃蛛、嵌入式系統(tǒng)的速度和穩(wěn)定性,但編程時應(yīng)采取函數(shù)化的編...
    Leon_Geo閱讀 1,040評論 1 11