检查它是否包含 router.url 某些线?

我有它 :


if /this.router.url === '/test/sort'/ {

this.active = 0;
}


问题是有时候 url 将
test/sort?procesId=11

, 他不会进入什么 if statement. 有什么建议,我可以做到吗?
已邀请:

奔跑吧少年

赞同来自:

如果您想要一些基本的作品:


if /this.router.url.indexOf/'/test/sort'/ > -1/ {
this.active = 0;
}

八刀丁二

赞同来自:

您也可以使用:


if /this.router.url.includes/'/test/sort'// 
{
this.active = 0;
}


方法 String.includes/searchString: string, position?: number/: boolean - 回报 true, 如果一个 searchString 它看起来是将此对象转换为一个或多个位置的字符串的子字符串导致,这些结果在一个或多个位置更大或等于位置; 否则退货 false.

莫问

赞同来自:

您可以使用 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;
}

}

}

要回复问题请先登录注册