在Angular中,URL参数通常是通过路由器(Router)来获取的。当URL没有以#结尾时,可以通过使用ActivatedRoute来获取URL参数。
下面是一个示例代码,演示了如何使用ActivatedRoute来获取URL参数:
import { ActivatedRoute, Router } from '@angular/router';
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
this.route.params.subscribe(params => {
// 在这里处理URL参数的逻辑
console.log(params); // 打印URL参数
});
}
在上述示例中,ngOnInit()方法会在组件初始化时被调用,并订阅URL参数的变化。当URL参数发生变化时,params对象会被更新,并可以在其中获取最新的URL参数。
确保在使用这种方法之前,需要在路由配置中定义参数的占位符。例如,如果要获取名为"id"的URL参数,需要在路由配置中定义如下:
{ path: 'example/:id', component: ExampleComponent }
通过以上步骤,你可以在Angular中获取URL参数,即使URL没有以#结尾。