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

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

问题描述

我不太明白,当我使用 Ionic 3 使用以下命令创建一个简单的页面 ionic g page contact 时,我究竟应该在哪里链接文件和页面?

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?

谢谢

推荐答案

当你使用创建页面时

离子g页面测试页

首先我们需要将它导入到 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

从 '../testpage/testpage' 导入 { TestpagePage };

在按钮点击事件中,

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: 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 部分中定义我们的页面组件.有这个单独的 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.

供应商:在提供者部分,我们可以为依赖注入注册服务.当你在 App Module 中注册一个服务时,它就可以在所有的应用程序中的组件.但是,我们不需要在此处包含所有服务,我们也可以决定仅在其 @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天全站免登陆