首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >parameters未接收参数

parameters未接收参数
EN

Stack Overflow用户
提问于 2020-01-30 11:54:17
回答 2查看 63关注 0票数 0

navbar.component.html

代码语言:javascript
复制
<a *ngFor="let item of studentClasses" routerLink="/classes/all-courses/, {queryParams: {prop: item.className}}">

class-routing.module.ts

代码语言:javascript
复制
export const routes: Routes = [
  {
    path: 'all-courses/:className',
    component: AllCoursesComponent
  },
]

all-courses.component.ts

代码语言:javascript
复制
ngOnInit() {
    this.activeRoute.queryParams.subscribe(queryParam => {
      console.log(queryParam);
      // here I'm receving empty object. But I'm expecting to receive className
    });
}
EN

回答 2

Stack Overflow用户

发布于 2020-01-30 12:00:28

代码语言:javascript
复制
You can set query params and fragment as follows:

`<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}">
  link to user component
</a>`


RouterLink will use these to generate this link: 

`/user/bob?debug=true`


  `constructor(private route: ActivatedRoute){}`

  `ngOnInit(){
      this.route.queryParamMap
                .map((params: Params) => params.params)
                .subscribe( (params) => {
                     if(params && params['test']){
                        let testQueryParamVal = params['test'];
                     }
                 });
  }`
票数 0
EN

Stack Overflow用户

发布于 2020-01-30 12:01:39

导入组件中的ActivatdeRoute和路由器模块,并将它们注入构造函数中

代码语言:javascript
复制
import { ActivatedRoute, Router } from '@angular/router'

contructor(private route: ActivatedRoute, private router: Router){ ... }
Subscribe the ActivateRoute to the ngOnInit

ngOnInit() {

    this.route.queryParams.subscribe(params => {
      console.log(params);

    })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59985498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档