Angular 8 – 如何将惰性模块加载到选项卡中

huangapple go评论82阅读模式
英文:

Angular 8 - How to load lazy modules into tabs

问题

我正在尝试使用延迟加载功能模块的选项卡。以下是我的代码:

主路由:

export const AppRoutes: Routes = [{
    path: '',
    redirectTo: 'home',
    pathMatch: 'full',
  },
  { path: 'login', component: LoginComponent },
  {
    path: '',
    component: DefaultLayoutComponent,
    children: [
    {
      path: 'home', canActivate: [AuthGuard],
      loadChildren: './views/home/home.module#HomeModule'
    },
    {
      path: 'settings',
      loadChildren: './views/settings/settings.module#SettingsModule'
    },
  ]}
];

settings.module:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { SettingsComponent } from './settings.component';
import { SettingsRoutingModule } from './settings-routing.module';

@NgModule({
  imports: [
    SettingsRoutingModule
  ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
  declarations: [ SettingsComponent ]
})

export class SettingsModule { }

settings-routing.module:

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

import { SettingsComponent } from './settings.component';

const routes: Routes = [
  {
    path: '',
    component: SettingsComponent,
    data: {
      title: 'Settings'
    },
    children: [
      { path: 'syspref', loadChildren: './systempreferences/systempreferences.module#SystempreferencesModule' },
      { path: 'userpref', loadChildren: './userpreferences/userpreferences.module#UserpreferencesModule' },
    ]
  }
];

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

export class SettingsRoutingModule {}

settings.component.html:

<section id="tabs">
	<div class="container">
		<div class="row">
			<div class="col-xs-12" style="width: 100vw;">
				<nav>
					<div class="nav nav-tabs nav-fill" id="nav-tab" role="tablist">
            <a class="nav-item nav-link" id="nav-syspref-tab" data-toggle="tab" href="#" role="tab" aria-controls="nav-syspref"
              aria-selected="false" routerLink="syspref">Default Preferences</a>
            <a class="nav-item nav-link" id="nav-userpref-tab" data-toggle="tab" href="#" role="tab" aria-controls="nav-userpref"
              aria-selected="false" routerLink="userpref">User Preferences</a>
					</div>
          </nav>
				<div class="tab-content py-3 px-3 px-sm-0" id="nav-tabContent">
					<div class="tab-pane fade show active" id="nav-syspref" role="tabpanel" aria-labelledby="nav-syspref-tab">
					</div>
					<div class="tab-pane fade" id="nav-userpref" role="tabpanel" aria-labelledby="nav-userpref-tab">
					</div>
				</div>

			</div>
		</div>
	</div>
</section>
<router-outlet></router-outlet>

systempreferences.module:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SystempreferencesComponent } from './systempreferences.component';

