Short Answer:
Whatever you pass inside routerLink
directive will end up calling encodeURI
function on value that have been passed.
Long Answer
This happens because when you directly pass whole url inside routerLink
it eventually call serializeUrl
method of routerLink
directive. Which call serialize
method of urlSerializer
. The implementation of serialize method looks like below. code link here
serialize(tree: UrlTree): string {
const segment = `/${serializeSegment(tree.root, true)}`;
const query = serializeQueryParams(tree.queryParams);
const fragment =
typeof tree.fragment === `string` ? `#${encodeUriFragment(tree.fragment !)}` : '';
return `${segment}${query}${fragment}`;
}
export function encodeUriFragment(s: string): string {
return encodeURI(s);
}
Possible workaround to solve this issue could be, decode the URL by using angular Router
's inbuilt method called as parseUrl
method. Which basically help you to find root
of the URL
and params
, queryParams
, etc.
Component
@Component({
...
})
export class AppComponent {
name = 'Angular 6';
url: any;
formattedUrl: any;
params: any = {};
constructor(private router: Router) {
}
ngOnInit() {
this.urlDecoder();
}
urlDecoder() {
this.url = '/a?i=1 a';
let parsedUrl = this.router.parseUrl(this.url);
console.log(parsedUrl)
const g: UrlSegmentGroup = parsedUrl.root.children[PRIMARY_OUTLET];
const s: UrlSegment[] = g.segments;
this.formattedUrl = `${s.map(p => p.path).join('/')}`; // this is required to combine all continued segment
this.params = parsedUrl.queryParams;
}
}
Html
<a [routerLink]="formattedUrl" [queryParams]="params">
Link Here
</a>
Demo Stackblitz