router.navigate is not working (Angular6, lazy loading)

前端 未结 3 2390
时光说笑
时光说笑 2021-02-20 11:48

I\'m a newbie for Angular 4+ (currently using v.6). I\'ve been trying to use this.router.navigate([\'/landing\']) function to redirect from login component to l

相关标签:
3条回答
  • 2021-02-20 12:14

    try change this:

    {
      path: 'login',
      loadChildren: './login/login.module#LoginModule'
    }
    

    to this:

    {
      path: 'login',
      loadChildren: () => LoginModule
    }
    
    0 讨论(0)
  • 2021-02-20 12:30

    But my best solution is this:

    add to constructor:

    constructor(private readonly loader: NgModuleFactoryLoader)
    

    then only after loading module itself you should call navigate:

     this.loader.load(./login/login.module#LoginModule)
                    .then(factory => {
                        this.router.navigate(['/landing']);
                    });
    
    0 讨论(0)
  • 2021-02-20 12:34

    I faced similar issue, how I resolved it:

    In Component.ts

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

    try this and let me know if it helps!!!

    0 讨论(0)
提交回复
热议问题