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

本文目錄

  • 一、項目起步
  • 二、編寫路由組件
  • 三、編寫頁面組件

    • 1.編寫單一組件
    • 2.模擬數據
    • 3.編寫主從組件
  • 四、編寫服務

    • 1.爲什麼需要服務
    • 2.編寫服務
  • 五、引入RxJS

    • 1.關於RxJS
    • 2.引入RxJS
    • 3.改造數據獲取方式
  • 六、改造組件

    • 1.添加歷史記錄組件
    • 2.添加和刪除歷史記錄
  • 七、HTTP改造

    • 1.引入HTTP
    • 2.通過HTTP請求數據
    • 3.通過HTTP修改數據
    • 4.通過HTTP增加數據
    • 5.通過HTTP刪除數據
    • 6.通過HTTP查找數據

本項目源碼放在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。 它會爲列表中的每項數據複寫它的宿主元素。
這時候可以看到頁面變成下面這個樣子:
圖片3-1

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

現在先新建一個books.ts文件來定義一個Book類,並添加idurltitleauthor四個屬性:

// 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>

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

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

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

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

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

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

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

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

改造完成後,可以看到,頁面顯示的還是一樣,接下來我們先這樣放着,因爲我們後面會進行數據模擬,和模擬服務器請求。

我們就這樣寫好第一個單一組件,並且數據是從JS中讀取的。

2.模擬數據

這時候爲了方便後面數據渲染,我們這裏需要模擬一些本地數據,我們創建一個本地 mock-books.ts文件來存放模擬的數據:

// 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中導入模擬的數據,並將原有的books值修改成導入的模擬數據BookList

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

並將原本的*ngFor中修改成這樣,綁定真正的數據:

<!-- 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.編寫主從組件

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

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

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

思考一下,我們這裏現在能抽成組件作爲公共代碼的,就是這個單個書本的內容,因爲每個書本的內容都一致,只是裏面數據的差異,於是我們再新建一個組件:

ng g component books

並將前面index.component.html中關於課本的代碼剪切到books.component.html中來,然後刪除掉*ngFor的內容,並將原本本地的變量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屬性綁定語法。這是一種單向數據綁定。從 IndexComponentitem 屬性綁定到目標元素的 list 屬性,並映射到了 BooksComponentlist 屬性。

做到這裏,我們已經將BooksComponent作爲IndexComponent的子組件來引用了,在實際開發過程中,這樣的父子組件關係,會用的非常多。

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

現在的效果圖:
圖片3-2

本部分內容到這結束

Author 王平安
E-mail [email protected]
博 客 www.pingan8787.com
微 信 pingan8787
每日文章推薦 https://github.com/pingan8787...
JS小冊 js.pingan8787.com
微信公衆號 前端自習課

前端自習課

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章