簡體   English   中英

如何從URL(路由)Angular 2獲取參數?

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

我嘗試在ngOnInit()這樣做:

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

我的網址是:

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

路由是:

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

試試看:

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

頁面是查詢參數,因此路徑必須是這樣的

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

不要忘記將路由添加到構造函數中

constructor(private route: ActivatedRoute) { }

和進口

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

嘗試

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