刷新后页面已更改

时间:2019-04-21 11:18:23

标签: angular typescript

我正在创建一个角度为6的Instagram副本,因此我遇到了问题。 如果我在主页组件的页面上进行刷新,则会正确刷新以显示我的所有帖子,但是如果我在个人资料页面上进行刷新,它将自动将我重定向到主页,而不是刷新并保留在个人资料页面上 home

刷新后可以正常工作

个人资料视图 Profile View

刷新后 enter image description here

app.routes

import {Routes} from '@angular/router'
import { AcessoComponent } from './acesso/acesso.component';
import { HomeComponent } from './home/home.component';
import { AutenticacaoGuard } from './autenticacao-guard.service';
import { ProfileComponent } from './profile/profile.component';



export const ROUTES:Routes=[
    {path:'', component:AcessoComponent},
    { path: "home", component: HomeComponent, canActivate:[AutenticacaoGuard]},
    { path: "profile", component: ProfileComponent, canActivate:[AutenticacaoGuard]}



]

Profile.ts

export class ProfileComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

home.ts

export class HomeComponent implements OnInit {
  @ViewChild('publicacoes') public publicacoes:any
  constructor() { }

  ngOnInit() {

  }



  public atualizarTimeLine(){
    this.publicacoes.atualizarTimeLine()
  }
}

为什么会这样?

1 个答案:

答案 0 :(得分:2)

首先,创建一个组件,以防用户输入随机路径并将其命名为PageNotFound

第二,由于角度开始与输入的路径从上到下匹配,因此ROUTES数组的顺序很重要,

最后:考虑将您的ROUTE数组更改为:

export const ROUTES:Routes = [
  { path: "home", component: HomeComponent, canActivate:[AutenticacaoGuard]},
  { path: "profile", component: ProfileComponent, canActivate[AutenticacaoGuard]},
  { path: '',   redirectTo: '/home', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent },
]; 

以及我建议您使用Lazy Loading