Ionic 3 - 每个页面都需要自己的模块吗? [英] Ionic 3 - Does each Page need its own Module?

查看:13
本文介绍了Ionic 3 - 每个页面都需要自己的模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ionic 的新手,并试图了解如何组织一个中型应用程序.

I am new to Ionic and trying to understand how to organize a medium sized application.

每个页面都应该有自己的模块吗?还是将相似的页面分组到同一个模块中更好?甚至有可能做到这一点吗?看来 IonicPageModule.forChild() 方法只接受一个页面:

Should each Page have its own Module? Or is it better to group similar Pages into the same Module? Is it even possible to do that? It appears the IonicPageModule.forChild() method only accepts one Page:

    IonicPageModule.forChild(AdministerUsersPage)

那么每个页面都有自己的模块可以吗?拥有这么多模块有什么缺点吗?

So is it okay for every single Page to have its own Module? Are there any drawbacks to having this many modules?

似乎我们从一个极端走向了另一个极端——没有在根上下文中声明所有内容的模块来分隔每个页面的模块.

It seems like we went from one extreme to the other - no modules where everything is declared in the root context to separate modules for every single Page.

推荐答案

每个页面都需要自己的模块吗?

Does each Page need its own Module?

您的问题的答案是:.

The answer to your question would be: Yes and no.

确实如果您希望页面延迟加载,则每个页面都需要一个模块.您可以在以下两个链接中找到有关 Ionic 中延迟加载的更多信息:

You do need a module per page if you want that page to be lazy-loaded. You can find more information about lazy-loading in Ionic in these two links:

基本上,该模块将封装组件(您的页面)运行所需的所有内容,以便您可以在您的应用程序中需要它时创建它.例如,您可以预加载一些用户在打开您的应用程序后会立即使用的页面,然后保留一些其他页面,仅当用户尝试导航到它们时才加载.由于您在打开应用时并未加载所有页面,因此启动时间会更好.

Basically that module will encapsulate everything that the component (your page) needs to function so it can be created when you need it in your app. For example, you could preload some pages that users will use a soon as they open your app, and then keep some other pages to be loaded only if users try to navigate to them. Since you're not loading all the pages when your app is opened, the startup time will be better.

为每个页面使用模块也与深度链接有关.您可以查看文档以了解更多信息有关深度链接和 Ionic 的信息,但基本上幕后的想法与延迟加载功能几乎相同.

Using modules for each page is also related to deep-linking. You can take a look at the docs to find more information about deep-linking and Ionic, but basically the idea behind the scenes is pretty much the same as with the lazy-loading feature.

或者将相似的页面分组到同一个模块中更好吗?甚至有可能做到这一点吗?

Or is it better to group similar Pages into the same Module? Is it even possible to do that?

Afaik 在 Ionic 中是不可能的,当然这不是推荐的使用方式.

Afaik that's not possible in Ionic, and of course it's not the recommended way to use it.

那么每个页面都有自己的模块可以吗?拥有这么多模块有什么缺点吗?

So is it okay for every single Page to have its own Module? Are there any drawbacks to having this many modules?

没错.Ionic 通过为每个页面创建一个模块来实现延迟加载功能.一个小缺点是您需要为每个页面创建一个模块.因此,如果您的应用有 20 多个页面,则需要创建 20 多个模块(但我想这不是一个大问题,CLI 为我们完成了所有工作,对吧?).

That's correct. Ionic implemented the lazy-loading feature by creating a module per page. One minor drawback would be that you need to create a module for each and every page. So if you have 20+ pages on your app, you'd need to create 20+ modules (but I guess that's not a big issue though, the CLI does all the job for us, right?).

一个更重要的问题是,如果您有一个 ComponentsModule 包装,比如说,10 个组件,然后您在每个 PageModule 上导入该 ComponentModule.这将创建大量代码重复(每个PageModule 将获得整个ComponentsModule 的不同副本).解决此问题的一种方法是为每个组件创建一个模块,以便每个 PageModule 可以准确导入它需要的内容.

A more important issue would be if you have a ComponentsModule that wraps, let's say, 10 components on it and then you import that ComponentModule on each PageModule. This will create a lot of code duplication (each PageModule will get a different copy of the entire ComponentsModule). One way to fix this issue would be to create a module per component, so that each PageModule can import exactly what it needs.

但这仍然不是什么大问题;捆绑包会更大一些,但我想更快的启动是任何移动应用程序最重要的功能之一.

But still this is not a big issue; the bundle would be a little bit bigger but I guess that a faster startup is one of the most important features of any mobile app.

话虽如此,如果您不想使用延迟加载页面,则无需为每个页面创建一个模块.请记住,您的所有页面和组件都将在您的应用启动期间加载,因此当用户打开您的应用时,一切准备就绪需要更多时间.

That being said, you don't need to create a module per page if you don't want to use lazy-loaded pages. Please keep in mind that all your pages and components will be loaded during your app startup, so it will take more time for everything to be ready when users open your app.

这篇关于Ionic 3 - 每个页面都需要自己的模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