2017-09-16 48 views
1

我生成了一個空白的應用程序使用離子開始。這運行良好。離子3角度未處理的承諾拒絕:模板分析錯誤:

現在我添加了一個名爲opty的新頁面,使用 離子g頁面opty。

創建一個導航使用navpush從home.html到opty.html點擊按鈕。很棒。現在

,我添加使用組件: 離子克組分opty頭

所有細直到這裏。現在,如果這包括在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/[email protected]: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以及dec因此我無法理解如何解決這個問題。

+0

你有'components.module.ts',所以只需將它導入到你的'opty.module.ts'。不要再導入組件。 – Duannx

+0

不知道如何將模塊導入到另一個模塊。請舉個例子 – Vik

+0

看我的[回覆](https://stackoverflow.com/a/46166922/4254681)。如果仍然面臨錯誤,請讓我知道 – Duannx

回答

2

由於您在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 { } 

延遲加載功能仍然在離子好轉,但這是網頁工作的推薦方法和延遲加載的組件。 因此,您需要創建一個包含所有組件的模塊(ComponentsModule),然後只需在每個頁面模塊中導入該模塊,以使其在該頁面中可用。

+1

你是對的。但爲什麼需要'出口:[OptyPage]'? – Duannx

+1

我的不好,根本不需要。感謝您指出:) – sebaferreras

+0

這根本不適合我。我修改了我的問題,並根據您的建議使用了最新的代碼。我收到了同樣的錯誤。 – Vik

0

首先您的components.module需要列出導入組件模塊的類。

components.module

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

你opty模塊需要出口的標題組件,因此它可以在其他組件使用

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 
    ] 
}) 
+0

這對我來說沒有意義。但我仍然試了一下,是的,它不起作用 – Vik

相關問題