Ionic 4: 'ion-slide' is not a known element

回眸只為那壹抹淺笑 提交于 2019-12-11 04:14:55

问题


I am using ion-slides in a new component.

carousel.component.html

<ion-slides pager="true" >
  <ion-slide>
    <h1>Slide 1</h1>
  </ion-slide>
  <ion-slide>
    <h1>Slide 2</h1>
  </ion-slide>
  <ion-slide>
    <h1>Slide 3</h1>
  </ion-slide>
</ion-slides>

carousel.component.ts

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

@Component({
  selector: 'app-carousel',
  templateUrl: './carousel.component.html',
  styleUrls: ['./carousel.component.scss']
})
export class CarouselComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

But when I use this component <app-carousel></app-carousel> inside my Ionic page, I get the following error:

'ion-slide' is not a known element:
1. If 'ion-slide' is an Angular component, then verify that it is part of this module.
2. If 'ion-slide' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("<ion-slides pager="true" >
  [ERROR ->]<ion-slide>
    <h1>Slide 1</h1>
  </ion-slide>
"): ng:///SharedModule/CarouselComponent.html@1:2
'ion-slide' is not a known element:
1. If 'ion-slide' is an Angular component, then verify that it is part of this module.
2. If 'ion-slide' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
    <h1>Slide 1</h1>
  </ion-slide>
  [ERROR ->]<ion-slide>
    <h1>Slide 2</h1>
  </ion-slide>

Please help me to solve this issue.


回答1:


You need to add CUSTOM_ELEMENTS_SCHEMA to schemas in NgModule.

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

...

@NgModule({
  imports: [],
  ...
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

export class YourModule {}



回答2:


Adding IonicModule.forRoot() to the parent module of my component helped to solve the issue.

@NgModule({
  imports: [
    ...
    IonicModule.forRoot(),
  ]
})



回答3:


Try to add CUSTOM_ELEMENTS_SCHEMA in your module file..

import { NgModule, CUSTOM_ELEMENTS_SCHEMA }  from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';

@NgModule({
    bootstrap: [AppComponent],
    declarations: [AppComponent],
    imports: [BrowserModule],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }



回答4:


Add IonicModule.forRoot() to the parent module (app.module.ts) and add IonicModule to the Module that holds your component that adresses ion-slide

app.module.ts:

@NgModule({
  imports: [
    ...
    IonicModule.forRoot(),
  ]
})

your component module (or shared.module.ts):

@NgModule({
  imports: [
    ...
    IonicModule,
  ]
})

derived from https://github.com/ionic-team/ionic/issues/17232



来源:https://stackoverflow.com/questions/54309846/ionic-4-ion-slide-is-not-a-known-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!