【问题标题】:Error: Type [somePage] in [somePath] is part of the declarations of 2 modules错误:[somePath] 中的类型 [somePage] 是 2 个模块声明的一部分
【发布时间】:2017-06-28 19:58:47
【问题描述】:

当我尝试构建我的应用程序的 --prod Android 版本时遇到的 ionic 3 错误,我快疯了。

错误:

Running app-scripts build: --prod --iscordovaserve --externalIpRequired --nobrowser

[21:21:54]  build prod started ... 
[21:21:54]  clean started ... 
[21:21:54]  clean finished in 2 ms 
[21:21:54]  copy started ... 
[21:21:54]  ngc started ... 
[WARN] Error occurred during command execution from a CLI plugin
       (@ionic/cli-plugin-cordova). Your plugins may be out of date.
Error: Type TestPage in
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.ts is part of the
declarations of 2 modules: AppModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/app/app.module.ts
and TestPageModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.module.ts! 
Please consider moving TestPage in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.ts 
to a higher module that imports AppModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/app/app.module.ts 
and TestPageModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.module.ts. 
You can also create a new NgModule that exports and includes TestPage in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.ts then import 
that NgModule in AppModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/app/app.module.ts and 
TestPageModule in 
/Users/sergidb/Desktop/Borrar/myStackApp/src/pages/test/test.module.ts.

重现步骤

  1. 创建一个新的 Ionic 应用程序ionic start myStackApp tabs
  2. 进入应用文件夹cd myStackApp
  3. 在浏览器中测试应用 ionic serve => 应用正常
  4. 编译 --prod build ionic cordova build android --release --prod => 没有错误
  5. 生成新页面ionic g page Test
  6. 编辑 app.module.ts
  7. 编辑 home.ts
  8. 编辑 home.html
  9. 在浏览器中测试应用 ionic serve => 应用正常
  10. 编译 --prod build ionic cordova build android --release --prod => 哎呀!

错误出现在控制台中。

编辑后的文件:

 app.module.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { TestPage } from '../pages/test/test';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    TestPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    TestPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { TestPage } from '../test/test';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {

  }

  goToTest(){
      this.navCtrl.push(TestPage);
  }

}

home.html

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h2>Welcome to Ionic!</h2>
  <p>
    This starter project comes with simple tabs-based layout for apps
    that are going to primarily use a Tabbed UI.
  </p>
  <p>
    Take a look at the <code>src/pages/</code> directory to add or change tabs,
    update any existing page or create new pages.
  </p>

    <button ion-button (click)="goToTest()">Go!</button>

</ion-content>

我的问题:

  • 我做错了什么?
  • 我是不是有些不懂?

提前致谢!

编辑

test.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the TestPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-test',
  templateUrl: 'test.html',
})
export class TestPage {

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad TestPage');
  }

}

test.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { TestPage } from './test';

@NgModule({
  declarations: [
    TestPage,
  ],
  imports: [
    IonicPageModule.forChild(TestPage),
  ],
  exports: [
    TestPage
  ]
})
export class TestPageModule {}

【问题讨论】:

  • 您的其他页面是否在顶部有@IonicPage 和独立的.modules.ts 文件
  • @IzzoObella 是的,ionic g page PageName 生成 4 个文件:.html、.ts、.scss 和 .module.ts
  • @IzzoObella 我编辑了帖子添加了 test.ts 和 test.module.ts 文件

标签: android cordova ionic-framework build


【解决方案1】:

所以我在为生产构建时遇到了同样的错误,我通过删除所有不同的组件/页面.module.ts 来修复它,然后从我的所有组件中删除@IonicPage()。例如,test.ts 会变成:-

 import { Component } from '@angular/core';
import {NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the TestPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@Component({
  selector: 'page-test',
  templateUrl: 'test.html',
})
export class TestPage {

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad TestPage');
  }

}

然后删除test.module.ts。似乎当前用于发布和生产的 ionic-cli 构建不支持多模块(子模块)。

所以我一直在研究 ionic 3 中的延迟加载,所以我终于找到了另一种更好的方法来使用 @IonicPage 并构建生产。这是我的 ionic 版本信息。

global packages:

    @ionic/cli-utils : 1.4.0
    Cordova CLI      : 7.0.1 
    Ionic CLI        : 3.4.0

local packages:

    @ionic/app-scripts              : https://registry.npmjs.org/@ionic/app-scripts/-/app-scripts-1.3.7.tgz
    @ionic/cli-plugin-cordova       : 1.4.0
    @ionic/cli-plugin-ionic-angular : 1.3.1
    Cordova Platforms               : android 6.2.3
    Ionic Framework                 : ionic-angular https://registry.npmjs.org/ionic-angular/-/ionic-angular-3.2.1.tgz

System:

    Node       : v7.4.0
    OS         : Linux 4.6
    Xcode      : not installed
    ios-deploy : not installed
    ios-sim    : not installed
    npm        : 5.0.0 

所以在你的函数goToTestPage() above, you would change thethis.navParams.push(TestPage)tothis.navParams.push('TestPage');` 中将页面作为字符串。这安全地实现了延迟加载。有关延迟加载 ion ionic 3 的更多信息,您可以参考此youtube tutorial

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 2019-05-03
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多