简体   繁体   English

从具有特殊字符的网址获取角度参数

[英]Angular Get Parameter from Url with Special Character

my routing is 我的路线是

{
                path: 'purchase/:discountCode',
                component: PurchaseComponent,
            },
            {
                path: 'purchase/:msg',
                component: PurchaseComponent,
            },

my input url is 我的输入网址是

www.example.com/purchase?msg='test message'
or
www.example.com/purchase?discountCode=1111111

i want to get msg or discount code 我想获得味精或折扣码

  this.routerParmsSub = this.route.params.subscribe(params => {
      if (params['discountCode']) {
        //save discountCode

      } else if (params['msg']) {
    //save msg
      }

but only when my url is 但只有当我的网址是

   www.example.com/purchase;msg='test message'
    or
    www.example.com/purchase;discountCode=1111111

can get parameter instead ? 可以获取参数吗? is ; 是;

and when input url is 当输入URL是

www.example.com/purchase/1111111

routing to discountCode 路由到discountCode

my angular version is 7 我的角度版本是7

You can do this like that 你可以那样做

import {ActivatedRoute} from '@angular/router';
...

constructor(private route:ActivatedRoute){}
dicountCode:string;

ngOnInit(){
    this.dicountCode = this.route.snapshot.params['dicountCode'];
}

If you want to access query params you can do it in the following way 如果要访问查询参数,可以通过以下方式进行

this.route.snapshot.queryParamMap.get('dicountCode');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM