【问题标题】:on refresh redirect to path in angular2刷新重定向到angular2中的路径
【发布时间】:2017-11-15 05:13:25
【问题描述】:

嗨,在我的应用程序中,如果我刷新它会转到主页,我需要避免这种情况并希望重定向到特定的所需路径,无法找到在路由或某处实现此功能的位置,谁能帮助我. 我的 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 }*/);

【问题讨论】:

  • 添加你的应用路由文件
  • @Sravan 添加了我的路由文件
  • 你从哪个路由刷新,因为如果你点击一个在路由中没有它的路由的页面,它会进入登录页面,就像你给的path: '**'
  • 一般情况下,path: '**' 应该被重定向到任何未找到的页面
  • @Sravan 如果我从 'superuser-miscsuperusers' 路径刷新它将进入登录页面,我必须避免这种情况并希望重定向到我在路由文件中没有提到的 'client' 路径跨度>

标签: angular angular4-router


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2019-07-16
    相关资源
    最近更新 更多