Angular 4路由如何正确接收含等号的URL参数
referral_code=jk Format Got it, let's break down why your current setup isn't working and how to fix it while sticking to your requirements (no query params, no URL format changes).
Why the Problem Occurs
Your current route path: 'start/:referral_code' uses Angular's default parameter matching pattern, which is [^/]+ (matches any character except a slash). But when you use a URL like http://localhost:4200/#/start/referral_code=jk, Angular's router is treating the = as a special character and truncating everything after it—hence you're seeing referral_code instead of referral_code=jk as the parameter value.
Step-by-Step Solution
1. Update Route Configuration to Allow = in Parameters
First, we need to tell Angular's router to match the entire segment including the = and the code after it. We do this by adding a custom regex pattern to the route parameter:
// In your routing module (e.g., app-routing.module.ts) const routes: Routes = [ // ... other routes { path: 'start/:referral_code(.+)', // The (.+) regex matches any non-slash characters, including = component: StartPageComponent } ];
The .+ regex ensures the router captures the full referral_code=jk string as the referral_code parameter value, instead of truncating at the =.
2. Parse the Parameter Value in Your Component
Now that the router is passing the full referral_code=jk string to your component, you just need to split that string to extract the actual code (jk):
// In StartPageComponent.ts import { ActivatedRoute } from '@angular/router'; import { OnInit } from '@angular/core'; export class StartPageComponent implements OnInit { constructor(private activatedRoute: ActivatedRoute) { } ngOnInit(): void { this.activatedRoute.params.subscribe((params) => { if (params.referral_code) { // Split the parameter string at the = sign const [key, refCode] = params.referral_code.split('='); if (key === 'referral_code' && refCode) { console.log(refCode); // This will log 'jk' now! } else { console.log('Invalid referral code format'); } } }); } }
3. Verify Hash Location Strategy (If Needed)
Since your URL uses the hash fragment (/#/), make sure your app is configured to use HashLocationStrategy to avoid any browser path parsing issues:
// In app.module.ts import { LocationStrategy, HashLocationStrategy } from '@angular/common'; @NgModule({ // ... imports, declarations, bootstrap providers: [ { provide: LocationStrategy, useClass: HashLocationStrategy } ] }) export class AppModule { }
This ensures the hash part of the URL is handled correctly by Angular's router.
Bonus: Use Route Snapshot If You Don't Need Parameter Updates
If your component doesn't need to listen for changes to the referral_code parameter after initialization, you can use the route snapshot for simpler code:
ngOnInit(): void { const paramValue = this.activatedRoute.snapshot.params.referral_code; if (paramValue) { const [key, refCode] = paramValue.split('='); if (key === 'referral_code' && refCode) { console.log(refCode); } } }
That's it! With these changes, your URL will stay as http://localhost:4200/#/start/referral_code=jk, and your component will correctly extract the jk value.
内容的提问来源于stack exchange,提问作者Tauqeer H.

