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

Angular 中的路由詳解

 更新時間:2023年11月08日 17:13:31   作者:@Autowire  
路由是實現(xiàn)單頁面應用的一種方式,通過監(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中的$state.go

    老生常談angularjs中的$state.go

    下面小編就為大家?guī)硪黄仙U刟ngularjs中的$state.go。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • AngularJS入門教程之Helloworld示例

    AngularJS入門教程之Helloworld示例

    這篇文章主要介紹了AngularJS入門教程之Helloworld示例,結(jié)合Helloworld入門示例分析了AngularJS的功能、原理、MVC框架、數(shù)據(jù)綁定與相關(guān)使用技巧,需要的朋友可以參考下
    2016-12-12
  • angular.js實現(xiàn)購物車功能

    angular.js實現(xiàn)購物車功能

    這篇文章主要為大家詳細介紹了angular.js購物車功能的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 初學者AngularJS的環(huán)境搭建過程

    初學者AngularJS的環(huán)境搭建過程

    這篇文章主要介紹了初學者AngularJS的環(huán)境搭建過程,在文章給大家提到了Angular-cli的特性,大家一起看看吧
    2017-10-10
  • angularjs自定義過濾器demo示例

    angularjs自定義過濾器demo示例

    這篇文章主要介紹了angularjs自定義過濾器,結(jié)合完整實例形式分析了angularjs自定義過濾器相關(guān)原理、使用方法及操作注意事項,需要的朋友可以參考下
    2019-08-08
  • angularjs $http調(diào)用接口的方式詳解

    angularjs $http調(diào)用接口的方式詳解

    今天小編就為大家分享一篇angularjs $http調(diào)用接口的方式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • angularjs使用div模擬textarea文本框的方法

    angularjs使用div模擬textarea文本框的方法

    今天小編就為大家分享一篇angularjs使用div模擬textarea文本框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 詳解Angular數(shù)據(jù)綁定及其實現(xiàn)方式

    詳解Angular數(shù)據(jù)綁定及其實現(xiàn)方式

    數(shù)據(jù)綁定是將應用程序UI或用戶界面綁定到模型的機制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來操縱網(wǎng)站上存在的元素。
    2021-05-05
  • AngularJS Module方法詳解

    AngularJS Module方法詳解

    AngularJS中的Module類負責定義應用如何啟動,它還可以通過聲明的方式定義應用中的各個片段。我們來看看它是如何實現(xiàn)這些功能的
    2015-12-12
  • angularJs 表格添加刪除修改查詢方法

    angularJs 表格添加刪除修改查詢方法

    下面小編就為大家分享一篇angularJs 表格添加刪除修改查詢方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02

最新評論