国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

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

2019-11-02 14:09:52
字體:
供稿:網(wǎng)友

序言:

Angular APP 視圖之間的跳轉(zhuǎn),依賴于 Router (路由),這一章,我們來講述 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

ng g c home ng g c about ng g c dashboard

路由與配置

(1)**引入 Angular Router **

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

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

(2) 路由配置

還記得由誰(shuí)來管理component 的吧,沒錯(cuò),由 module 來管理。 所以,把新創(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 }];

說明: Angular 2.X 以上版本,開始使用 TypeScript 編寫代碼,而不再是 JavaScript,所以,文件的后綴是: ts 而不是 js

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

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

聲明 Router Outlet

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

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连云港市| 武山县| 漯河市| 佛学| 曲靖市| 汶川县| 洛浦县| 龙门县| 南充市| 镇赉县| 区。| 嘉义市| 吉林市| 梅州市| 北碚区| 平遥县| 阿鲁科尔沁旗| 环江| 通河县| 新余市| 齐河县| 望江县| 特克斯县| 宽甸| 潢川县| 邓州市| 巢湖市| 新密市| 常德市| 贵州省| 定边县| 奉贤区| 佛冈县| 民丰县| 微博| 石城县| 鹤庆县| 慈溪市| 德钦县| 藁城市| 高唐县|