最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Allowing Angular 4 routes to pass special characters in URL - Stack Overflow

programmeradmin0浏览0评论

What i am trying to do

I am trying to create a route in my application where i want to allow the admin to type in the url as

http://localhost:4200/#/start/referral_code=jk

And then get the value for referral_code i.e jk inside the ponent.

In my routes i have defined the route as

{ path: 'start/:referral_code', ponent: StartPageComponent },    

What i am trying to achieve is that when the admin enters the above provided URL then the value for the variable referral_code should be received inside the specified ponent StartPageComponent. I have added the following inside ngOnInit() as follows

this.activatedRoute.params.subscribe((params: any) => {
      if (params) {
        let refCode = params.referral_code;
        console.log(refCode);
      }
    });

What Happens Actually

As soon as i type in the above URL the part after = is removed along with the = and the resulting url is changed to

http://localhost:4200/#/start/referral_code

and inside the ponent the console.log(refCode); displays the string referral_code rather than the value for the referral_code i.e jk.

Limitation

I cannot use QueryParams like http://localhost:4200/#/start?referral_code=jk neither I can change the url http://localhost:4200/#/start/referral_code=jk

I appreciate any help.

What i am trying to do

I am trying to create a route in my application where i want to allow the admin to type in the url as

http://localhost:4200/#/start/referral_code=jk

And then get the value for referral_code i.e jk inside the ponent.

In my routes i have defined the route as

{ path: 'start/:referral_code', ponent: StartPageComponent },    

What i am trying to achieve is that when the admin enters the above provided URL then the value for the variable referral_code should be received inside the specified ponent StartPageComponent. I have added the following inside ngOnInit() as follows

this.activatedRoute.params.subscribe((params: any) => {
      if (params) {
        let refCode = params.referral_code;
        console.log(refCode);
      }
    });

What Happens Actually

As soon as i type in the above URL the part after = is removed along with the = and the resulting url is changed to

http://localhost:4200/#/start/referral_code

and inside the ponent the console.log(refCode); displays the string referral_code rather than the value for the referral_code i.e jk.

Limitation

I cannot use QueryParams like http://localhost:4200/#/start?referral_code=jk neither I can change the url http://localhost:4200/#/start/referral_code=jk

I appreciate any help.

Share Improve this question edited Jan 23, 2018 at 14:32 Zooly 4,7874 gold badges38 silver badges55 bronze badges asked Jan 23, 2018 at 14:10 Tauqeer H.Tauqeer H. 8012 gold badges10 silver badges20 bronze badges 7
  • Have you tried typing this url: http://localhost:4200/#/start/jk. Now let refCode = params.referral_code;should work. – abdul-wahab Commented Jan 23, 2018 at 14:14
  • I know that works but what I want to get is getting the value from URL entered with referral-code='jk' – Tauqeer H. Commented Jan 23, 2018 at 14:16
  • I see, is it possible that you encode = with %3D (encoded uri). – abdul-wahab Commented Jan 23, 2018 at 14:19
  • I can't change the url also. There's a list of urls I have and I'm bound to use only that urls – Tauqeer H. Commented Jan 23, 2018 at 14:27
  • Can you afford that typed url http://localhost:4200/#/start/referral_code=jk automatically bees http://localhost:4200/#/start?referral_code=jk. Means Angular will change it itself and will make query parameter from url itself. – abdul-wahab Commented Jan 23, 2018 at 17:30
 |  Show 2 more ments

1 Answer 1

Reset to default 5

You can override Angular's DefaultUrlSerializer.

import {BrowserModule} from '@angular/platform-browser';
import {Injectable, NgModule} from '@angular/core';

import {AppComponent} from './app.ponent';
import {DefaultUrlSerializer, RouterModule, Routes, UrlSegment, UrlSerializer, UrlTree} from '@angular/router';
import {RouteTestComponent} from './route-test/route-test.ponent';

@Injectable()
export class CustomUrlSerializer implements UrlSerializer {
  /** Parses a url into a {@link UrlTree} */
  private defaultSerializer: DefaultUrlSerializer = new DefaultUrlSerializer();

  /** Parses a url into a {@link UrlTree} */
  parse(url: string): UrlTree {

    // This is the custom patch where you'll collect segment containing '='
    const lastSlashIndex = url.lastIndexOf('/'), equalSignIndex = url.indexOf('=', lastSlashIndex);
    if (equalSignIndex > -1) { // url contians '=', apply patch
      const keyValArr = url.substr(lastSlashIndex + 1).split('=');
      const urlTree = this.defaultSerializer.parse(url);

      // Once you have serialized urlTree, you have two options to capture '=' part
      // Method 1. replace desired segment with whole "key=val" as segment
      urlTree.root.children['primary'].segments.forEach((segment: UrlSegment) => {
        if (segment.path === keyValArr[0]) {
          segment.path = keyValArr.join('='); // Suggestion: you can use other unique set of characters here too e.g. '$$$'
        }
      });

      // Method 2. This is the second method, insert a custom query parameter
      // urlTree.queryParams[keyValArr[0]] = keyValArr[1];
      return urlTree;
    } else {
      // return as usual
      return this.defaultSerializer.parse(url);
    }
  }

  /** Converts a {@link UrlTree} into a url */
  serialize(tree: UrlTree): string {
    return this.defaultSerializer.serialize(tree);
  }
}

const appRoutes: Routes = [
  {
    path: 'start/:referral_code',
    ponent: RouteTestComponent
  }
];

@NgModule({
  declarations: [
    AppComponent,
    RouteTestComponent
  ],
  imports: [
    RouterModule.forRoot(appRoutes, {useHash: true}),
    BrowserModule
  ],
  providers: [
    {
      provide: UrlSerializer,
      useClass: CustomUrlSerializer
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Inside the ponent

this.route.params.subscribe(params => {
   console.log(params['referral_code']); // prints: referral_code=jk
});
// url http://localhost:4200/#/start/referral_code=jk will be changed to http://localhost:4200/#/start/referral_code%3Djk

Or if you prefer Method 2 above, use:

this.route.queryParams.subscribe(queryParams => {
  console.log(queryParams['referral_code']); // prints: jk
});
// url http://localhost:4200/#/start/referral_code=jk will be changed to http://localhost:4200/#/start/referral_code?referral_code=jk
发布评论

评论列表(0)

  1. 暂无评论