Ionic 4 не может импортировать пользовательский компонент - элемент не известен - PullRequest
0 голосов
/ 12 ноября 2018

Я новичок в Ionic и хочу создать собственный список с расширяемыми элементами по этой ссылке: https://www.joshmorony.com/creating-an-accordion-list-in-ionic/.

Проблема в том, что мой пользовательский компонент никогда не распознается, я пробовал много вещей, но ничего не работает ... Я использую Ionic 4, и они не говорят о генерации компонентов в своем документе.

Ошибка:

Error: Template parse errors:
'expandable' is not a known element:
1. If 'expandable' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

Вот папка моего проекта

expandable.component.html:

<div #expandWrapper class='expand-wrapper' [class.collapsed]="!expanded">
    <ng-content></ng-content>
</div>

expandable.component.ts:

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

@Component({
  selector: 'app-expandable',
  templateUrl: './expandable.component.html',
  styleUrls: ['./expandable.component.scss']
})
export class ExpandableComponent {

  @ViewChild('expandWrapper', {read: ElementRef}) expandWrapper;
  @Input('expanded') expanded;
  @Input('expandHeight') expandHeight;

  constructor(public renderer: Renderer) {
  }

  ngAfterViewInit(){
      this.renderer.setElementStyle(this.expandWrapper.nativeElement, 'height', this.expandHeight + 'px');   
  }

}

А вот как я пытаюсь это использовать:

<expandable [expandHeight]="itemExpandHeight" [expanded]="pet.expanded">
      Hello people
</expandable>

Наконец, мой app.module.ts:

@NgModule({
  declarations: [AppComponent, ExpandableComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(), 
    AppRoutingModule,
    IonicStorageModule.forRoot()
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    DatabaseService, 
    PetService,
    SQLite
  ],
  bootstrap: [AppComponent]
})

Ответы [ 3 ]

0 голосов
/ 31 января 2019

Я нашел решение, вам нужно создать component.module.ts в корне вашего компонента, а затем добавить в ваш импорт ComponentsModule

component.module

0 голосов
/ 11 августа 2019

В Ionic 3 и 4 мне помогло помнить, что недостаточно добавить

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

...
@NgModule({
  imports: [
    ...
    ComponentsModule,
    ...
  ],

к вашему app.module.ts

Вам также нужно добавить его в модуль каждой страницы!

src/app/home/home.module.ts
src/app/about/about.module.ts
src/app/game/game.module.ts

Мне никогда не нравилось использовать CUSTOM_ELEMENTS_SCHEMA в NgModule ...

0 голосов
/ 12 ноября 2018

Проблема с селектором компонента. Вы определили селектор app-expandable однако вы используете expandable Замените приведенный ниже код

<expandable [expandHeight]="itemExpandHeight" [expanded]="pet.expanded">
      Hello people
</expandable>

по

<app-expandable [expandHeight]="itemExpandHeight" [expanded]="pet.expanded">
      Hello people
</app-expandable>

OR

или вы измените селектор в ExpandableComponent

@Component({
  selector: 'expandable', //<-- change here
  templateUrl: './expandable.component.html',
  styleUrls: ['./expandable.component.scss']
})
export class ExpandableComponent {

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...