路由(Router)簡單使用

模擬需求

現(xiàn)在超市里面有2部分信息要展示许布,1員工信息 2.產(chǎn)品信息球及。其中產(chǎn)品信息有2個(gè)分類,并且要展示2個(gè)分類的具體信息

關(guān)鍵點(diǎn)

Router
RouterModule.forRoot
RouterModule.forChild

注意:forChild的時(shí)候注冊Component需要在對應(yīng)Module中創(chuàng)建

步驟

1. 創(chuàng)建一個(gè)Index

創(chuàng)建一個(gè)index來告訴用戶我們是有2個(gè)大分類的 (員工,產(chǎn)品)

index.component.ts

import { OnInit, Component } from '@angular/core';


@Component({
    templateUrl: './index.component.html'
})

export class IndexComponent implements OnInit {
    ngOnInit(): void {

    }
}

index.component.html

<!-- 簡單的創(chuàng)建2個(gè)button來表示一下即可 -->
<!-- routerLink 是路由在html中的使用 -->
<!--The content below is only a placeholder and can be replaced.-->
<button routerLink="/employee" routerLinkActive="active">職位</button>
<button routerLink="/product" routerLinkActive="active">產(chǎn)品</button>

2. 創(chuàng)建 員工和產(chǎn)品

創(chuàng)建員工信息

1. employee.ts

// 員工信息的Modle
export class Employee {
    id: number;
    name: String;
    post: String;
    postId: number;
}

2. employee-factory.ts

// 仿造工廠類 - 產(chǎn)生員工信息
import { Employee } from './employee';

export const DEFAULT_EMPLOYEE: Employee[] = [
    { id: 1, name: '張三', post: '售貨員', postId: 1 },
    { id: 2, name: '李四', post: '收銀員', postId: 2 }
];

3. employee.component.ts

// 邏輯信息展示
import { DEFAULT_EMPLOYEE } from './employee-factory';
import { Router } from '@angular/router';
import { OnInit, Component } from '@angular/core';
import { Employee } from './employee';


@Component({
    // 這個(gè)地方個(gè)人也不是很理解 自定義出來的其他怎么處理
    selector: 'app-root',
    templateUrl: './employee.component.html',
})


export class EmployeeComponent implements OnInit {
    private employees: Employee[];

    constructor(private router: Router) { }
    // 生命周期 在初始化的時(shí)候把 employees 初始化
    ngOnInit(): void {
        this.employees = DEFAULT_EMPLOYEE;
    }

}

4. employee.component.html

<!-- UI 一個(gè)簡單的For循環(huán)展示 -->
<li *ngFor="let employee of employees">
  {{employee.post}} {{employee.name}}
</li>

創(chuàng)建產(chǎn)品

product.ts

// 產(chǎn)品信息的定義 定義一個(gè)id和name和type分類
export class Product {
  id: number;
  name: string;
  type: number;
}

product-factory.ts

// 工廠類來創(chuàng)建當(dāng)前產(chǎn)品的分類

import {Product} from './product';

export const DEFAULT_PRODUCTS: Product[] = [
  {id: 1, name: '水果', type: 1},
  {id: 2, name: '蔬菜', type: 2}
];

product.component.ts

import { Component, OnInit } from '@angular/core';
import { Product } from './product';
import { DEFAULT_PRODUCTS } from './product-factory';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './product.component.html',
})

export class ProductComponent implements OnInit {
  private mProductList: Product[];

  constructor(private router: Router) {
  }

  ngOnInit(): void {
    this.mProductList = DEFAULT_PRODUCTS;
  }

  // 查看具體的信息
  viewInfo(id: number): void {
    if (id === 1) {
      this.router.navigate(['/product/fruit']);
    } else if (id === 2) {
      this.router.navigate(['/product/vegetables']);
    }
  }

}

product.component.html

<!-- For 循環(huán)來展示一下我們當(dāng)前擁有的 產(chǎn)品 -->
<!-- click點(diǎn)擊來顯示詳情 -->
<button *ngFor="let product of mProductList" (click)="viewInfo(product.id)">
  {{product.id}} {{product.name}}
</button>
<br />

<!-- 子路由顯示區(qū)域, 我們用來顯示 產(chǎn)品信息的 -->
<router-outlet></router-outlet>

fruit.component.ts

# 簡單的顯示一下水果的信息
# 這個(gè)是子路由顯示

import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';

