Angular应用程序抑制queryParams查询参数
创始人
2024-10-30 14:30:20
0

要在Angular应用程序中使用queryParams查询参数,需要在路由上声明对应的参数名称。示例代码如下:

在路由定义中声明查询参数名称:

const routes: Routes = [
  { path: 'products', component: ProductListComponent },
  { path: 'products/:id', component: ProductDetailComponent },
  { path: 'login', component: LoginComponent },
  { path: '', redirectTo: '/products', pathMatch: 'full' }
];

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

在组件中获取并使用查询参数:

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

@Component({
  selector: 'app-product-detail',
  templateUrl: './product-detail.component.html',
  styleUrls: ['./product-detail.component.css']
})
export class ProductDetailComponent implements OnInit {
  productId: number;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.productId = +this.route.snapshot.queryParamMap.get('id');
  }
}

相关内容

热门资讯

开挂透视!wepoker免费辅... 开挂透视!wepoker免费辅助器,微乐自建房黑科技辅助工具下载,经验教程(有挂总结)-哔哩哔哩1、...
解迷透视!pokemmo手机版... 解迷透视!pokemmo手机版脚本,小程序微乐游戏辅助器,操作教程(有挂秘诀)-哔哩哔哩进入游戏-大...
关于透视!HH平台挂,微乐春天... 关于透视!HH平台挂,微乐春天扑克开挂辅助插件,项目教程(真实有挂)-哔哩哔哩;1、HH平台挂免费脚...
分享透视!红龙poker作弊指... 分享透视!红龙poker作弊指令,微乐手游脚本辅助,秘籍教程(有挂分享)-哔哩哔哩1、打开软件启动之...
详情透视!菠萝德州透视脚本,微... 详情透视!菠萝德州透视脚本,微信小程序游戏破解微乐游戏,方式教程(讲解有挂)-哔哩哔哩1、菠萝德州透...
解谜透视!wepoker辅助器... 解谜透视!wepoker辅助器下载,微信小程序微乐破解器2024,资料教程(有挂解惑)-哔哩哔哩1、...
专业透视!德普之星辅助软件,微... 专业透视!德普之星辅助软件,微乐春天小程序辅助,总结教程(有挂头条)-哔哩哔哩1、进入到德普之星辅助...
科普透视!hhpoker辅助软... 科普透视!hhpoker辅助软件下载,微乐小程序脚本入口,妙计教程(有挂存在)-哔哩哔哩1)hhpo...
科普透视!wepoker怎么破... 科普透视!wepoker怎么破解游戏,微乐小程序游戏破解器下载,绝活教程(有挂方略)-哔哩哔哩1)w...
分享透视!wepoker透视有... 分享透视!wepoker透视有吗,微乐自建房插件如何安装,积累教程(存在有挂)-哔哩哔哩1、该软件可...