[英]How to get query params in angular 6/7 without routes
I have an angular app with a single page for a confirmation that accepts a hash value in the query.我有一个带有单个页面的 angular 应用程序,用于确认接受查询中的哈希值。 There are no routes in the application.
应用程序中没有路由。 Just a single page and the component.
只是一个页面和组件。 How can I retrieve the hash value from the url query parameter?
如何从 url 查询参数中检索哈希值?
I tried adding ActivatedRoute to the component but got an error saying there was no provider.我尝试将 ActivatedRoute 添加到组件中,但收到一条错误消息,指出没有提供程序。
I would like to avoid having to add routes for this purpose, if possible.如果可能,我想避免为此目的添加路由。
I ended up using https://stackoverflow.com/a/31057221/10609045我最终使用了https://stackoverflow.com/a/31057221/10609045
do i get it right, that you want to use ActivatedRoute in a component like this?:我说得对吗,您想在这样的组件中使用 ActivatedRoute 吗?:
@Component({..})
export class YourComponent {
constructor(
private readonly route: ActivatedRoute,
) {
route.queryParams.subscribe( (params) => this.anyAction(params));
}
}
have you added the RouterModule to the module where you declared your component?:您是否将 RouterModule 添加到您声明组件的模块中?:
@NgModule({
imports: [
RouterModule
],
declarations: [
YourComponent
]
})
export class YourModule {}
Check the answer at Angular error: no provider for ActivatedRoute检查Angular 错误中的答案:没有 ActivatedRoute 的提供者
You need to import RouterModule.forRoot([])
in your AppModule
to be able to use ActivatedRoute
您需要导入
RouterModule.forRoot([])
在您AppModule
以能够使用ActivatedRoute
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.