本文介紹了
1. 獲取url以及route里面的對象
在用戶使用應用程序時,Angular 的路由器能讓用戶從一個視圖導航到另一個視圖。但是當你想要獲取url里面的一些值呢?下面是幾個方法獲取Route對象。
路由器會構建出一個 ActivatedRoute組成的樹,它表示路由器的當前狀態(tài)。 你可以在應用中的任何地方用 Router服務及其 routerState 屬性來訪問當前的 RouterState 值。
export class CompanyComponent implements OnInit {
constructor(private route: ActivatedRoute) {}
ngOnInit() {
// Get Parent Path: about 獲取父級path值
/*[UrlSegment]
0: UrlSegment
parameterMap: (...)
parameters: {}
path: "xxxx"
__proto__: Object
length: 1
__proto__: Array(0)
*/
this.route.parent.url.subscribe(url => console.log(url[0].path));
// Get Current Path: company 同理
this.route.url.subscribe(url => console.log(url[0].path));
// Route Data: { title: 'Company' }
this.route.data.subscribe(data => console.log(data));
}
}