【问题标题】:ionic 3 angular Unhandled Promise rejection: Template parse errors:ionic 3 angular Unhandled Promise 拒绝:模板解析错误:
【发布时间】:2018-02-25 05:09:18
【问题描述】:

我使用 ionic start 生成了一个空白应用程序。那运行良好。

现在我添加了一个名为 opty 的新页面 ionic g page opty。

单击按钮时使用 navPush 从 home.html 到 opty.html 创建导航。效果很好。

现在,我添加了一个组件,使用: ionic g 组件 opty-header

到这里为止一切都很好。现在,如果将其包含在 opty.html 中

它以错误结束:

Unhandled Promise rejection: Template parse errors:
'opty-header' is not a known element:
1. If 'opty-header' is an Angular component, then verify that it is part of this module.
2. If 'opty-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

<ion-content padding>
  [ERROR ->]<opty-header></opty-header>
</ion-content>
"): ng:///AppModule/OptyPage.html@16:2 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:
'opty-header' 

我的 opty.module.ts 看起来像

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { OpportunitiesPage } from './opportunities';

import { ComponentsModule } from '../../components/components.module'


@NgModule({
  declarations: [
    OpportunitiesPage,
  ],
  imports: [
    IonicPageModule.forChild(OpportunitiesPage),
    ComponentsModule
  ],
})
export class OpportunitiesPageModule {}

我的 components.module.ts 看起来像

import { NgModule } from '@angular/core';
import { OptyHeaderComponent } from './opty-header/opty-header';
@NgModule({
    declarations: [OptyHeaderComponent],
    imports: [],
    exports: [OptyHeaderComponent]
})
export class ComponentsModule {}

由于 opty.module.ts 明确导入 OptyHeaderComponent 并声明,所以我无法理解如何修复它。

【问题讨论】:

  • 您有components.module.ts,因此只需将其导入您的opty.module.ts。不要再次导入组件。
  • 不确定如何将模块导入另一个模块。请举个例子
  • 见我的answer。如果仍然遇到错误,请告诉我
  • 我在哪里创建 share.module.ts ?以及任何特定的 cli 命令等来做到这一点?
  • 您可以使用components.module.ts 代替share.module.ts。这只是一个名字。请参阅this以更好地理解

标签: angular typescript ionic2 ionic3


【解决方案1】:

因为在您的components.module.ts 文件中,您声明并导出了OptyHeaderComponent

import { NgModule } from '@angular/core';
import { OptyHeaderComponent } from './opty-header/opty-header';

@NgModule({
    declarations: [OptyHeaderComponent],
    imports: [],
    exports: [OptyHeaderComponent]
})
export class ComponentsModule {}

现在在opty.module.ts 文件中只需导入ComponentsModule,如下所示:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { OptyPage } from './opty';

// import the ComponentsModule...

@NgModule({
  declarations: [
    OptyPage
  ],
  imports: [
    IonicPageModule.forChild(OptyPage),
    ComponentsModule // The ComponentsModule includes the OptyHeaderComponent
  ]
})
export class OptyPageModule { }

Ionic 中的延迟加载功能仍在改进中,但这是处理延迟加载的页面和组件的推荐方式。 因此,您需要创建一个包含所有组件的单个模块(ComponentsModule),然后将该模块导入每个页面模块中,使其在该页面中可用

【讨论】:

  • 你是对的。但是为什么需要exports: [ OptyPage ]
  • 我的错,根本不需要。感谢您指出这一点:)
  • 这对我来说根本不起作用。我根据您的建议用我拥有的最新代码修改了我的问题。我遇到了同样的错误。
【解决方案2】:

首先您的 components.module 需要在导入时列出组件模块的类。

components.module

import { NgModule } from '@angular/core';
import { OptyHeaderComponent } from './opty-header/opty-header';
@NgModule({
    imports: [OptyHeaderComponentModule]
})
export class ComponentsModule {}

您的 opty 模块需要导出 header 组件,以便它可以在其他组件中使用

opty.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { OptyPage } from './opty';

import { OptyHeaderComponent } from '../../components/opty-header/opty-header'
@NgModule({
  declarations: [
    OptyPage,
    OptyHeaderComponent
  ],
  imports: [
    IonicPageModule.forChild(OptyPage)
  ],
  exports: [
    OptyHeaderComponent
  ]
})

【讨论】:

  • 这对我来说没有意义。但我还是试了一下,是的,它不起作用
猜你喜欢
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2017-05-21
  • 2017-01-07
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多