Angular:打开起始页时如何读取URL字符串中的参数?

问题描述 投票:0回答:1

打开起始页时如何读取URL字符串中的参数?如果我将ID传递给URL:mysite.com/productid,应用程序将阅读并记住此ID以供将来参考?我想阅读QR码,其中包含产品ID的路径,然后直接转到该产品部分的网站。

angular url start-page
1个回答
0
投票

首先在应用程序路由模块文件中创建这样的路由

{path: 'test/:id', component: TestComponent}

并在这样的启动组件中获取ID并存储在本地存储中

constructor(
private activeRoute: ActivatedRoute
) {}

ngOnInit() { 
const getidfromurl = this.activeRoute.snapshot.paramMap.get('id');
localStorage.setItem('my id',getidfromurl);
}
© www.soinside.com 2019 - 2024. All rights reserved.