@Component({
    template: '<a> 水果的信息 </a>'
})

export class FruitComponent implements OnInit {


    constructor(private router: Router) {
    }

    ngOnInit(): void {
    }
}

vegetables.component.ts

# 來展示蔬菜的具體信息

import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';

@Component({
    template: '<a> 蔬菜的信息 </a>',
})

export class VegetablesComponent implements OnInit {


    constructor(private router: Router) {
    }

    ngOnInit(): void {
    }
}

配置路由

方法1

import { ProductComponent } from './product/product.component';
import { IndexComponent } from './index/index.component';
import { EmployeeComponent } from './employee/employee.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
    { path: '', redirectTo: '/index', pathMatch: 'full' },
    { path: 'index', component: IndexComponent },
    {
        path: 'product',
        component: ProductComponent,
        children: [
            { path: 'fruit', component: FruitComponent },
            { path: 'vegetables', component: VegetablesComponent },
        ]
    },

    { path: 'employee', component: EmployeeComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

export class AppRoutingModule { }

# 注意
所有的component 需要在app.module 中注冊一下

方法2

# 創(chuàng)建一個(gè) product.module 來配置子路由
import { VegetablesComponent } from './vegetables/vegetables.component';
import { FruitComponent } from './fruit/fruit.component';
import { ProductComponent } from './product.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';

export const ROUTES: Routes = [
  {
    # 這個(gè)地方 path要為空莽鸭,因?yàn)檫@個(gè)是需要上層傳過來的
    path: '',
    children: [
      { path: 'fruit', component: FruitComponent },
      { path: 'vegetables', component: VegetablesComponent },
    ]
  }
];

@NgModule({

  declarations: [
    FruitComponent,
    VegetablesComponent,
  ],

  imports: [
    CommonModule,
    RouterModule.forChild(ROUTES)
  ],
})

export class ProductModule {
}

# 注意:
# FruitComponent 和  VegetablesComponent 要在這里注冊,不能在app.modules中注冊
# app-routing.module.ts 修改為 loadChildren

import { ProductComponent } from './product/product.component';
import { IndexComponent } from './index/index.component';
import { EmployeeComponent } from './employee/employee.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
    { path: '', redirectTo: '/index', pathMatch: 'full' },
    { path: 'index', component: IndexComponent },
    {
        path: 'product',
        component: ProductComponent,
        loadChildren: './product/product.module#ProductModule'
    },

    { path: 'employee', component: EmployeeComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

export class AppRoutingModule { }

疑問

1. 為什么路由要這么聲明

constructor(private router: Router) {
}

2. selector 怎么修改和配置和使用

@Component({
  selector: 'app-root',
  templateUrl: './product.component.html',
})

源碼地址

https://github.com/i-show/angular-Router

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吃靠,一起剝皮案震驚了整個(gè)濱河市硫眨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌巢块,老刑警劉巖礁阁,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異族奢,居然都是意外死亡氮兵,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門歹鱼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人卜高,你說我怎么就攤上這事弥姻。” “怎么了掺涛?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵庭敦,是天一觀的道長。 經(jīng)常有香客問我薪缆,道長秧廉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任拣帽,我火速辦了婚禮疼电,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘减拭。我一直安慰自己蔽豺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布拧粪。 她就那樣靜靜地躺著修陡,像睡著了一般沧侥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上魄鸦,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天宴杀,我揣著相機(jī)與錄音,去河邊找鬼拾因。 笑死旺罢,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盾致。 我是一名探鬼主播主经,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼庭惜!你這毒婦竟也來了罩驻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤护赊,失蹤者是張志新(化名)和其女友劉穎惠遏,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體骏啰,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡节吮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了判耕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片透绩。...
    茶點(diǎn)故事閱讀 40,503評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖壁熄,靈堂內(nèi)的尸體忽然破棺而出帚豪,到底是詐尸還是另有隱情,我是刑警寧澤草丧,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布狸臣,位于F島的核電站,受9級特大地震影響昌执,放射性物質(zhì)發(fā)生泄漏烛亦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一懂拾、第九天 我趴在偏房一處隱蔽的房頂上張望煤禽。 院中可真熱鬧,春花似錦岖赋、人聲如沸呜师。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汁汗。三九已至衷畦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間知牌,已是汗流浹背祈争。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留角寸,地道東北人菩混。 一個(gè)月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像扁藕,于是被迫代替她去往敵國和親沮峡。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評論 2 359

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