我正在尝试根据某些条件将用户重定向到另一个页面。这是我的示例登录组件:
ngOnInit() {
console.log(">>> router", this.router)
console.log(">>> activatedRoute", this.activatedRoute)
if (this.activatedRoute.queryParams['value'].param === 'value') {
console.log(">>> redirecting")
this.router.navigate(['home']);
}
}
如果我导航到没有参数的登录组件,那么我会看到登录组件模板,这很好。
但是,当我使用名为 param 的参数和 value 的值导航到登录组件时,我想将用户重定向到主页。那是行不通的。
这是我的主页路线:
import { NgModule } from '@angular/core';
import { HomeComponent } from './home.component';
import { RouterModule, Routes } from '@angular/router';
const thisRoute: Routes = [
{
path: 'home',
component: HomeComponent
}
];
@NgModule({
imports: [
RouterModule.forRoot(thisRoute, {
useHash: true, initialNavigation: false
})
],
declarations: [
HomeComponent
]
})
export class HomeModule { }
我在 github 中创建了一个测试项目,供您下载并在您的计算机上运行。只需下载这个项目:https ://github.com/wvary/route-test
运行npm install
。
然后运行npm run start
。
导航到http://localhost:8080/#/home
您应该会看到如下内容:
您可以通过单击三个链接查看每个页面的内容。这个想法是当您单击中间链接时位于主页上。
谢谢