【问题标题】:Page changed after refresh刷新后页面变了
【发布时间】:2019-04-21 19:47:48
【问题描述】:

我正在创建 Angular 6 的 Instagram 克隆,但我遇到了问题。 如果我在我的主页组件的页面中进行刷新,它会正确刷新以显示我的所有帖子,但是如果我在个人资料页面上进行刷新,它会自动将我重定向到主页而不是刷新并保留在个人资料页面上

刷新后它仍然可以正常工作

个人资料视图

刷新后

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]}



]

个人资料.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()
  }
}

为什么会这样?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    首先,创建一个组件以防用户输入随机路径并将其命名为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

    【讨论】:

    • 问题是,当用户通过身份验证并进行刷新(路径:'')时,它应该被重定向到主页,但是如果用户没有经过身份验证,他应该被重定向到 accessoComponent(我有登录表单)
    • 我按照你说的做了,但是当我写localhost:4200/jhgdkjshadl 之类的东西时,它会将我重定向到 PageNotFoundComponent,几秒钟后它会再次自动将我重定向到主组件,我不知道为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2012-02-08
    • 1970-01-01
    • 2021-10-24
    • 2013-08-26
    相关资源
    最近更新 更多