我有这样的:检查router.url是否包含特定的字符串?
if (this.router.url === '/test/sort') {
this.active = 0;
}
问题是,有时URL将被test/sort?procesId=11
比if语句也不会介入;任何建议我该怎么做?
我有这样的:检查router.url是否包含特定的字符串?
if (this.router.url === '/test/sort') {
this.active = 0;
}
问题是,有时URL将被test/sort?procesId=11
比if语句也不会介入;任何建议我该怎么做?
这有更好/更清洁的解决方案,但如果你想要的东西基本的作品:
if (this.router.url.indexOf('/test/sort') > -1) {
this.active = 0;
}
我很想以此为基础回答这个问题,因为你说这不是一个好的答案,还有更好的解决方案...... – musefan
您可以使用LocationStrategy获得URL不带参数。
import {LocationStrategy} from '@angular/common';
export class AppComponent implements OnInit {
constructor(private url:LocationStrategy) { }
ngOnInit() {
console.log(this.url.path());
if(this.url.path()==='/test/sort'){
this.active=0;
}
}
}
为什么不复制问题标题并将其粘贴到Google中并阅读一些结果。像[第一个](https://stackoverflow.com/questions/32133680/how-to-check-whether-the-url-contains-id-or-not)这几乎是你想要的 – musefan
我会并不总是有ID – None