I need to browse a URL direct and pass a value, such as localhost:4200/vip/mycode, then this will direct to the default page.
I have the routing.module.ts set like this:
const routes: Routes = [
{ path: 'enroll', component: EnrollComponent },
{ path: 'default', component: DefaultComponent },
{ path: 'aboutus', component: AboutusComponent },
{ path: 'overview', component: OverviewComponent },
{ path: 'contactus', component: ContactusComponent },
{ path: 'vip/:code', component: VipComponent },
];
And the vip.component.ts file like this:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
@Component({
selector: 'app-vip',
templateUrl: './vip.component.html',
styleUrls: ['./vip.component.css']
})
export class VipComponent implements OnInit {
public code: string;
public APIresult: any;
constructor(
private _route: ActivatedRoute,
private _router: Router,
) {
this._route.params.subscribe((params: Params) => {
this.code = params.code;
if (this.code != null) {
sessionStorage.setItem('code', this.code);
alert(this.code)
}
})
}
ngOnInit(): void {
}
}
But I just cant make it work when browsing localhost:4200/vip/mycode
When I do, it just takes me to the default component.
Here is my app.component.ts:
import { Component } from '@angular/core'; import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Website';
constructor(
private router: Router,
){
this.router.navigateByUrl("/default")
}
}
Any help will be appreciated. Thanks.