@NgModule({
  imports: [
    CommonModule
  ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
  declarations: [SystempreferencesComponent]
})

export class SystempreferencesModule { }

我的期望是当我选择一个选项卡时,例如选择"SystempreferencesModule",它会被加载并显示"SystempreferencesComponent"组件,但这并没有发生。我是不是搞错了什么?

谢谢!

英文:

I am trying have tabs that would lazy load feature modules. Here is my code:

Main router:

export const AppRoutes: Routes = [{
    path: &#39;&#39;,
    redirectTo: &#39;home&#39;,
    pathMatch: &#39;full&#39;,
  },
  { path: &#39;login&#39;, component: LoginComponent },
  {
    path: &#39;&#39;,
    component: DefaultLayoutComponent,
    children: [
    {
      path: &#39;home&#39;, canActivate: [AuthGuard],
      loadChildren: &#39;./views/home/home.module#HomeModule&#39;
    },
    {
      path: &#39;settings&#39;,
      loadChildren: &#39;./views/settings/settings.module#SettingsModule&#39;
    },
  ]}
];

settings.module:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from &#39;@angular/core&#39;;
import { SettingsComponent } from &#39;./settings.component&#39;;
import { SettingsRoutingModule } from &#39;./settings-routing.module&#39;;

@NgModule({
  imports: [
    SettingsRoutingModule
  ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
  declarations: [ SettingsComponent ]
})

export class SettingsModule { }

settings-routing.module:

import { NgModule } from &#39;@angular/core&#39;;
import { Routes, RouterModule } from &#39;@angular/router&#39;;

import { SettingsComponent } from &#39;./settings.component&#39;;

const routes: Routes = [
  {
    path: &#39;&#39;,
    component: SettingsComponent,
    data: {
      title: &#39;Settings&#39;
    },
    children: [
      { path: &#39;syspref&#39;, loadChildren: &#39;./systempreferences/systempreferences.module#SystempreferencesModule&#39; },
      { path: &#39;userpref&#39;, loadChildren: &#39;./userpreferences/userpreferences.module#UserpreferencesModule&#39; },
    ]
  }
];

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

settings.component.html

&lt;section id=&quot;tabs&quot;&gt;
	&lt;div class=&quot;container&quot;&gt;
		&lt;div class=&quot;row&quot;&gt;
			&lt;div class=&quot;col-xs-12&quot; style=&quot;width: 100vw;&quot;&gt;
				&lt;nav&gt;
					&lt;div class=&quot;nav nav-tabs nav-fill&quot; id=&quot;nav-tab&quot; role=&quot;tablist&quot;&gt;
            &lt;a class=&quot;nav-item nav-link&quot; id=&quot;nav-syspref-tab&quot; data-toggle=&quot;tab&quot; href=&quot;#&quot; role=&quot;tab&quot; aria-controls=&quot;nav-syspref&quot;
              aria-selected=&quot;false&quot; routerLink=&quot;syspref&quot;&gt;Default Preferences&lt;/a&gt;
            &lt;a class=&quot;nav-item nav-link&quot; id=&quot;nav-userpref-tab&quot; data-toggle=&quot;tab&quot; href=&quot;#&quot; role=&quot;tab&quot; aria-controls=&quot;nav-userpref&quot;
              aria-selected=&quot;false&quot; routerLink=&quot;userpref&quot;&gt;User Preferences&lt;/a&gt;
					&lt;/div&gt;
        &lt;/nav&gt;
				&lt;div class=&quot;tab-content py-3 px-3 px-sm-0&quot; id=&quot;nav-tabContent&quot;&gt;
					&lt;div class=&quot;tab-pane fade show active&quot; id=&quot;nav-syspref&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;nav-syspref-tab&quot;&gt;
					&lt;/div&gt;
					&lt;div class=&quot;tab-pane fade&quot; id=&quot;nav-userpref&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;nav-userpref-tab&quot;&gt;
					&lt;/div&gt;
				&lt;/div&gt;

			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/section&gt;
&lt;router-outlet&gt;&lt;/router-outlet&gt;

systempreferences.module

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { SystempreferencesComponent } from &#39;./systempreferences.component&#39;;

@NgModule({
  imports: [
    CommonModule
  ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
  declarations: [SystempreferencesComponent]
})
export class SystempreferencesModule { }

My expectation was when I select a tab I will get a module such as SystempreferencesModule loaded and the SystempreferencesComponent component would show up, but that's not happening. Am I getting it all wrong?

Thanks

答案1

得分: 1

I think you are using the old way of setting up lazy loaded routes. This was changed in Angular 7 or 8 (I think...). New way should be like this

{
  "path": "syspref",
  "loadChildren": () => import('./systempreferences/systempreferences.module').then(m =>
    m.SystempreferencesModule)
}

Also in your SystemPreferences module you need a route like this:

const routes: Routes = [
  {
    "path": "",
    "component": SystempreferencesComponent
  }
];

You can just add the routes variable at the top of your System Preferences module and add the below to your imports array (just slightly easier than setting up a routing module... functionally the same):

RouterModule.forChild(routes)
英文:

I think you are using the old way of setting up lazy loaded routes. This was changed in Angular 7 or 8 (I think...). New way should be like this

{
	path: &#39;syspref&#39;,
	loadChildren: () =&gt; import(&#39;./systempreferences/systempreferences.module&#39;).then(m =&gt; 
    m.SystempreferencesModule)
},

Also in your SystemPreferences module you need a route like this:

const routes: Routes = [
  {
    path: &#39;&#39;,
    component: SystempreferencesComponent,
  }
];

You can just add the routes variable at the top of your System Preferences module and add the below to your imports array (just slightly easier than setting up a routing module... functionally the same):

RouterModule.forChild(routes)

huangapple
  • 本文由 发表于 2020年1月7日 02:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定