首页 > 解决方案 > HashLocationStrategy 和 Location Strategy Import.Angular 2 & Typescript 后未检测到路径

问题描述

if (location.pathname.includes('aboutUs')) {
      this.isShow = false;
     }
  1. 在 HashLocationStrategy 和 Location Strategy Import 未完成之前,它的工作正常。
  2. 导入后由于# in url 无法搜索给定的路径。

标签: angulartypescript

解决方案


您可以使用这种方法来处理ActivatedRoute、 路径,并获取 url 参数和查询参数。

export class YourComponent {

    constructor(private activatedRoute: ActivatedRoute) { }

    this.activatedRoute.url.subscribe(urlSegments => {
      for (const item of urlSegments) {
        if (item.path === 'aboutUs') {
          console.log('Awesome! Path Found...');
        }
      }
    });
}

推荐阅读