欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular 5.x 學(xué)習(xí)筆記之Router(路由)應(yīng)用

 更新時(shí)間:2018年04月08日 11:04:52   作者:全棧工程師的早讀課  
本篇文章主要介紹了Angular 5.x 學(xué)習(xí)筆記之Router(路由)應(yīng)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

序言:

Angular APP 視圖之間的跳轉(zhuǎn),依賴(lài)于 Router (路由),這一章,我們來(lái)講述 Router 的應(yīng)用

實(shí)例講解

運(yùn)行結(jié)果如下。 設(shè)置了3個(gè)導(dǎo)航欄, Home、 About、Dashboard。 點(diǎn)擊不同的導(dǎo)航欄,跳轉(zhuǎn)到相應(yīng)的頁(yè)面:


創(chuàng)建3個(gè) component

  1. ng g c home
  2. ng g c about
  3. ng g c dashboard

路由與配置

(1)**引入 Angular Router **

當(dāng)用到 Angular Router 時(shí),需要引入 RouterModule,如下:

// app.module.ts
import { RouterModule } from '@angular/router';
imports: [
 BrowserModule, RouterModule
],

(2) 路由配置

還記得由誰(shuí)來(lái)管理component 的吧,沒(méi)錯(cuò),由 module 來(lái)管理。 所以,把新創(chuàng)建的 component,引入到 app.moudle 中。 如下:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { appRoutes } from './routerConfig';

import { AppComponent } from './app.component';
import { AboutComponent } from './components/about/about.component';
import { HomeComponent } from './components/home/home.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

提示: 注意component的路徑,為便于管理,我們把新創(chuàng)建的component 移到了 components 文件夾中。

創(chuàng)建 Router Configure 文件

在 app 目錄下, 創(chuàng)建 routerConfig.ts 文件。 代碼如下:

import { Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
import { AboutComponent } from './components/about/about.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

export const appRoutes: Routes = [
 { path: 'home', 
 component: HomeComponent 
 },
 {
 path: 'about',
 component: AboutComponent
 },
 { path: 'dashboard',
 component: DashboardComponent
 }
];

說(shuō)明: Angular 2.X 以上版本,開(kāi)始使用 TypeScript 編寫(xiě)代碼,而不再是 JavaScript,所以,文件的后綴是: ts 而不是 js

這個(gè) routerConfigue 文件,怎么調(diào)用呢? 需要把它加載到 app.module.ts 中,這是因?yàn)?app.moudle.ts 是整個(gè)Angular App 的入口。

// app.module.ts
import { appRoutes } from './routerConfig';
imports: [
 BrowserModule,
 RouterModule.forRoot(appRoutes)
],

聲明 Router Outlet

在 app.component.html 文件中,添加代碼:

<div style="text-align:center">
 <h1>
  {{title}}!!
 </h1>
 <nav>
  <a routerLink="home" routerLinkActive="active">Home</a>
  <a routerLink="about">About</a>
  <a routerLink="dashboard">Dashboard</a>
 </nav>
 <router-outlet></router-outlet>
 </div>

運(yùn)行

進(jìn)入到該工程所在的路徑, 運(yùn)行;

ng serve --open

當(dāng) webpack 編譯成功后,在瀏覽器地址欄中,輸入: http://localhost:4200

即可看到本篇開(kāi)始的結(jié)果。

關(guān)于Router,換一種寫(xiě)法:

在 app.moudle.ts 文件中,代碼如下 :

 imports: [
  BrowserModule,
  RouterModule.forRoot(
  [
   { path: 'home', 
   component: HomeComponent 
   },
   {
   path: 'about',
   component: AboutComponent
   },
   {
   path: 'dashboard',
   component: DashboardComponent
   }
  ]
  )
 ],

這樣一來(lái),可以不用單獨(dú)創(chuàng)建 routerConfigure.ts 文件。

小結(jié)

自從引入了面向組件(component)后,路由管理相比 AngularJS (1.X),方便了很多。

進(jìn)一步優(yōu)化:

或許你已經(jīng)注意到,當(dāng)訪(fǎng)問(wèn) http://localhost:4200 時(shí),它的路徑應(yīng)該是 “/”, 我們應(yīng)該設(shè)置這個(gè)默認(rèn)的路徑。

{
   path: '',
   redirectTo:'/home',
   pathMatch: 'full'
   },

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論