【CuteJavaScript】Angular6入門項目(2.構建項目頁面和組件)

本文目錄

  • 一、項目起步
  • 二、編寫路由組件
  • 三、編寫頁面組件
    • 1.編寫單一組件
    • 2.模擬數(shù)據(jù)
    • 3.編寫主從組件
  • 四、編寫服務
    • 1.為什么需要服務
    • 2.編寫服務
  • 五、引入RxJS
    • 1.關于RxJS
    • 2.引入RxJS
    • 3.改造數(shù)據(jù)獲取方式
  • 六、改造組件
    • 1.添加歷史記錄組件
    • 2.添加和刪除歷史記錄
  • 七、HTTP改造
    • 1.引入HTTP
    • 2.通過HTTP請求數(shù)據(jù)
    • 3.通過HTTP修改數(shù)據(jù)
    • 4.通過HTTP增加數(shù)據(jù)
    • 5.通過HTTP刪除數(shù)據(jù)
    • 6.通過HTTP查找數(shù)據(jù)

本項目源碼放在github

三、編寫頁面組件

接下來開始編寫頁面組件,這里我們挑重點來寫,一些布局的樣式,后面可以看源碼。

1.編寫單一組件

我們首先寫一個書本信息的組件,代碼如下:

<!-- index.component.html -->
<div class="content">
  <div class="books_box">
    <!-- 單個課本 -->
    <div class="books_item" *ngFor="let item of [1,2,3,4,5,6,7,8,9,10]">
      <img class="cover" src="https://img3.doubanio.com/view/subject/m/public/s29988481.jpg">
      <div class="title"><a>像火焰像灰燼</a></div>
      <div class="author">程姬</div>
    </div>
  </div>
</div>

知識點
*ngFor 是一個 Angular 的復寫器(repeater)指令,就像angular1中的ng-forvuejs中的v-for。 它會為列表中的每項數(shù)據(jù)復寫它的宿主元素。
這時候可以看到頁面變成下面這個樣子:

圖片3-1

接下來我們要把寫死在HTML上面的數(shù)據(jù),抽到JS中:

現(xiàn)在先新建一個books.ts文件來定義一個Book類,并添加id,url,titleauthor四個屬性:

// src/app/books.ts
export class Book {
    id: number;
    url: string;
    title: string;
    author: string;
}

然后回到index.component.ts文件去引入它,并定義一個books屬性,使用導入進來的Book類作為類型:

// index.component.ts
import { Book } from '../books';
export class IndexComponent implements OnInit {
  books: Book = {
    id: 1,
    url: 'https://img3.doubanio.com/view/subject/m/public/s29988481.jpg',
    title: '像火焰像灰燼',
    author: '程姬',
  }
}

然后再改造前面的組件文件index.component.html:

<!-- index.component.html -->
<div class="books_item" *ngFor="let item of [1,2,3,4,5,6,7,8,9,10]">
  <img class="cover" src="{{books.url}}" alt="{{books.id}}">
  <div class="title">
    <a>{{books.title}}</a>
  </div>
  <div class="author">{{books.author}}</div>
</div>

接著,我們再為每個課本添加一個點擊事件,來實現(xiàn)點擊封面圖能查看大圖的效果,現(xiàn)在index.component.ts中定義一個getDetailImage方法,并在index.component.html中綁定該方法:

// index.component.ts
export class IndexComponent implements OnInit {
  getDetailImage(books){
    alert(`正在查看id為${books.id}的大圖!`);
  }
}

這邊方法的具體實現(xiàn),不寫,不是本文重點。下面是增加點擊事件的綁定:

<!-- index.component.html -->
<img class="cover" src="{{books.url}}" alt="{{books.id}}" (click)="getDetailImage(books)">

知識點
(click)是Angular用來綁定事件,它會讓 Angular 監(jiān)聽這個<img> 元素的 click 事件。 當用戶點擊 <img> 時,Angular 就會執(zhí)行表達式 getDetailImage(books)。

再來,我們引入前面學到的路由鏈接指令來改造HTML:

<!-- index.component.html -->
<a routerLink="/detail/{{books.id}}">{{books.title}}</a>

這時候,我們在點擊書本的標題,發(fā)現(xiàn)頁面跳轉到URL地址為http://localhost:4200/detail/1的頁面,這就說明,我們頁面的路由跳轉也成功了~

改造完成后,可以看到,頁面顯示的還是一樣,接下來我們先這樣放著,因為我們后面會進行數(shù)據(jù)模擬,和模擬服務器請求。

我們就這樣寫好第一個單一組件,并且數(shù)據(jù)是從JS中讀取的。

