Angular 中的路由詳解
1 使用 routerLink 指令 路由跳轉(zhuǎn)
命令創(chuàng)建項(xiàng)目:
ng new ng-demo
創(chuàng)建需要的組件:
ng g component components/home ng g component components/news ng g component components/produect
找到 app-routing.module.ts 配置路由:
引入組件:
import { HomeComponent } from './components/home/home.component';
import { NewsComponent } from './components/news/news.component';
import { ProductComponent } from './components/product/product.component';配置路由:
const routes: Routes = [
{path: 'home', component: HomeComponent},
{path: 'news', component: NewsComponent},
{path: 'product', component: ProductComponent},
{path: '**', redirectTo: 'home'}
];找到 app.component.html 根組件模板,配置 router-outlet 顯示動(dòng)態(tài)加載的路由:
<h1> <a routerLink="/home" routerLinkActive="active">首頁(yè)</a> <a routerLink="/news" routerLinkActive="active">新聞</a> </h1> <router-outlet></router-outlet>
routerLink 跳轉(zhuǎn)頁(yè)面默認(rèn)路由:
//匹配不到路由的時(shí)候加載的組件 或者跳轉(zhuǎn)的路由
{path: '**', redirectTo: 'home'}routerLinkActive: 設(shè)置 routerLink 默認(rèn)選中路由
<h1>
<a routerLink="/home" routerLinkActive="active">
首頁(yè)
</a>
<a routerLink="/news" routerLinkActive="active">
新聞
</a>
</h1>
.active {
color: green;
}<h1>
<a [routerLink]="[ '/home' ]" routerLinkActive="active">首頁(yè)</a>
<a [routerLink]="[ '/news' ]" routerLinkActive="active">新聞</a>
</h1>2 使用方法跳轉(zhuǎn)路由 - 使用 router.navigate 方法
在組件中注入 Router 服務(wù),并使用 navigate 方法進(jìn)行路由跳轉(zhuǎn):
import { Component } from '@angular/core';
import { Router} from "@angular/router";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'routerProject';
constructor(public router: Router) {
}
goToPage(path: string) {
this.router.navigate([path]).then(r => {})
}
}<h1>
<button (click)="goToPage('home')">首頁(yè)</button>
<button (click)="goToPage('news')">新聞</button>
</h1>
<router-outlet></router-outlet>3 routerLink跳轉(zhuǎn)頁(yè)面?zhèn)髦?- GET傳值的方式
頁(yè)面跳轉(zhuǎn) - queryParams屬性是固定的:
<h1>
<a routerLink="/home" routerLinkActive="active" [queryParams]="{name: 'index'}">首頁(yè)</a>
<a routerLink="/news" routerLinkActive="active" [queryParams]="{name: 'news'}">新聞</a>
</h1>
<router-outlet></router-outlet>獲取參數(shù)方式:
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit{
constructor(public activatedRoute: ActivatedRoute) {
}
ngOnInit(): void {
this.activatedRoute.queryParams.subscribe(data => {
console.log(data)
})
}
}4 使用方法跳轉(zhuǎn)頁(yè)面?zhèn)髦?- GET傳值的方式
<h1>
<button (click)="goToPage('home', 'home')">首頁(yè)</button>
<button (click)="goToPage('news', 'news')">新聞</button>
</h1>
<router-outlet></router-outlet>
import { Component } from '@angular/core';
import { Router} from "@angular/router";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'routerProject';
constructor(public router: Router) {
}
goToPage(path: string, param: string) {
this.router.navigate([path], {
queryParams: {
name: param
}
}).then(r => {})
}
}5 動(dòng)態(tài)路由的方式-路由跳轉(zhuǎn)
配置路由文件:
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {HomeComponent} from "./components/home/home.component";
import {NewsComponent} from "./components/news/news.component";
import {ProductComponent} from "./components/product/product.component";
const routes: Routes = [
{path: 'home/:id', component: HomeComponent},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}頁(yè)面設(shè)置參數(shù):
<h1> <a [routerLink]="['/home', '1000']" routerLinkActive="active">首頁(yè)</a> </h1> <router-outlet></router-outlet>
參數(shù)接受:
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit{
constructor(public activatedRoute: ActivatedRoute) {
}
ngOnInit(): void {
this.activatedRoute.params.subscribe(data => {
console.log(data)
})
}
}6 父子路由
創(chuàng)建組件引入組件
import {HomeComponent} from "./components/home/home.component";
import {NewsComponent} from "./components/news/news.component";配置路由
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {HomeComponent} from "./components/home/home.component";
import {NewsComponent} from "./components/news/news.component";
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
children: [
{
path: 'news',
component: NewsComponent
},
{path: '**', redirectTo: 'home'}
]
},
{path: '**', redirectTo: 'home'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}父組件中定義router-outlet
<router-outlet></router-outlet>
到此這篇關(guān)于Angular 中的路由的文章就介紹到這了,更多相關(guān)Angular 中的路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
angular.js實(shí)現(xiàn)購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了angular.js購(gòu)物車功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
初學(xué)者AngularJS的環(huán)境搭建過(guò)程
這篇文章主要介紹了初學(xué)者AngularJS的環(huán)境搭建過(guò)程,在文章給大家提到了Angular-cli的特性,大家一起看看吧2017-10-10
angularjs $http調(diào)用接口的方式詳解
今天小編就為大家分享一篇angularjs $http調(diào)用接口的方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
angularjs使用div模擬textarea文本框的方法
今天小編就為大家分享一篇angularjs使用div模擬textarea文本框的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式
數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來(lái)操縱網(wǎng)站上存在的元素。2021-05-05

