我的angular 6应用程序中有两个模块,一个是app模块,另一个是User模块,我正在尝试实现用户模块的延迟加载。如果我手动点击URL,则路由有效,然后将显示来自用户模块的正确页面。但是,当我尝试从标题组件导航时,它并没有导航到用户页面,而是转到了默认页面。
Header.html
<form class="form-inline my-2 my-lg-0">
<button class="btn btn-secondary my-2 my-sm-0" type="submit" routerLink="/user/login">Login</button>
<button class="btn btn-secondary my-2 my-sm-0" type="submit" routerLink="/user/register">Register</button>
</form>
应用程序路由模块
import { NgModule, Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { WelcomeComponent } from './components/welcome/welcome.component';
import { PagenotfoundComponent } from './components/pagenotfound/pagenotfound.component';
const routes: Routes = [
{
path:'welcome',
component:WelcomeComponent
},
{
path:'user',
loadChildren:'./user/user.module#UserModule'
},
{
path: '',
redirectTo: 'welcome',
pathMatch: 'full' },
{
path: '**',
component: PagenotfoundComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
用户路由模块
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './components/login/login.component';
import { RegisterComponent } from './components/register/register.component';
const routes: Routes = [
{
path:'login',
component: LoginComponent
},
{
path:'register',
component:RegisterComponent
},
{
path:'',
redirectTo:'login',
pathMatch:'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UserRoutingModule { }
应用程序模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
import { WelcomeComponent } from './components/welcome/welcome.component';
import { PagenotfoundComponent } from './components/pagenotfound/pagenotfound.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
WelcomeComponent,
PagenotfoundComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
因此,当我单击登录按钮时,我可以看到登录页面,但是立即将其重定向到欢迎页面(http://localhost:4200/welcome)
答案 0 :(得分:2)
我认为您只是将router-outlet
映射到错误的组件中-请检查
我在stackblitz中尝试了您的相同情况,请看一下
app.component.html
<form class="form-inline my-2 my-lg-0">
<button class="btn btn-secondary my-2 my-sm-0" type="submit" routerLink="/user/login">Login</button>
<button class="btn btn-secondary my-2 my-sm-0" type="submit" routerLink="/user/register">Register</button>
</form>
<router-outlet></router-outlet>
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes} from '@angular/router';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
const routes: Routes = [
{
path:'welcome',
component:HelloComponent
},
{
path:'user',
loadChildren:'./modules/lazy/lazy.module#LazyModule'
},
{
path: '',
redirectTo: 'welcome',
pathMatch: 'full'
}
];
@NgModule({
imports: [ BrowserModule, FormsModule, RouterModule.forRoot(routes) ],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
一切都按预期进行-请查看上面的链接-希望对您有所帮助-编码愉快:)
更新
最后,找到并解决问题花费了很多时间-但问题似乎很小,无法占用我们的时间:)-是的,问题是一个button
标签,类型为submit
并且太包裹在form
标记内-这会导致问题并导致页面重新加载,因为它假定表单已提交,并且使应用component
返回
因此,第一个解决方法是您的header.component.html
,或者删除form
标签,或者将button
类型更改为type="button"
(如果未将其更改为anchor
标签)
<form class="form-inline my-2 my-lg-0">
<button class="btn btn-secondary my-2 my-sm-0" type="button" [routerLink]="['user']">Login</button>
<button class="btn btn-secondary my-2 my-sm-0" type="button" routerLink="/user/register">Register</button>
</form>
这将完全解决您的问题-剩下的就是需要根据您的方便进行修复的
app-routing.module.ts
请勿在此处进行引导,引导程序应仅位于app.module
上,因此路由@NgModule()
应该像这样
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule { }
user-routing.module.ts
您需要导出RouteModule
并从LazyModule
中读取,因此路由@NgModule()
应该是这样的
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
export class UserRoutingModule { }
所有这些都可以修复所有问题,并且您的代码可以完美运行-随时与您分享您的疑虑-祝您编程愉快:)