第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

路由之跳轉(zhuǎn)與傳參

路由跳转

通常理解的页面跳转是,发生点击事件时,从一个页面跳转到另一个页面的过程。
但对于 Angular 这种单页面应用而言,页面跳转只是与路径进行匹配,若匹配成功则渲染相应的组件,并通过 html5 的 history.pushState 方法更新历史记录。

模板中的指令跳转

在模板文件中,通过 RouterLink 指令来设置路由跳转。

例子:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './components/login/login.component';
import { WelcomeComponent } from './components/welcome/welcome.component';
import { PageNotFoundComponent } from './components//page-not-found/page-not-found.component';
import { AboutComponent } from './components//about/about.component';

const routes: Routes = [
  { 
    path: 'login', 
    component: LoginComponent
  },
  { 
    path: 'welcome', 
    component: WelcomeComponent
  },
  { 
    path: 'about', 
    component: AboutComponent
  },
  { 
    path: '', 
    redirectTo: '/login', 
    pathMatch: 'full'
  },
  { path: '**', 
    component: PageNotFoundComponent
  }
];

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

app.component.html

<section>
  <header style="text-align: center;">
    <h2>大连 —— 美丽的北方明珠</h2>
  </header>

  <p style="display: flex; justify-content: space-evenly;">
    <a [routerLink]="['/welcome']">欢迎页面</a>
    <a [routerLink]="['/about']">关于大连</a>
  </p>
  
  <hr>
  <router-outlet></router-outlet>

  <footer></footer>
</section>

图片描述

图片描述

RouterLink 指令不局限于 <a> 标签,它可以被应用在任何 html 标签上,比如常用的 <button>

例子:

// app.component.html
<p style="display: flex; justify-content: space-evenly;">
  <button [routerLink]="['/welcome']">欢迎页面</button>
  <button [routerLink]="['/about']">关于大连</button>
</p>

图片描述

当 RouterLink 被激活时,可以通过 routerLinkActive 指令指定 CSS 类。

例子:

// app.component.scss
.active{
  border: 1px solid red;
}

// app.component.html
<p style="display: flex; justify-content: space-evenly;">
  <button [routerLink]="['/welcome']" [routerLinkActive]="['active']">欢迎页面</button>
  <button [routerLink]="['/about']" [routerLinkActive]="['active']">关于大连</button>
</p>

图片描述

组件中的代码跳转

在组件中,通过 Router.navigate() 方法来完成跳转。

例子:

app.component.html

<p style="display: flex; justify-content: space-evenly;">
  <button (click)="toWelcome()">欢迎页面</button>
  <button (click)="toAbout()">关于大连</button>
</p>

app.component.ts

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

// 导入 Router 对象
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  constructor(

    // 初始化 Router 对象
    private router: Router,
  ) { }

  // 跳转 welcome 页面
  toWelcome(){
    this.router.navigate(['/welcome']);
  }

  // 跳转 about 页面
  toAbout(){
    this.router.navigate(['/about']);
  }

}

图片描述

图片描述

路由传参

Angular 路由具有传递参数的功能,可以通过 URL 向目标组件传递数据。

路径参数

// 基本形式
http://localhost:4200/welcome/李雷

路径参数,顾名思义,在配置路由的 path 属性时,可以在路径一段的前面添加冒号,表明这是一个参数。

// 在 path 下配置路由参数 name
{ 
  path: 'welcome/:name', 
  component: WelcomeComponent
}

接下来,不管是手动输入 URL,还是通过 RouterLink,或者 Router.navigate() 完成跳转,都需要给参数 name 赋值。

[routerLink]="['/welcome','李雷']"
this.router.navigate(['/welcome','李雷']);

图片描述

获取参数

为了使用路径参数,我们需要在目标组件中导入 ActivatedRoute 对象。
该 ActivatedRoute 对象包含了一个 params 参数订阅,可以从 URL 中解析出路径参数。

例子:

welcome.component.ts

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

// 导入 ActivatedRoute
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.scss']
})
export class WelcomeComponent implements OnInit {
  name!:string;

  constructor(

    // 初始化 ActivatedRoute
    private activedRoute: ActivatedRoute,
  ) { }

  ngOnInit() {

    // 使用参数订阅
    this.activedRoute.params.subscribe((params:any)=>{
      this.name = params['name'];
    });
  }

}

图片描述

查询参数

// 基本形式
http://localhost:4200/welcome?name=李雷&local=武汉

查询参数无需在 path 属性中进行配置,而且可以拥有任意多个参数。
同样通过 RouterLink 或者 Router.navigate() 来赋值。

[routerLink]="['/welcome']" [queryParams]="{name:'李雷', local:'武汉'}"

this.router.navigate(
    ['/welcome'],
    {
      queryParams:{
        name:'李雷',
        local:'武汉'
      }
    }
  );

获取参数

通过 ActivatedRoute 对象的 queryParams 参数订阅,获取到查询参数。

this.activedRoute.queryParams.subscribe((queryParams:any)=>{
  this.name = queryParams['name'];
  this.local = queryParams['local'];
});

图片描述


end

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消