最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular 中的路由詳解

 更新時間:2023年11月08日 17:13:31   作者:@Autowire  
路由是實現(xiàn)單頁面應(yīng)用的一種方式,通過監(jiān)聽hash或者history的變化,渲染不同的組件,起到局部更新的作用,避免每次URL變化都向服務(wù)器請求數(shù)據(jù),本文給大家介紹Angular 中的路由,感興趣的朋友一起看看吧

1 使用 routerLink 指令 路由跳轉(zhuǎn)

命令創(chuà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 顯示動態(tài)加載的路由:

<h1>
  <a routerLink="/home" routerLinkActive="active">首頁</a>
  <a routerLink="/news" routerLinkActive="active">新聞</a>
</h1>
<router-outlet></router-outlet>

routerLink 跳轉(zhuǎn)頁面默認路由:

//匹配不到路由的時候加載的組件 或者跳轉(zhuǎn)的路由
{path: '**', redirectTo: 'home'}

routerLinkActive: 設(shè)置 routerLink 默認選中路由

<h1>
  <a routerLink="/home" routerLinkActive="active">
    首頁
  </a>
  <a routerLink="/news" routerLinkActive="active">
    新聞
  </a>
</h1>
.active {
  color: green;
}
<h1>
    <a [routerLink]="[ '/home' ]" routerLinkActive="active">首頁</a>
    <a [routerLink]="[ '/news' ]" routerLinkActive="active">新聞</a>
</h1>

2 使用方法跳轉(zhuǎn)路由 - 使用 router.navigate 方法

在組件中注入 Router 服務(wù),并使用 navigate 方法進行路由跳轉(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')">首頁</button>
  <button (click)="goToPage('news')">新聞</button>
</h1>
<router-outlet></router-outlet>

3 routerLink跳轉(zhuǎn)頁面?zhèn)髦?- GET傳值的方式

頁面跳轉(zhuǎn) - queryParams屬性是固定的:

<h1>
  <a routerLink="/home" routerLinkActive="active" [queryParams]="{name: 'index'}">首頁</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)頁面?zhèn)髦?- GET傳值的方式

<h1>
    <button (click)="goToPage('home', 'home')">首頁</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 動態(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 {
}

頁面設(shè)置參數(shù):

<h1>
  <a [routerLink]="['/home', '1000']" routerLinkActive="active">首頁</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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • AngularJS動態(tài)生成select下拉框的方法實例

    AngularJS動態(tài)生成select下拉框的方法實例

    這篇文章主要給大家介紹了關(guān)于AngularJS動態(tài)生成select下拉框的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用AngularJS具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • AngularJS中的$parse服務(wù)與$eval服務(wù)用法實例

    AngularJS中的$parse服務(wù)與$eval服務(wù)用法實例

    這篇文章主要介紹了AngularJS中的$parse服務(wù)與$eval服務(wù)用法,結(jié)合實例形式分析了AngularJS中$parse服務(wù)與$eval服務(wù)的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2023-05-05
  • AngularJS API之copy深拷貝詳解及實例

    AngularJS API之copy深拷貝詳解及實例

    這篇文章主要介紹了AngularJS API之copy深拷貝詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • 利用Angular7開發(fā)一個Radio組件的全過程

    利用Angular7開發(fā)一個Radio組件的全過程

    這篇文章主要給大家介紹了關(guān)于如何利用Angular7開發(fā)一個Radio組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Angular7具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • AngularJs中 ng-repeat指令中實現(xiàn)含有自定義指令的動態(tài)html的方法

    AngularJs中 ng-repeat指令中實現(xiàn)含有自定義指令的動態(tài)html的方法

    今天用angular寫table的時候,遇到了一個問題。在ng-repeat中,含有動態(tài)的html,而這些html中含有自定義指令,怎么實現(xiàn)呢?下面小編給大家分享AngularJs中 ng-repeat指令中實現(xiàn)含有自定義指令的動態(tài)html的方法,一起看看吧
    2017-01-01
  • AngularJS深入探討scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期

    AngularJS深入探討scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期

    這篇文章主要介紹了AngularJS的scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期,較為詳細的分析了scope的作用域、層次結(jié)構(gòu)、繼承及生命周期相關(guān)概念與使用技巧,需要的朋友可以參考下
    2016-11-11
  • AngularJS實現(xiàn)的自定義過濾器簡單示例

    AngularJS實現(xiàn)的自定義過濾器簡單示例

    這篇文章主要介紹了AngularJS實現(xiàn)的自定義過濾器,結(jié)合實例形式分析了AngularJS自定義過濾器的簡單定義與使用操作技巧,需要的朋友可以參考下
    2019-02-02
  • Angularjs中的ui-bootstrap的使用教程

    Angularjs中的ui-bootstrap的使用教程

    這篇文章主要介紹了Angularjs中的ui-bootstrap的使用教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • 詳細AngularJs4的圖片剪裁組件的實例

    詳細AngularJs4的圖片剪裁組件的實例

    本篇文章主要介紹了詳細AngularJs4的圖片剪裁組件的實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • angular寫一個列表的選擇全選交互組件的示例

    angular寫一個列表的選擇全選交互組件的示例

    本篇文章主要介紹了angular寫一個列表的選擇全選交互組件的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論

横峰县| 九江市| 南投市| 陇川县| 平山县| 马边| 阿巴嘎旗| 高唐县| 合肥市| 财经| 德化县| 深圳市| 开封市| 海林市| 外汇| 黔西县| 巧家县| 太谷县| 澄江县| 成都市| 临武县| 汉沽区| 时尚| 景宁| 舞钢市| 屏东县| 曲阜市| 北安市| 双峰县| 内乡县| 曲松县| 平度市| 青海省| 江油市| 汾西县| 梁山县| 什邡市| 新平| 宜春市| 宁蒗| 沭阳县|