加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

router.navigate不起作用(Angular6,延迟加载)

发布时间:2020-12-17 18:06:52 所属栏目:安全 来源:网络整理
导读:我是Angular 4的新手(目前正在使用v.6).我一直在尝试使用this.router.navigate([‘/ landing’])函数从登录组件重定向到登陆组件,它无法正常工作. 它将显示登录页面一秒钟,然后再次重定向回登录页面. 但是,如果我尝试浏览正常链接,例如, a routerLink="/land
我是Angular 4的新手(目前正在使用v.6).我一直在尝试使用this.router.navigate([‘/ landing’])函数从登录组件重定向到登陆组件,它无法正常工作.
它将显示登录页面一秒钟,然后再次重定向回登录页面.

但是,如果我尝试浏览正常链接,例如,

<a routerLink="/landing">landing</a>

它工作正常.

这是我的app-routing.module.ts

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

const routes: Routes = [
  {
    path: 'login',loadChildren: './login/login.module#LoginModule'
  },{
    path: 'landing',loadChildren: './landing/landing.module#LandingModule'
  },{
    path: '',redirectTo: 'landing',pathMatch: 'full'
  },{
    path: '**',pathMatch: 'full'
  }
];

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

这是登录组件中的代码,

export class LoginComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() {
  }

  login(){
    this.router.navigate(['/landing']);
  }
  
}

这是landing-routing.module.ts中的代码

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

import { LandingComponent } from './landing.component';

const routes: Routes = [
  {
    path:'',component: LandingComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],exports: [RouterModule]
})
export class LandingRoutingModule { }

谢谢你的帮助.

解决方法

我遇到了类似的问题,我是如何解决它的:

在Component.ts中

constructor(private router: Router,private route: ActivatedRoute){}

this.router.navigate(['../landing'],{relativeTo: this.route})

试试这个,让我知道它是否有帮助!!!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读