2017-10-13 22 views
1

私はionic-v1で1年ほど前にやったionicで再開発しているアプリがあります。ユーザーが選択する複数のテンプレートタイプがあり、ユーザー構成値に基づいてtemplateURLをロードする動的コンポーネントを作成したいと考えています。つまり、アプリケーションを実行するとき以外は、コンポーネントをロードして適切なテンプレートを取得する限り、すべてが機能しているので、ngModelが角属性であるかどうかわからないかのようにテンプレートエラーが発生します。 は、ここで私が持っているものです。イオン3ダイナミックテンプレートのURL

import { Compiler, Component, AfterViewInit, OnInit, Injector, ViewChild, NgModule, NgModuleRef, ViewContainerRef } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 

import { SomeService } from '../../app/services/app.services'; 

@Component({ 
    template: `<ng-container #dynamicvc></ng-container>` 
}) 
export class DynamicHome implements OnInit { 
    @ViewChild('dynamicvc', { read: ViewContainerRef }) container; 
    ehi: any; 
    sponsorGroups: any[]; 
    baseUrl: string; 
    selectedSegment: string; 

    constructor(private compiler: Compiler, private injector: Injector, private moduleRef: NgModuleRef<any>, private someSvc: SomeService, private navCtrl: NavController) { 
     let vm = this; 

     vm.selectedSegment = 'home'; 
    } 

    ngAfterViewInit() { 
     let vm = this; 

     const MaterialCardsSpookyHomeComponent = Component({ templateUrl: './materialcardsspooky/materialcardsspooky-home.html' })(class MaterialCardsSpookyHomeComponent { }); 
     const MaterialCardsSpookyHomeModule = NgModule({ declarations: [MaterialCardsSpookyHomeComponent]})(class MaterialCardsSpookyHomeModule { }); 

     let moduleToUse = null; 
     switch (vm.someSvc.template.toLowerCase()) { 
      case 'materialcardsspooky': 
       moduleToUse = MaterialCardsSpookyHomeModule; 
       break; 
     } 

     if (moduleToUse) { 
      vm.compiler.compileModuleAndAllComponentsAsync(moduleToUse).then((factories) => { 
       const f = factories.componentFactories[0]; 
       const cmpRef = f.create(vm.injector, [], null, vm.moduleRef); 
       cmpRef.instance.ehi = vm.ehi; 
       cmpRef.instance.sponsorGroups = vm.sponsorGroups; 
       cmpRef.instance.baseUrl = vm.baseUrl; 
       cmpRef.instance.selectedSegment = vm.selectedSegment; 
       vm.container.insert(cmpRef.hostView); 
      }); 
     } 
    } 

    ngOnInit() { 
     let vm = this; 

    } 
} 

は、ここに私のテンプレートです:

<ion-header> 
    <ion-toolbar no-border-top no-border-bottom> 
    <ion-segment [(ngModel)]="selectedSegment"> 
     <ion-segment-button value="home" no-border-bottom> 
     <ion-icon name="home"></ion-icon> 
     </ion-segment-button> 
    </ion-segment> 
    </ion-toolbar> 
</ion-header> 
<ion-content> 
    <div [ngSwitch]="selectedSegment"> 
    <div *ngSwitchCase="'home'"> 
     ... 
    </div> 
    </div> 
</ion-content> 

そして、ここではクローム開発ツールの私のエラーです:

Can't bind to 'ngModel' since it isn't a known property of 'ion-segment'.

  1. If 'ion-segment' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.
  2. If 'ion-segment' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("

任意のアイデアはどのようにか、私が達成することができても、この?また、VS2017と標準のIonicテンプレートを使用して、デフォルトのビルドスクリプト、つまりwebpackを使用します。角度は、以下のNgModuleの輸入配列で必要とされるカスタムコンポーネントオニクス認識させる

+0

私はそれが可能であるかどうかはわかりませんが、これをあなたのNgModule: 'imports:[IonicPageModule.forChild(MaterialCardsSpookyHomeComponent)]'に追加しようとすることができます。 – David

+0

ニース!ありがとう!それは確かに正しい道のりです。これは、ngModelのような角度モジュールを使用しようとするとエラーが発生しましたが、今はテンプレートで使用するカスタムコンポーネントの使用に関するエラーが発生しています。それはそれを認識していないので、同様に動作するようにそれを得る方法を理解しようとしていると言います... – shinsnake

+0

良い、私は少しあなたを助けることができてうれしいです。カスタムコンポーネントは、別のモジュールで宣言されている場合はモジュールのインポート配列に、別のモジュールで宣言されていない場合は宣言配列内にある必要があります。 – David

答えて

0

imports: [ IonicPageModule.forChild(MaterialCardsSpookyHomeComponent) ] 

また、モジュールのどこかで使用するすべてのカスタムコンポーネントのthatsはすでに場合imports配列にする必要があります他のモジュールで宣言され、エクスポートされている場合は配列内に宣言されていません。

関連する問題