简体   繁体   English

延迟加载模块中的角度延迟加载模块

[英]angular lazy load module in lazy loaded module

Can I lazy load module in lazy loaded module? 我可以在延迟加载模块中延迟加载模块吗?
I have such module for 'publications' route, but it loads too much functionality for editing this publication while user maybe don`t have permissions for this. 我有“发布”路径的这样的模块,但它加载了太多的功能来编辑这个出版物,而用户可能没有权限。 So I try to optimize it somehow, because I have similar components with 'edit/add' depending on permissions. 所以我尝试以某种方式优化它,因为我有类似的组件与'编辑/添加'取决于权限。
If you have suggestions for optimizing by permissions somehow, will be glad. 如果您有某些建议通过权限优化,将很高兴。

UPDATED Yes, you can. 更新是的,你可以。 Just import other module for route inside lazy loaded module 只需在延迟加载的模块中导入其他模块进行路由

FpgTimeComponent is not a part of the files of the lazy module that will be loaded, so you can't do that. FpgTimeComponent不是将要加载的延迟模块的文件的一部分,因此您不能这样做。 Think about it, you are trying to import a component in the lazy module that the module knows nothing about since it isn't defined in that module, and it isn't defined in any other module you have imported in the LazyModule . 考虑一下,您正在尝试在惰性模块中导入一个组件,模块一无所知,因为它未在该模块中定义,并且未在LazyModule导入的任何其他模块中定义。 So where is it going to get the component from? 那么从哪里获取组件?

Add FpgTimeComponent to a SharedModule and import the SharedModule in your LazyModule , or move the FpgTimeComponent to your LazyModule . FpgTimeComponent添加到SharedModule并导入SharedModule中的LazyModule ,或将FpgTimeComponent移动到LazyModule Once you do one of those, it should work then. 一旦你做了其中之一,它应该工作。

This is a better approach anyways, because I can guarantee as you keep developing you will keep running into the same error with other components/other lazy modules. 无论如何,这是一种更好的方法,因为我可以保证,随着您的不断开发,您将与其他组件/其他惰性模块保持同样的错误。 If you are using the components in multiple places, then they should live in a SharedModule as Angular best practices defines , and that SharedModule should be imported in all your lazy modules. 如果您在多个位置使用组件,那么它们应该像Angular最佳实践定义的那样存在于SharedModule中,并且应该在所有惰性模块中导入SharedModule

Here is an example. 这是一个例子。

SharedModule: SharedModule:

import { FpgTimeComponent } from './some/path';

@NgModule({
    declarations: [
        FpgTimeComponent
    ],
    exports: [
        FpgTimeComponent
    ]
})

LazyModule: LazyModule:

import { SharedModule } from '../shared/shared.module';

import { LazyComponent }   from './lazy.component';
import { routing } from './lazy.routing';

@NgModule({
    imports: [
        routing,
        SharedModule
    ],
    declarations: [
        LazyComponent
    ]
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM