Accordion List within ionic 2

2019-09-08 04:05发布

问题:

I've create a custom components named Accordion within iconic 2 and working in browser perfectly but on device not working.

I've split my code up into components, where

Home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {DataCards} from '../../components/data-cards/data-cards';
import {Data} from '../../components/data/data';

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

  public dataList: Data[];

  constructor(public navCtrl: NavController) {
    this.dataList = [
      new Data('title 1', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ','ios-remove-circle-outline', true),
      new Data('title 2', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ','ios-add-circle-outline', false),
      new Data('title 3', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ','ios-add-circle-outline', false)
    ];
  }

}

and the corresponding HTML

<ion-content padding>
  <data-cards [data]="dataList"></data-cards>
</ion-content>

contain my custom component data-cards. data-cards has an input parameter data, through which the list of data is passed.

data.ts

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

@Component({
  selector: 'data',
  templateUrl: 'data.html'
})
export class Data {

  constructor(public title: string, public details: string, public icon: string, public showDetails: boolean) {}

}

data-cards.ts

import { Component } from '@angular/core';
import { Data } from '../data/data';

@Component({
  selector: 'data-cards',
  inputs: ['data'],
  templateUrl: 'data-cards.html'
})
export class DataCards {

  public data: Data[];

  constructor() {}

  toggleDetails(data: Data) {
    if (data.showDetails) {
        data.showDetails = false;
        data.icon = 'ios-add-circle-outline';
    } else {
        data.showDetails = true;
        data.icon = 'ios-remove-circle-outline';
    }
  }

}

app.module.ts

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { Data } from '../components/data/data';
import { DataCards } from '../components/data-cards/data-cards';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    Data,
    DataCards
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    Data,
    DataCards
  ],
  providers: []
})
export class AppModule {}

When run on iOS ( ionic run ios ) i've got an error like below :

[08:44:54]  Error: Error at /Users/imac/Documents/ionic2Accordion/.tmp/components/data/data.ngfactory.ts:29:71 
[08:44:54]  Property 'string' does not exist on type 'typeof "/path/ionic2Accordion/.tmp/components/data/data"'. 
[08:44:54]  Error at /path/ionic2Accordion/.tmp/components/data/data.ngfactory.ts:29:111 
[08:44:54]  Property 'string' does not exist on type 'typeof "/path/ionic2Accordion/.tmp/components/data/data"'. 
[08:44:54]  Error at /path/ionic2Accordion/.tmp/components/data/data.ngfactory.ts:29:151 
[08:44:54]  Property 'string' does not exist on type 'typeof "/path/ionic2Accordion/.tmp/components/data/data"'. 
[08:44:54]  Error at /path/ionic2Accordion/.tmp/components/data/data.ngfactory.ts:29:191 
[08:44:54]  Property 'boolean' does not exist on type 'typeof "/path/ionic2Accordion/.tmp/components/data/data"'. 
[08:44:54]  ngc failed 
[08:44:54]  ionic-app-script task: "build" 
[08:44:54]  Error: Error 

so my question : how i can resolve this problem any suggestion ?

回答1:

In data-card.ts change

public data: Data[];

o be

Input() data: Data[]; 

since you will be assigning it from the component creation in the home.html? You'll also need to import the Input module via

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