在刷新时重定向到angular2中的路径

时间:2017-11-15 05:13:25

标签: angular angular4-router

嗨,在我的应用程序中如果我刷新它将转到主页,我需要避免这种情况并希望重定向到特定的所需路径,无法在路由或某处找到实现此位置的地方,任何人都可以帮助我。 我的app.component文件

import {Component, OnInit} from '@angular/core';
import {Router, ActivatedRoute, Params} from "@angular/router";
import {AppService} from "../../services/app.service";

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
    islogin: boolean = true;
    currentPage: string = '';
    constructor(
      private router: Router,
      public appService: AppService
    ) {
    }
  ngOnInit() {
      this.islogin = !this.islogin;
  }
}

路由文件:

export const appRoutes: Routes = [
     {
    path: 'login',
    component: LoginComponent
},
    {
        path: 'superuser-miscsuperusers',
        component: miscsuperusersComponent
    },
    {
        path: '**',
        component: LoginComponent
    },
{
    path: 'clients',
    component: ClientsComponent
}

];

export const appRoutingProviders: any[] = [];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes/*, { enableTracing: true }*/);

1 个答案:

答案 0 :(得分:0)

您可以从app.component.ts文件导航到特定页面。在启动或加载时

//Decide the page based on your logic .
let link = ['/landingpage'];
this.router.navigate(link);

这里的问题是你将在刷新时丢失所有临时数据,因此你应该使用其他一些存储来恢复旧的应用程序状态ex:localstorage。