Javascript 触发模块延迟加载手动角度7
官方文档中有很多关于如何延迟加载角度模块的信息 这基本上会在用户访问Javascript 触发模块延迟加载手动角度7,javascript,angular,lazy-loading,Javascript,Angular,Lazy Loading,官方文档中有很多关于如何延迟加载角度模块的信息 这基本上会在用户访问/客户或/订单路线时加载模块 但是,我不知道如何从另一个模块加载模块 在我的应用程序中,我有以下模块: 认证 核心 事件 即时消息 我的身份验证模块(配置文件页)的一条路径必须使用事件模块中的ngrx存储 我的代码如下所示: import { Observable } from 'rxjs'; import { Component, OnInit } from '@angular/core'; import { Store
/客户
或/订单
路线时加载模块
但是,我不知道如何从另一个模块加载模块
在我的应用程序中,我有以下模块:
- 认证
- 核心
- 事件
- 即时消息
身份验证模块
(配置文件页)的一条路径必须使用事件模块
中的ngrx存储
我的代码如下所示:
import { Observable } from 'rxjs';
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '../../app.store';
import { IUser } from '../auth.api.service';
import { selectUser } from '../store/auth.selectors';
import { IEvent } from '../../events/events.api.service';
import { selectAllEvents, selectIsLoading } from '../../events/store/events.selectors';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.scss'],
})
export class ProfileComponent implements OnInit {
isLoading$: Observable<boolean>;
events$: Observable<IEvent[]>;
user$: Observable<IUser>;
constructor(
private store: Store<AppState>,
) {
this.user$ = this.store.select(selectUser);
this.isLoading$ = this.store.select(selectIsLoading);
this.events$ = this.store.select(selectAllEvents);
}
ngOnInit() {
}
}
Angular CLI bundler基于两个方面对模块进行捆绑: 1) 如果将模块设置为延迟加载(
loadChildren
),它将单独捆绑模块并延迟提供
2) 但是,如果在任何其他模块中存在对延迟加载模块的任何引用(通过将其添加到其imports
数组中),则它会将该模块与引用的组件捆绑在一起
所以应该发生的是,如果您的事件模块是从组件引用的,那么它应该与该组件捆绑在一起
对于包含引用它的组件的模块,是否在imports
数组中引用了该模块
你到底犯了什么错误
顺便说一句-我在本演讲的“延迟加载”部分介绍了这一点:Angular CLI捆绑程序基于两个方面打包模块: 1) 如果将模块设置为延迟加载(
loadChildren
),它将单独捆绑模块并延迟提供
2) 但是,如果在任何其他模块中存在对延迟加载模块的任何引用(通过将其添加到其imports
数组中),则它会将该模块与引用的组件捆绑在一起
所以应该发生的是,如果您的事件模块是从组件引用的,那么它应该与该组件捆绑在一起
对于包含引用它的组件的模块,是否在imports
数组中引用了该模块
你到底犯了什么错误
顺便说一句-我在本演讲的“延迟加载”部分介绍了这一点:您不必担心加载
。/../events
。因为您有import语句,所以类/接口将在模块中可用。如果出于某种原因,您想使用其他模块的功能,可以在@NgModule
声明的导入
数组中添加模块名称。您不必担心加载。/../events
。因为您有import语句,所以类/接口将在模块中可用。如果出于某种原因,您想使用其他模块的功能,您可以在@NgModule
声明的imports
数组中添加模块名称。请使用您收到的任何错误进行更新,并尽可能创建stackblitz。请使用您收到的任何错误进行更新,并尽可能创建stackblitz。
import { Observable } from 'rxjs';
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '../../app.store';
import { IUser } from '../auth.api.service';
import { selectUser } from '../store/auth.selectors';
import { IEvent } from '../../events/events.api.service';
import { selectAllEvents, selectIsLoading } from '../../events/store/events.selectors';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.scss'],
})
export class ProfileComponent implements OnInit {
isLoading$: Observable<boolean>;
events$: Observable<IEvent[]>;
user$: Observable<IUser>;
constructor(
private store: Store<AppState>,
) {
this.user$ = this.store.select(selectUser);
this.isLoading$ = this.store.select(selectIsLoading);
this.events$ = this.store.select(selectAllEvents);
}
ngOnInit() {
}
}
constructor(
private store: Store<AppState>,
) {
this.user$ = this.store.select(selectUser);
this.loadModule('../../events/events.module.ts').then(() => {
this.isLoading$ = this.store.select(selectIsLoading);
this.events$ = this.store.select(selectAllEvents);
})
}