Angular 5-在刷新浏览器时将页面重定向到主页 [英] Angular 5 - redirect page to homepage on browser refresh

查看:313
本文介绍了Angular 5-在刷新浏览器时将页面重定向到主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当我从

http://localhost:4200/feedback

它保持在同一条路线上.但我想将路线重定向到

it stays on the same route. But I want the route to redirect to

http://localhost:4200

我看到人们问过如何实施刷新以保持相同的路线.因此,我想默认角度应在浏览器刷新时重定向到主页.知道为什么我的角度默认项目会这样做吗?

I saw people have asked how to implement the refresh to stay on the same route. So I guess, the default angular should redirect to homepage on browser refresh. Any idea why my angular default project does this otherwise?

下面是我的AppRoutingModule

Below is my AppRoutingModule

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { SmileyFeedbackComponent } from '../smiley-feedback/smiley-feedback.component';
import { FeedbackFormComponent } from '../feedback-form/feedback-form.component';
import { ThankYouComponent } from '../thank-you/thank-you.component';

const routes: Routes = [
  { path: '', redirectTo: '/smiley', pathMatch: 'full' },
  { path: 'smiley', component: SmileyFeedbackComponent },
  { path: 'feedback', component: FeedbackFormComponent },
  { path: 'thank-you', component: ThankYouComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [ RouterModule ],
  declarations: []
})
export class AppRoutingModule { }

推荐答案

正如克里斯·夏普(Chris Sharp)所述,您的应用程序确实在做应做的事情,因为您没有告诉应用程序,它会路由到url指向的位置否则.

As mentioned by Chris Sharp, your app is doing exactly what it should do, routing to the place that the url is pointing to, since you have not told it otherwise.

您可以做的是,在您的app.component中,您可以在OnInit中重定向到root.这意味着当(重新)初始化应用程序时,您将被重定向到根页面.

What you can do, is that in your app.component you can in OnInit redirect to root. This then means that when app is (re)initialized, you are being redirected to root page.

export class AppComponent { 
  constructor(private router: Router) {}

  ngOnInit() {
    this.router.navigate([''])
  }
}

这篇关于Angular 5-在刷新浏览器时将页面重定向到主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