我有這樣的:檢查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