简体   繁体   English

url angular 中的路径变量

[英]url path variable in angular

How get variable from url path variable in angular2(v12)如何从 angular2(v12) 中的 url 路径变量中获取变量

 path: 'shop/:id', <-- get id from here
 component: RedirectGuard,
  canActivate: [RedirectGuard],
  data: {
    externalUrl: `https://example.com/shop/:id` <-- set id to here
  }

You want to use the ActivateRoute class in your component:您想在组件中使用ActivateRoute class:

import { ActivatedRoute, Router } from '@angular/router';

export class RedirectGuard implements OnInit {
 constructor(
        private route: ActivatedRoute
    ) {}

 ngOnInit() {
   const id = this.route.snapshot.params.id;
   this.router.navigate([`shop`, id]);
 }
}

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

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