【问题标题】:Ionic 3 error 'collapse' is not a known element:Ionic 3 错误“崩溃”不是已知元素:
【发布时间】:2019-02-27 16:37:34
【问题描述】:

我正在尝试使用 Ionic 3 创建我自己的组件,我按照以下步骤操作

  • 第 1 步 - 使用命令行创建组件
离子 g 分量崩溃
  • 第 2 步 - 在 app.module 中添加

... 声明:[ 折叠组件 ], ... 入口组件:[ 折叠组件 ], ...
  • 第 3 步 - 尝试使用标签
 折叠 

但我有这个错误。

我尝试了很多替代方案,但都没有奏效。

  • collapse.ts 文件 从'@angular/core'导入{组件};

    @Component({
      selector: 'collapse',
      templateUrl: 'collapse.html'
    })
    export class CollapseComponent {
      text: string;
    
      constructor() {
        console.log('Hello CollapseComponent Component');
        this.text = 'Hello World';
      }
    }
    


* component.mudule.ts 文件

import {NgModule} from '@angular/core';
import {PopoverComponent} from './popover/popover';
import {CollapseComponent} from './collapse/collapse';
import {IonicModule} from "ionic-angular";

@NgModule({
    declarations: [
        PopoverComponent,
        CollapseComponent
    ],
    imports: [
        IonicModule,
    ],
    exports: [
        PopoverComponent,
        CollapseComponent
    ]
})
export class ComponentsModule {
}

【问题讨论】:

  • 通常当您执行“离子 g 组件崩溃”时,您应该会在终端中看到 [OK] Generated a component named collapse! 您是否收到此消息?
  • 是的,我收到了这条消息。

标签: ionic-framework ionic2 ionic3


【解决方案1】:

你必须这样写你的组件:

@Component({
  selector: 'collapse',
  templateUrl: `<URL>`
})
export class CollapseComponent {

}

默认情况下,组件标签具有app 之类的前缀。你也可以试试。
selector字段最重要

【讨论】:

  • 它也不起作用,我尝试使用前缀'app'和'ion'
  • 从'@angular/core'导入{组件}; @Component({ selector: 'collapse', templateUrl: 'collapse.html' }) 导出类 CollapseComponent { text: string; constructor() { console.log('Hello CollapseComponent 组件'); this.text = 'Hello World'; } }
  • 检查.angular-cli.json 文件和prefix 字段。默认情况下,它是app。您还可以检查 tslint.json 文件 a,d component-selector 字段。它必须包含app
  • 您的终端或浏览器中是否有错误消息?还是开发工具?
  • 我改变了实现。谢谢大家
猜你喜欢
  • 2019-04-27
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2019-06-18
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多