如何从完整的 url 字符串中获取唯一路径 [英] How to get only path from full url string

查看:38
本文介绍了如何从完整的 url 字符串中获取唯一路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用路由器 (this.router.url) 时,我得到字符串 /MyRouterName?type=abc,有时 /MyRouterName 如果没有参数

When using Router ( this.router.url) I am get the string /MyRouterName?type=abc, sometime /MyRouterName if not have parameter

我是否只能从完整的 url 中获取路径 /MyRouterName(对于所有情况)?

Can I only get the path /MyRouterName (for all cases) from the full url?

推荐答案

您可以使用激活的路由组件的快照来获取此 url.

You can use the activated route component's snapshot to get this url.

    import { Router, ActivatedRoute } from '@angular/router';
     constructor(
        private router: Router,
        private activatedRoute: ActivatedRoute) {

        console.log("routes");
        console.log(activatedRoute.snapshot.url); // array of states
        console.log(activatedRoute.snapshot.url[0].path); 
    }

链接到 Jose G Varanam 的原始 SO 答案如何获取当前路线

Link to original SO answer by Jose G Varanam How to get current route

这篇关于如何从完整的 url 字符串中获取唯一路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