简体   繁体   English

未捕获(承诺):错误:无法匹配任何路线。 URL 段:Angular 中的“布局”

[英]Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'layout' in Angular

I am making a normal login with username and password admin & admin.In login component navigate to layout.But i am getting an error like "core.js:1448 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'layout'".please help me..我正在使用用户名和密码 admin 和 admin 进行正常登录。在登录组件中导航到布局。但我收到类似“core.js:1448 错误错误:未捕获(承诺):错误:无法匹配任何路由。 URL Segment: 'layout'"。请帮帮我..

app.routing.module.ts app.routing.module.ts

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


@NgModule({
    imports: [
        RouterModule.forRoot([
            {path: '', redirectTo: '/login', pathMatch: 'full'},
            {path: 'login', loadChildren: 'app/login/login.module#LoginModule'}

        ])
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule {
}

my login.component.ts is我的 login.component.ts 是

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { AuthenticationService } from '../../services/authentication.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  username:string;
  password:string;

  constructor(

    public authService: AuthenticationService,
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {

  }

  login(){
    if(this.authService.login(this.username, this.password)){
      this.router.navigate(['/layout']);
    }
  }
}

my login.routing.module.ts is我的 login.routing.module.ts 是

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { LayoutComponent } from '../layout/layout/layout.component';
import { LayoutRoutingModule } from '../layout/layout-routing.module';

const routes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'layout', component: LayoutComponent }




];
@NgModule({
imports: [RouterModule.forChild(routes)],

  exports: [RouterModule]
})
export class LoginRoutingModule { }

Extending on what JB Nizet said in your comment, when you have a module that is lazy loaded, the paths inside of that lazy loaded module are relative.扩展 JB Nizet 在您的评论中所说的内容,当您有一个延迟加载的模块时,该延迟加载模块内部的路径是相对的。 If there is any path before the lazy load, that path is pre-pended to ALL of the paths in the lazy loaded module.如果在延迟加载之前有任何路径,则该路径将被添加到延迟加载模块中的所有路径之前。

So in this case, here is how your paths map:因此,在这种情况下,您的路径映射如下:

path: ''路径:''
loaded component: LoginComponent加载组件:LoginComponent
why: redirects to '/login'.为什么:重定向到“/登录”。 See login below for more information.有关更多信息,请参阅下面的登录。

path : '/login'路径:'/登录'
loaded component: LoginComponent加载组件:LoginComponent
why: lazy loads the LoginModule, where is matches the path of '' and thus loads the LoginComponent为什么:lazy 加载 LoginModule,其中 is 匹配 '' 的路径,从而加载 LoginComponent

path: '/login/layout'路径:'/登录/布局'
loaded component: LayoutComponent加载的组件:LayoutComponent
why: lazy loads the LoginModule, where is matches the path of 'layout' and thus loads the LayoutComponent为什么:lazy 加载 LoginModule,其中 is 匹配 'layout' 的路径,从而加载 LayoutComponent

path: '/layout'路径:'/布局'
loaded component: NONE加载的组件:无
why: as this is not prepended with 'login' it does not lazy load the LoginModule, and as such is just looking in the app.routing, where the only valid paths are '' and '/login'为什么:因为这没有以“登录”为前缀,所以它不会延迟加载 LoginModule,因此只是在 app.routing 中查找,其中唯一的有效路径是 '' 和 '/login'

The issue comes up to me after I try to implement lazy loading.在我尝试实现延迟加载后,我想到了这个问题。

  1. Create SpecificModule.routing.module.ts file创建SpecificModule.routing.module.ts 文件

    const routes: Routes = [ { path: "", component: AComponent, canActivate: [AuthGuard]}, { path: "newmessage", component: BComponent, canActivate: [AuthGuard], }, { path: "editmessage/:MessageId/:Mode", component: NewmessageComponent, canActivate: [AuthGuard] }, ]; const 路由:Routes = [ { path: "", component: AComponent, canActivate: [AuthGuard]}, { path: "newmessage", component: BComponent, canActivate: [AuthGuard], }, { path: "editmessage/:MessageId /:Mode", 组件: NewmessageComponent, canActivate: [AuthGuard] }, ];

    @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) @NgModule({ 导入:[RouterModule.forChild(routes)],导出:[RouterModule] })

  2. Import SpecificModuleRoutingModule to SpecificModule将SpecificModuleRoutingModule 导入SpecificModule

  3. Add entries for SpecificModuleRouting to AppRoutingModule.将特定模块路由的条目添加到 AppRoutingModule。

    import { NgModule } from "@angular/core";从“@angular/core”导入 { NgModule }; import { Routes, RouterModule } from "@angular/router";从“@angular/router”导入 { Routes, RouterModule }; import { AuthGuard } from "./shared/services/auth.guard";从“./shared/services/auth.guard”导入 { AuthGuard };

    const routes: Routes = [ { path: "specificmodule", loadChildren: () => import("./specificmodule/specificmodule.module").then(d => d.SpecificModule), canActivate: [AuthGuard] }, ]; const 路由:Routes = [ { path: "specificmodule", loadChildren: () => import("./specificmodule/specificmodule.module").then(d => d.SpecificModule), canActivate: [AuthGuard] }, ] ;

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

  4. Import SpecificModule to AppModule [This was missing which caused the issue]将特定模块导入 AppModule [这是导致问题的缺失]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 测试:未捕获错误:未捕获(承诺中):错误:无法匹配任何路由。 URL 段:“家” - Angular Testing: Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home' 角单元测试:未捕获错误:未捕获(承诺中):错误:无法匹配任何路线。 网址段:“注销” - Angular Unit Test: Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'logout' NativeScript Angular:错误错误:未捕获(承诺):错误:无法匹配任何路由。 网址段:“ add Patient” - NativeScript Angular: ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'addpatient' ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。 Angular 中的 URL 段 13 - ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment in Angular 13 Electron + Angular 应用程序 + 错误:未捕获(承诺):错误:无法匹配任何路由。 URL 段: - Electron + Angular Application + Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: Ionic 5(Angular9)-错误:未捕获(承诺):错误:无法匹配任何路由。 URL 段:'profile' - Ionic 5(Angular9)-Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'profile' 角路由错误:未捕获(承诺):错误:无法匹配任何路由。 网址段:“仪表板/配置文件” - Angular Routing Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'dashboard/profile' 失败:未捕获(承诺):错误:无法匹配任何路由。 URL段:“登录”。 茉莉花&角6 - Failed: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login'. Jasmine & Angular 6 角度2命名的出口:未捕获(承诺):错误:无法匹配任何路线。 网址段: - Angular 2 Named Outlet: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 未捕获(承诺):错误:无法匹配任何路线。 URL 段:角度 - Uncaught (in promise): Error: Cannot match any routes. URL Segment: Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM