Angular路由

在項(xiàng)目創(chuàng)立的最初期,我們應(yīng)該想到任何項(xiàng)目都會(huì)有路由跳轉(zhuǎn)。所以在創(chuàng)建新項(xiàng)目的時(shí)候我們就可以把router一并安裝好,
創(chuàng)建新項(xiàng)目并安裝路由命令:

ng new my-app --routing

這樣安裝的項(xiàng)目會(huì)自動(dòng)配置好路由,無(wú)需下面的操作?。?!

—————————————————————————————————————

如果在最開(kāi)始直接是使用ng new my-app新建的項(xiàng)目,可以在后期安裝router:

npm install @angular/router -s

那么我們需要在app文件夾下新建一個(gè)app-routing.module.ts文件:
app-routing.module.ts

import { NgModule } from '@angular/core';
import {Routes,RouterModule} from "@angular/router";
import {HomeComponent} from "./home/home.component"
import {code404} from "./code404/code404.component"

const routes: Routes = [
    {path:'',component:HomeComponent}//配置路由
    {path:'**',component:code404}//配置404頁(yè)面
];

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

然后在app.module.ts中添加:

import {AppRoutingModule} from './app-routing.module'
@NgModule({
  declarations: [// 源數(shù)據(jù),只能聲明組件、指令和管道
    AppComponent , HomeComponent
  ],
  imports: [
    BrowserModule,//開(kāi)發(fā)web必備模塊
    AppRoutingModule
  ],
  providers: [{ 
      provide: [], 
      useValue: []
    }],//只能聲明服務(wù)
  bootstrap: [AppComponent]
})
export class AppModule { }

最后在app.component.html中:

  <router-outlet></router-outlet>

在html模板中使用a鏈接進(jìn)行頁(yè)面跳轉(zhuǎn):

<a [routerLink]="['/']">首頁(yè)</a>

模板中頁(yè)面跳轉(zhuǎn)如上,在事件中用js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)如下:

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

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.css']
})
export class FooterComponent implements OnInit {

  constructor(private router: Router) { }//這樣可以拿到router對(duì)象
//事件方法
toclick(){
    this.router.navigate(["/"])
}
  ngOnInit() {
  }

}

—————————————————————————————————————

路由傳參:

第一種方法:
在查詢參數(shù)中傳遞數(shù)據(jù)

地址欄顯示這樣!


image.png

html中:

<a [routerLink]="['/test']" [queryParams]="{id:1}">首頁(yè)</a>

test組件中獲取地址欄參數(shù):

 import { Component, OnInit } from '@angular/core';   
@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.css']
})
export class FooterComponent implements OnInit {
  private testId:number;//聲明一個(gè)數(shù)據(jù)用來(lái)存放獲取到的路由參數(shù)
  constructor(private routeInfo: ActivatedRoute) { }//這樣可以獲取到參數(shù)
  ngOnInit() {
    this.testId = this.routeInfo.snapshot.queryParams["id"];
  }

}

.................................................................................................

第二種方法:
在路由路徑中傳遞參數(shù)

地址欄顯示這樣!


image.png

相比上一種方法,這種方法需要在路由配置中設(shè)置路由可以添加參數(shù):

//在app-routing.module.ts中
{path:'test/:id',component:PageOneComponent},

html中:

<a [routerLink]="['/test',1]">首頁(yè)</a>

test組件中獲取地址欄參數(shù):

 import { Component, OnInit } from '@angular/core';   
@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.css']
})
export class FooterComponent implements OnInit {
  private testId:number;//聲明一個(gè)數(shù)據(jù)用來(lái)存放獲取到的路由參數(shù)
  constructor(private routeInfo: ActivatedRoute) { }//這樣可以獲取到參數(shù)
  ngOnInit() {
    this.testId = this.routeInfo.snapshot.params["id"];
  }

}

特別注意:當(dāng)路由是從一個(gè)路由到另外一個(gè)路由跳轉(zhuǎn)傳遞參數(shù)時(shí),使用

this.testId = this.routeInfo.snapshot.params["id"];

在同一個(gè)路由下面只改變參數(shù)就要參數(shù)訂閱,則不使用上面的代碼,而是使用如下代碼:

this.routeInfo.params.subscribe((params:Params) => this.testId = params["id"];)

—————————————————————————————————————

重定向路由:

//在app-routing.module.ts中
const routes: Routes = [
    {path:'',redirectTo:'/home',pathMatch:'full'},//重定向路由
    {path:'home',component:HomeComponent},
];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第二節(jié):路由介紹 1.生成新項(xiàng)目 ng new 項(xiàng)目名 --routing : 注意 --routing的作...
    咖啡浮點(diǎn)閱讀 1,175評(píng)論 0 2
  • 一:路由基礎(chǔ) 什么是路由: 在web開(kāi)發(fā)中,路由的概念由來(lái)已久,簡(jiǎn)而言之,就是利用URL的唯一性來(lái)指定特定的事物,...
    真的稻城閱讀 6,068評(píng)論 2 7
  • angular路由 北京小課堂 分享人:吳昊杰 目錄 1.背景介紹 2.知識(shí)剖析 3.常見(jiàn)問(wèn)題 4.解決方案 5....
    吳杰看世界閱讀 720評(píng)論 0 1
  • 一、SPA 單頁(yè)Web應(yīng)用(single page web application,SPA),就是只有一張Web頁(yè)...
    笨蛋小明閱讀 933評(píng)論 0 3
  • 知否,知否,應(yīng)是綠肥紅瘦 作者:形容瘦 一腳踩進(jìn)泥里一手伸進(jìn)云端,...
    KFCuncle閱讀 766評(píng)論 0 0

友情鏈接更多精彩內(nèi)容