简体   繁体   English

如何在Angular 7模块设置中延迟加载模态

[英]How to lazy load modals in Angular 7 module setup

in my previous project we were using one big "app module" that imported everything (components, pipes, directives, pages) at the start of the app. 在我之前的项目中,我们使用了一个大的“app模块”,它在应用程序的开头导入了所有内容(组件,管道,指令,页面)。

This is obviously not the best structure because as the app grows it gets slower. 这显然不是最好的结构,因为随着应用程序的增长,它变慢。

We recently switched to Angular 7 using Ionic 4, and have lazy loading set up on the routes with a module for each page now. 我们最近使用Ionic 4切换到Angular 7,并且在路由上设置了延迟加载,现在每个页面都有一个模块。 The Page Modules imports the Components Module 页面模块导入组件模块

This works great... 这很棒......

The problem is when we get into modals that pop up over the page. 问题是当我们进入页面弹出的模态时。

Note: I'm going to bold the word "modal" so as not to get confused with "module". 注意:我要加粗“模态”这个词,以免与“模块”混淆。

A button on a component may pop open a modal . 组件上的按钮可以弹出一个模态 So the component needs to import the modal module. 因此组件需要导入模态模块。 The modal needs access to components so it imports the Components module. 模态需要访问组件,以便导入组件模块。 This causes a circular dependency. 这会导致循环依赖。

What is the proper way for importing modals in a modular setup with lazy loading? 在延迟加载的模块化设置中导入模态的正确方法是什么?

I guess there are multiple ways to avoid that circular dependency issue depending on your specific project, but one way could be the following: instead of being the component the one who opens the modal, that component could tell the parent page to actually open the modal (by emitting an event to the parent page when the button is clicked). 我想有多种方法可以避免这种循环依赖问题,具体取决于你的特定项目,但有一种方法可能如下: 该组件可以告诉父页面实际打开模态,而不是打开模态的组件。 (通过在单击按钮时向父页面发出事件)。 That way the parent page would import the modal module (which could make sense as well in the context of your app for example, if the parent page is a list of pets and the modal shows the details of each pet). 这样,父页面将导入模态模块(例如,如果父页面是宠物列表并且模态显示每个宠物的详细信息,那么在应用程序的上下文中也可能有意义)。

I'm debating between going with that solution or just giving every component it's own module and only importing the components I'm actually using into each other component/page/modal to kill the circular dependency. 我正在讨论使用该解决方案或只是为每个组件提供它自己的模块,并且只将我实际使用的组件导入到每个其他组件/页面/模式中以消除循环依赖关系。 Maybe having a “core” components module for all the basic things that don't have many dependencies 也许有一个“核心”组件模块,用于所有没有很多依赖关系的基本事物

I'm glad that you mentioned that approach because that's exactly what I like to do in the projects I work on. 我很高兴您提到了这种方法,因为这正是我喜欢在我工作的项目中所做的事情。 I usually prefer each component to have its own module to have control over which components are imported on each page . 我通常更喜欢每个组件都有自己的模块来控制每个页面上导入的组件

It usually happens that there's a "core" components module that is imported everywhere, but the problem is that some other developers may add components there because they are used in more than one page, but that results in those components being imported in ten or more pages even if those pages only use one or two components from that module. 通常情况下,会有一个“核心”组件模块被导入到任何地方,但问题是其他一些开发人员可能会在那里添加组件,因为它们在多个页面中使用,但这会导致这些组件被导入十个或更多页面,即使这些页面仅使用该模块中的一个或两个组件。

It depends on how big your project is, how many components are there and how often they are imported by some other pages, but when talking about mobile apps (specially hybrid mobile apps) I think the most important thing is to be able to load pages as fast as possible, so importing only the components that each page actually needs sounds like the right way to go. 这取决于您的项目有多大,有多少组件以及其他页面导入的频率,但在谈论移动应用程序(特别是混合移动应用程序)时,我认为最重要的是能够加载页面尽可能快,因此只导入每个页面实际需要的组件听起来像是正确的方式。

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

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