为了账号安全,请及时绑定邮箱和手机立即绑定

angular路由传参,这两种方式不得不看!

标签:
AngularJS

    前面给大家介绍了angular路由跳转,这次说一说angular路由传参,两种具体如下:

1,查询方式传参

<a href="" routerLink="/about" [queryParams]='{"nihao":"d dod f"}'>关于我们</a>


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

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

@Component({

  selector: 'app-about',

  templateUrl: './about.component.html',

  styleUrls: ['./about.component.css']

})

export class AboutComponent implements OnInit {


  constructor(private route:ActivatedRoute) { }


  ngOnInit(): void {

    debugger

    this.route.queryParamMap.subscribe(function(query){

      debugger

      console.log(query)

      console.log(query.get("nihao"))

      

    })

  }


}


2,动态方式传参

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

import { RouterModule, Routes, UrlSegment } from '@angular/router';

import { AboutComponent } from './page/about/about.component';

import { HomeComponent } from './page/home/home.component';


const routes: Routes = [

  { path: 'home/:name/:age', component: HomeComponent },

  { path: 'about', component: AboutComponent },

  //pathMatch表示完全匹配,其中

  { path: '', redirectTo:"/about",pathMatch:"full" },

];


@NgModule({

  //RouterModule.forRoot()启动路由

  imports: [RouterModule.forRoot(routes)],

  exports: [RouterModule],

})

export class AppRoutingModule {}


<a href="" [routerLink]="['/home','zhangdan']">首页</a> &ngsp;

    以上便是关于angular路由传参的两种方法,更多内容干货可关注慕课网~

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消