【问题标题】:How to setroot to my main application with tabs properly in ionic 3?如何在 ionic 3 中使用选项卡正确设置我的主应用程序?
【发布时间】:2018-10-23 16:53:25
【问题描述】:

大家好,我只是使用这个命令“ionic start helloWorld tabs”开始一个项目 它生成选项卡式项目,然后我添加一个按钮来推送到一个新页面,这是我想要返回到我的主应用程序的按钮,这是我如何编写新页面按钮

newpage.ts

  addItem(item: Item) {
    this.shopping.addItem(item).then(ref =>{
      this.navCtrl.setRoot('TabsPage', {key : ref.key});
    })
  }

保存项目后,我想回到我的主应用程序,我对我的 TabsPage 执行 setRoot,但我显示了这个错误

无效链接:TabsPage

这是我的 tabs.ts

import { Component } from '@angular/core';

import { AboutPage } from '../about/about';
import { ContactPage } from '../contact/contact';
import { HomePage } from '../home/home';

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tab1Root = HomePage;
  tab2Root = AboutPage;
  tab3Root = ContactPage;

  constructor() {

  }
}

我在这里错过了什么?我应该导航到 tabspage 类吗?

在我用@Yerkon 回答选项二更新我的代码后,我得到了这些错误:

Error: Uncaught (in promise): Error: Type TabsPage is part of the 2 个模块的声明:AppModule 和 TabsPageModule!请 考虑将 TabsPage 移动到导入 AppModule 的更高模块和 标签页模块。你也可以创建一个新的 NgModule 来导出和 包括 TabsPage 然后在 AppModule 中导入该 NgModule 和 标签页模块。错误:类型 TabsPage 是 2 的声明的一部分 模块:AppModule 和 TabsPageModule!请考虑移动 TabsPage 到导入 AppModule 和 TabsPageModule 的更高模块。你可以 还创建一个新的 NgModule 导出并包含 TabsPage 然后 在 AppModule 和 TabsPageModule 中导入该 NgModule。

它说我必须将我的标签页模块移高,这样做正常吗?还是我错过了什么?

【问题讨论】:

  • 添加按钮以推送到新页面 为什么不直接从 newPage 弹出? newPage 不是被推送到其中一个标签页上吗?

标签: ionic-framework ionic2 tabs ionic3 tabpage


【解决方案1】:

无效链接:TabsPage

抛出此错误,因为 TabsPage 未在模块中注册。 有两种注册方式:

  1. 如果页面渴望加载

app.module.ts:

@NgModule({
  declarations: [
    ConferenceApp,
    AboutPage,
    AccountPage,
    LoginPage,
    MapPage,
    PopoverPage,
    SchedulePage,
    ScheduleFilterPage,
    SessionDetailPage,
    SignupPage,
    SpeakerDetailPage,
    SpeakerListPage,
    TabsPage,
    TutorialPage,
    SupportPage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(ConferenceApp, {}, {
      links: [
        { component: TabsPage, name: 'TabsPage', segment: 'tabs-page' },
         ...
      ]
    }),
    IonicStorageModule.forRoot()
  ],
...
  1. 如果页面将延迟加载。在这里为您创建模块的每个页面。 提示:使用 ionic CLI 无需手动创建 page/page.modules。只需运行:ionic g page TabsPage。结果应该类似于:

tabs.module.ts:

import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';

import { TabsPage } from './tabs';

@NgModule({
  declarations: [
    TabsPage,
  ],
  imports: [
    IonicPageModule.forChild(TabsPage),

  ],
  exports: [
    TabsPage
  ]
})
export class TabsPageModule { }

tabs.ts:

@IonicPage()
@Component({
  selector: 'page-tabs',
  templateUrl: 'tabs.html'
})
export class TabsPage {
  tab1Root: any = Tab1Root;
  tab2Root: any = Tab2Root;
  tab3Root: any = Tab3Root;
...

【讨论】:

  • 嗨,我试试你的选项 2 答案,我收到一个很长的错误,我更新了我的问题中的错误,你能帮我看看吗?
  • @KeVin,从app.module 中删除 TabsPage 组件和如果存在模块。如果您使用第二种方法,则无需在app.module 中声明
猜你喜欢
  • 2021-08-11
  • 2011-10-04
  • 2017-11-04
  • 1970-01-01
  • 2012-10-06
  • 2018-09-12
  • 1970-01-01
  • 2017-06-24
  • 2020-08-29
相关资源
最近更新 更多