Angular 5-在刷新浏览器时将页面重定向到主页 [英] Angular 5 - redirect page to homepage on browser refresh
问题描述
当前,当我从
它保持在同一条路线上.但我想将路线重定向到
it stays on the same route. But I want the route to redirect to
我看到人们问过如何实施刷新以保持相同的路线.因此,我想默认角度应在浏览器刷新时重定向到主页.知道为什么我的角度默认项目会这样做吗?
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屋!