简体   繁体   English

如何从URL(路由)Angular 2获取参数?

[英]How to get parameter from URL(Routing) Angular 2?

I tried to do that in ngOnInit() like: 我尝试在ngOnInit()这样做:

ngOnInit() {
    this.route.queryParams
      .filter(params => params.type)
      .subscribe(params => {
        this.tab = params.type;
        this.setHeader();
      });
  }

My URL is: 我的网址是:

http://localhost:4200/s/6/t/p/create/1

Routing is: 路由是:

{path: ':Id/t/p/create/:type', component: CalendarComponent},

try that : 试试看:

this.route.snapshot.params["page"]

the page is the query param so the route must be like that 页面是查询参数,因此路径必须是这样的

const routes: Routes = [
  {
    path: 'page/:page',
    component: OrdersComponent,
    data: {
      title: 'Orders'
    }
  }
]

don't forget the add the route into the constructor 不要忘记将路由添加到构造函数中

constructor(private route: ActivatedRoute) { }

and the import 和进口

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

Try 尝试

this.route.params.subscribe(params => {
    this.tab = params.type;
    this.setHeader();
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM