我正在尝试实现延迟加载并尝试延迟加载登录组件,但是当单击登录链接时,出现异常“无法匹配任何路由”。
//app.component.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Register / Login</a>
</div>
<ul class="nav navbar-nav">
<li><a routerLink="signin" routerLinkActive="nav-link active">Sign In</a></li>
<li><a>Sign Up</a></li>
<li><a routerLink="help" routerLinkActive="nav-link active">Help</a></li>
</ul>
</div>
</nav>
<div>
</div>
<router-outlet></router-outlet>
//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'
import { AppComponent } from './app.component';
import { HelpComponent } from './help/help.component';
const route: Routes = [
{
path: '', component: HelpComponent
},
{
path: 'lazymodule', loadChildren: './login-register/login-register.module#LoginRegisterModule'
},
{
path: 'help', component: HelpComponent
}
];
@NgModule({
declarations: [
AppComponent,
HelpComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(route)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
//login-register.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SignInComponent } from './sign-in/sign-in.component';
import { FormsModule } from '../../../node_modules/@angular/forms';
import { Routes, RouterModule } from '../../../node_modules/@angular/router';
const route: Routes = [
{path: 'signin', component: SignInComponent}
]
@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule.forChild(route)
],
declarations: [SignInComponent]
})
export class LoginRegisterModule { }
登录组件位于 app-> login-register->登录文件夹中。请指教。
答案 0 :(得分:1)
使用粗箭头功能,则不必担心模块的路径
例如
{ path: 'lazymodule', loadChildren: () => LoginRegisterModule }
和您的 routerLink 应该是
<li><a routerLink="/lazymodule/signin" routerLinkActive="nav-link active">Sign In</a></li>
答案 1 :(得分:0)
angular-cli1.7.1对于在字符串中懒惰laoding有问题
{path:'user-panel',loadChildren:'./user-panel/user-panel.module#UserPanelModule',},->会给错误一些时间。
到 {path:'user-panel',loadChildren:()=> UserPanelModule,},
从routing.ts中的“ /模块路径”导入{UserPanelModule} 将延迟加载到预加载器模块中->将模块作为组件级别加载-将模块作为组件加载。
答案 2 :(得分:0)
您的路线不正确。 试试这个:
//app.component.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Register / Login</a>
</div>
<ul class="nav navbar-nav">
<li><a routerLink="lazymodule/signin" routerLinkActive="nav-link active">Sign In</a></li>
<li><a>Sign Up</a></li>
<li><a routerLink="help" routerLinkActive="nav-link active">Help</a></li>
</ul>
</div>
</nav>
<div>
</div>
<router-outlet></router-outlet>
答案 3 :(得分:-1)
您可以这样做
//app.component.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Register / Login</a>
</div>
<ul class="nav navbar-nav">
<li><a routerLink="lazymodule/signin" routerLinkActive="nav-link active">Sign In</a></li>
<li><a>Sign Up</a></li>
<li><a routerLink="help" routerLinkActive="nav-link active">Help</a></li>
</ul>
</div>
</nav>
<div>
</div>
<router-outlet></router-outlet>
// login-register-module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SignInComponent } from './sign-in/sign-in.component';
import {FormsModule} from '@angular/forms';
import {Routes,RouterModule} from '@angular/router';
const route: Routes = [
{path: 'signin', component: SignInComponent}
]
@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule.forChild(route)
],
declarations: [SignInComponent]
})
export class LoginRegisterModule { }
// app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'
import { AppComponent } from './app.component';
import { HelpComponent } from './help/help.component';
import { LoginRegisterModule } from '../app/login-register/login-register.module';
const route: Routes = [
{
path: '', component: HelpComponent
},
{
path: 'lazymodule', loadChildren: () => LoginRegisterModule
},
{
path: 'help', component: HelpComponent
}
];
@NgModule({
declarations: [
AppComponent,
HelpComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(route)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }