我正在尝试设置我的角2应用程序到默认的页面加载路由,不管它是什么路由,但我似乎无法让它工作。
我在应用程序组件的ngOnInit中添加了逻辑,以重定向到我的主路由,但它被忽略了。
import {Component, OnInit} from '@angular/core';
import {Router} from "@angular/router";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private router: Router) {}
closeApplication() {
electron.ipcRenderer.send('close-app');
}
ngOnInit() {
console.log('I am being run.');
this.router.navigate(['']);
}
}这是我的路由配置
import {LoginComponent} from "./login/login.component";
import {HomeComponent} from "./home/home.component";
import {AuthGuard} from "./shared/security/auth.guard";
import {RegisterComponent} from "./register/register.component";
import {ListingComponent} from "./listing/listing.component";
import {RedirectGuard} from "./shared/security/redirect.guard";
import {WelcomeComponent } from "./welcome/welcome.component";
export const routerConfig = [
{ path: 'login', component: LoginComponent, canActivate: [RedirectGuard] },
{ path: 'register', component: RegisterComponent, canActivate: [RedirectGuard] },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard],
children: [
{
path: '',
component: ListingComponent
},
{
path: 'listing/:id',
component: ListingComponent
}
]
},
{ path: '', component: WelcomeComponent },
{ path: '**', component: HomeComponent },
];对于某些上下文,我正在创建一个具有线性用户流的电子应用程序,如果用户在默认页面上启动该应用程序,它将工作得非常出色。如果我在第3页上并刷新,整个应用程序状态都不正常。我不使用数据库来保留状态,我认为存储在本地存储对我的最终目标来说是一种负担。
干杯!
发布于 2017-01-31 00:10:34
我刚刚将以下体系结构实现到一个项目中,在该项目中,我创建了一个redirect.component,该项目解决了课程加载后的位置问题。以下是一个超级简化的版本;
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-redirect',
template: ``
})
export class RedirectComponent implements OnInit {
private goDownPathOne: boolean = true;
constructor() { }
ngOnInit() {
if(goDownPathOne)
this.router.navigate('path1');
else
this.router.navigate('path2');
}
}然后,在我的app.routing中,我有以下内容:
const COURSE_ROUTES: Routes = [
// Home / Menu Screen
{ path: '', component: RedirectComponent },
// Default Routes
{ path: 'screen', redirectTo: '/', pathMatch: 'full' },
{ path: 'text', redirectTo: '/', pathMatch: 'full' },
{ path: 'activity', redirectTo: '/', pathMatch: 'full' },
{ path: '**', redirectTo: '/', pathMatch: 'full' }
];
export const courseRouting: ModuleWithProviders = RouterModule.forRoot(COURSE_ROUTES);因此,在应用程序加载时,它总是会转到RedirectComponent,然后由它决定我们应该在哪里处理所提供的数据--在我的例子中,我把它从数据库中拖了下来。我不确定这是否是最优的方法,但如果违抗有效!
https://stackoverflow.com/questions/40055668
复制相似问题