首页 文章

使用activatedRoute访问Angular中的编码查询参数

提问于
浏览
1

我有角度应用程序,从另一个应用程序重定向后加载 . 我想在身份验证网站返回我的Angular应用程序时访问查询参数 . 网址看起来像

http://localhost:4200/#/starterview?openId%3Dhttps:%2F%2Fmy.fidesque.net%2Fopenid%2Fyobtest_smeuser%26email%3Dyobtest@gml.nl%26userAuthMode%3DSP=

我想访问查询参数但我无法正确使用 this.activatedRoute.snapshot.queryParams 我怀疑它是由于编码参数 . 有没有解决方法?

问候,Venky

2 回答

  • 0

    您可以使用decodeURIComponent解码编码的URL,如Venky所回答的那样 . 如果您知道查询参数并想要从解码的URL中提取值 . 使用此功能:

    getQueryParamFromMalformedURL(name) {
        const results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(decodeURIComponent(this.router.url)); // or window.location.href
        if (!results) {
            return 0;
        }
        return results[1] || 0;
    }
    

    如果网址是http://localhost:4200/login?username=jerry&code=1234,那么

    this.getQueryParamFromMalformedURL('username'); // should return 'jerry'
    
  • 0

    我使用以下代码解码了URL . 然后我可以正确地提取参数

    const encodedUrl = 'http://localhost:4200/#/starterview?openId%3Dhttps:%2F%2Fmy.fidesque.net%2Fopenid%2Fyobtest_smeuser%26email%3Dyobtest@gml.nl%26userAuthMode%3DSP='
    const decodedUrl = decodeURIComponent(encodedUrl);
    console.log(decodedUrl);
    

相关问题