我必须在哪里导入Ionic 3上的页面? [英] Where do I have to import a page on Ionic 3?

查看:62
本文介绍了我必须在哪里导入Ionic 3上的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解,当我使用以下命令ionic g page contact用Ionic 3创建一个简单页面时,我到底在哪里链接文件和页面?

I don't very understand, when I create a simple page with Ionic 3 with the following command ionic g page contact, where exactly have I to link the files and the page?

仅在src/app/app.module.ts上有此行吗?

Only on src/app/app.module.ts with this line?

import { ContactPage } from '../pages/contact/contact';

为什么我需要将其推到providersdeclarations上?我必须对我的应用程序具有的所有页面执行此操作吗?

Why do I need to push it on providers and declarations then? I have to do this for all the page my app will have?

如果我要创建指向此页面的链接,是否也必须将其导入首页的打字稿文件中?

And if I want create a link to this page, I have to import it too on the homepage's typescript file?

谢谢

推荐答案

使用

ionic g page testpage

首先,我们需要将其导入app.module.ts文件

first we need to import it in app.module.ts file

import { TestpagePage } from '../pages/testpage/testpage';

以及declarations数组和entryComponents数组

declarations: [
    MyApp,
    HomePage,
    ListPage,
    TestpagePage
  ],
  entryComponents: [
    MyApp,
    HomePage,
    ListPage,
    TestpagePage
  ],

我们无需将其推入providers数组.例如,如果您要从主页导航到此页面,则

We no need to push it into providers array. and for example, if you want to navigate to this page from homepage, then

->在home.ts文件中导入测试页

-> import testpage in home.ts file

import { TestpagePage } from '../testpage/testpage';

并点击按钮事件,

this.navCtrl.push(TestpagePage);

声明: 在声明部分,我们需要包含所有创建的组件和指令,如果我们不在此处包含它们,则在尝试使用它们时会出现错误,因为Angular无法在我们的代码中识别它们.

declarations : In the declarations section we need to include all components and directives we create.If we don't include them here, we'll get an error when we try to use them because Angular won't be able to recognise them in our code.

entryComponents : 在entryComponents部分中,我们定义了仅按类型加载的任何组件.所有页面组件都是这种情况,因为它们都是 通过导航控制器加载.

entryComponents: In the entryComponents section we define any component that is only loaded by its type. This is the case for all Page components since these are all loaded through the Navigation Controller.

声明式加载的组件(即在另一个组件的模板中引用的组件)不需要包含在entryComponents数组中. 如您所见,我们有一些重复,我们必须在声明和entryComponents部分中都定义Page组件. 拥有这个单独的entryComponents部分的原因是,Angular可以为仅包含组件的应用程序编译一个捆绑包 该应用程序中实际使用的内容.

Components that are loaded declaratively (i.e. are referenced in another component's template) don't need to be included in the entryComponents array. So as you can see we have some duplication where we have to define our Page components in both the declarations and the entryComponents sections. The reason for having this separate entryComponents section is so that Angular can compile a bundle for the app that will only include the components that are actually used within the app.

提供者: 在提供者部分,我们可以注册服务以进行依赖项注入.当您在应用模块中注册服务时,可以在所有 应用中的组件. 但是,我们不需要在此处包含所有服务,我们还可以决定仅针对其@Component中的特定组件注册服务. 装饰器.

providers: In the providers section we can register services for dependency injection. When you register a service in the App Module, it can be used in all the components in your app. However, we don't need to include all our services here, we can also decide to register a service only for a specific component in its @Component decorator.

来源:选中此选项网址

这篇关于我必须在哪里导入Ionic 3上的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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