2.模擬數(shù)據(jù)

這時候為了方便后面數(shù)據(jù)渲染,我們這里需要模擬一些本地數(shù)據(jù),我們創(chuàng)建一個本地mock-books.ts文件來存放模擬的數(shù)據(jù):

// app/mock-books.ts
import { Books } from './books';
export const BookList: Books[] = [
    {
        id: 1, 
        url: 'https://img3.doubanio.com/view/subject/m/public/s29988481.jpg',
        title: '像火焰像灰燼',
        author: '程姬',
    },
    // 省略其他9條
]

然后在index.component.ts中導入模擬的數(shù)據(jù),并將原有的books值修改成導入的模擬數(shù)據(jù)BookList

// index.component.ts
import { BookList } from '../mock-books';
books = BookList;

并將原本的*ngFor中修改成這樣,綁定真正的數(shù)據(jù):

<!-- index.component.html -->
<div class="books_item" *ngFor="let item of books">
  <img class="cover" src="{{item.url}}" alt="{{item.id}}">
  <div class="title">
    <a>{{item.title}}</a>
  </div>
  <div class="author">{{item.author}}</div>
</div>

3.編寫主從組件

當我們寫完一個單一組件后,我們會發(fā)現(xiàn),如果我們把每個組件都寫到同一個HTML文件中,這是很糟糕的事情,這樣做有缺點:

  • 代碼復用性差;(導致每次相同功能要重新寫)
  • 代碼難維護;(因為一個文件會非常長)
  • 影響性能;(打開每個頁面都要重復加載很多)

為了解決這個問題,我們這里就要開始使用真正的組件化思維,將通用常用組件抽離出來,通過參數(shù)傳遞來控制組件的不同業(yè)務形態(tài)。
這便是我們接下來要寫的主從組件。

思考一下,我們這里現(xiàn)在能抽成組件作為公共代碼的,就是這個單個書本的內(nèi)容,因為每個書本的內(nèi)容都一致,只是里面數(shù)據(jù)的差異,于是我們再新建一個組件:

ng g component books

并將前面index.component.html中關于課本的代碼剪切到books.component.html中來,然后刪除掉*ngFor的內(nèi)容,并將原本本地的變量books替換成list,這個變量我們等會會取到:

<!-- books.component.html -->
<div class="books_item">
  <img class="cover" src="{{list.url}}" alt="{{list.id}}" (click)="getDetailImage(list)">
  <div class="title">
    <a routerLink="/detail/{{list.id}}">{{list.title}}</a>
  </div>
  <div class="author">{{list.author}}</div>
</div>

再將這個組件,引用到它的父組件中,這里是要引用到index.component.html的組件中,并將前面的*ngFor再次傳入<app-books>

<div class="content">
  <div class="books_box">
    <app-books *ngFor="let item of books"></app-books>
  </div>
</div>

接下來要做的就是獲取到list變量的值,顯然這個值是要從外面組件傳進來的,我們需要在books.component.ts引入前面定義的 Books類 和 @Input() 裝飾器,還要添加一個帶有 @Input() 裝飾器list 屬性,另外還要記得將getDetailImage方法也剪切過來:

// books.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Books } from '../books';

export class BooksComponent implements OnInit {
  @Input() list: Books;
  constructor() { }
  ngOnInit() {}
  getDetailImage(books){
    alert(`正在查看id為${books.id}的大圖!`);
  }
}

@Input() 裝飾器介紹具體可以查看 手冊

我們要獲取的 list 屬性必須是一個帶有@Input()裝飾器的輸入屬性,因為外部的 IndexComponent 組件將會綁定到它。就像這樣:

<app-books *ngFor="let list of books" [list]="item"></app-books>

知識點
[list]="item"Angular屬性綁定語法。這是一種單向數(shù)據(jù)綁定。從 IndexComponentitem 屬性綁定到目標元素的 list 屬性,并映射到了 BooksComponentlist 屬性。

做到這里,我們已經(jīng)將BooksComponent作為IndexComponent的子組件來引用了,在實際開發(fā)過程中,這樣的父子組件關系,會用的非常多。

寫到這里,看看我們項目,還是一樣正常在運行,只是現(xiàn)在項目中組件分工更加明確了。

現(xiàn)在的效果圖:

圖片3-2

本部分內(nèi)容到這結束

Author 王平安
E-mail pingan8787@qq.com
博 客 www.pingan8787.com
微 信 pingan8787
每日文章推薦 https://github.com/pingan8787/Leo_Reading/issues
JS小冊 js.pingan8787.com
微信公眾號 前端自習課
前端自習課
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容