【问题标题】:Ionic button showing 'ion-button' is not a known element显示“离子按钮”的离子按钮不是已知元素
【发布时间】:2018-07-26 17:53:20
【问题描述】:

我是 ionic 新手,这似乎是一个愚蠢的问题,但我需要一些帮助 使用一些简单的按钮会引发错误。我正在使用离子 4.0。

'ion-button' 不是已知元素: 1. 如果 'ion-button' 是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 如果“ion-button”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

<ion-button color="primary">Primary</ion-button>

【问题讨论】:

    标签: ionic-framework ionic4


    【解决方案1】:

    试试这个,

    <button ion-button color="primary">Primary</button>
    

    【讨论】:

    • 感谢您的回答。有效。我尝试了官方链接中提供的文档,但不知何故它不起作用。 beta.ionicframework.com/docs/api/button/#usage 这个路径还提供了文档,node_modules/ionic-angular/components/button/button.js
    • 所有 UI 组件文档都可以在这里找到ionicframework.com/docs/components
    • 你根本没有使用 Ionic 4 但仍然是版本 3 ;)
    • 我将如何使用 ionic 4?我需要改变什么。我和 OP 有同样的问题。
    【解决方案2】:

    为了避免该错误消息:

    1. CUSTOM_ELEMENTS_SCHEMA 导入 app.modules.ts:
        import { ErrorHandler, NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
    
    1. schema: [CUSTOM_ELEMENTS_SCHEMA] 添加到 app.modules.ts,如下所示:
        @NgModule({
          declarations: [
            MyApp,
            HomePage
          ],
          imports: [
            BrowserModule,
            HttpClientModule,
            MomentModule,
            IonicModule.forRoot(MyApp),
          ],
          bootstrap: [IonicApp],
          entryComponents: [
            MyApp,
            HomePage
          ],
          providers: [
            StatusBar,
            SplashScreen,
            {provide: ErrorHandler, useClass: IonicErrorHandler},
          ],
          schemas: [CUSTOM_ELEMENTS_SCHEMA]
        })
    

    【讨论】:

    • 我认为要强调的另一件重要事情是对于使用延迟加载的人来说,您必须对每个页面的模块执行此操作。
    • 这对我不起作用。我也把它放在所有模块中,因为我使用的是延迟加载。
    • 你永远不应该使用“CUSTOM_ELEMENTS_SCHEMA”,它所做的只是告诉 Angular 不要抱怨它无法识别的组件
    • 由于这是一个与离子有关而不是角度的问题,我同意@cuddlemeister。您应该改用 IonicModule。
    【解决方案3】:

    我认为解决方案是在相应的模块导入中导入 Ionic 模块

    import { IonicModule } from '@ionic/angular';  <--add this line
    @NgModule({
    imports: [
        CommonModule,
        FormsModule,
        IonicModule, <-- add this line
      ],
    
    })
    

    【讨论】:

    • 这应该是答案
    • @VladimirDespotovic 然后使用“-g”再次正确安装 ionic cli
    • 当我这样做时,我得到一个不同的错误:Declaration expected.ts(1146)
    【解决方案4】:

    我也遇到过。您的解决方案不是最好的,因为新的 Ionic 4 方法是使用 &lt;ion-button&gt; (https://beta.ionicframework.com/docs/components/#button)。

    在我在/src/app/my-page/my-page.html 下的页面中它确实对我有用,但是当我把它放在/src/shared/components/my-comp/my-comp.html 中时它给出了错误。奇怪的是我在同一页&lt;ion-grid&gt;&lt;ion-row&gt;&lt;ion-col&gt; 中有其他离子元素。此外,此代码曾经位于 my-page.html 中,它可以正常工作。

    仅供参考,MyComponentcomponents.module.ts 中作为declarationexport。还不知道我错过了什么......

    更新 1:将 components 目录移动到 srcsrc/app 下都没有任何区别。

    更新 2:这是我的环境:

       ionic (Ionic CLI)          : 4.0.6
       Ionic Framework            : @ionic/angular 4.0.0-beta.2
       @angular-devkit/core       : 0.7.2
       @angular-devkit/schematics : 0.7.2
       @angular/cli               : 6.1.2
       @ionic/ng-toolkit          : 1.0.0
       @ionic/schematics-angular  : 1.0.1
    

    更新 3:在这种环境中仍然存在问题:

       ionic (Ionic CLI)          : 4.1.0
       Ionic Framework            : @ionic/angular 4.0.0-beta.3
       @angular-devkit/core       : 0.7.2
       @angular-devkit/schematics : 0.7.2
       @angular/cli               : 6.1.2
       @ionic/ng-toolkit          : 1.0.6
       @ionic/schematics-angular  : 1.0.5
    

    更新 4:经过反复试验,我不得不将 schemas: [CUSTOM_ELEMENTS_SCHEMA] 添加到我的 components.module.ts 文件中。我能够保持原样的目录结构。不过,我不确定为什么这种情况需要这样做。

    【讨论】:

    • CUSTOM_ELEMENTS_SCHEMA 允许 Angular 合并自定义元素。 angular.io/api/core/CUSTOM_ELEMENTS_SCHEMA
    • 我在 ionic 4 上遇到了类似的问题。我必须将 CUSTOM_ELEMENTS_SCHEMA 添加到 app.module.ts 和 components.module.ts 才能使其正常工作。
    【解决方案5】:

    您似乎没有在组件模块中导入ionicModule。所以,在module.ts中导入IonicModule,其他的都可以正常使用

    【讨论】:

      【解决方案6】:

      是的,试试这个

      <button ion-button color="primary">Primary</button>
      

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
      • 感谢您的回答。有效。我尝试了官方链接中提供的文档,但不知何故它不起作用。 beta.ionicframework.com/docs/api/button/#usage 这个路径还提供了文档,node_modules/ionic-angular/components/button/button.js
      【解决方案7】:

      在 Ionic 5 中,我在使用 --prod 选项构建时遇到了同样的问题。

      由于 *.module.ts 文件在 Ionic 5 的组件中不可用,我需要为组件添加共享模块,然后将 CUSTOM_ELEMENTS_SCHEMA 架构添加到该共享模块。

      ionic g module modules/shared
      

      cat shared.module

      import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
      import { CommonModule } from '@angular/common';
      import { FooComponent } from '../../components/foo/foocomponent.component'; <== Add your components
      
      
      @NgModule({
        declarations: [FooComponent], <== Add your components
        imports: [
          CommonModule
        ], 
        exports: [FooComponent], <== Add your components
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
      })
      export class SharedModule { }
      

      【讨论】:

        【解决方案8】:

        在父模块中导入您的自定义组件。例如在您的 app.module.ts 中:

        declarations: [MyComponent],
        exports: [
            PopoverComponent,
        ]
        

        【讨论】:

          【解决方案9】:

          我的问题是,在此错误之前出现了一些错误,这些错误似乎是级联的。我遇到的错误是声明中应该包含在提供程序中的一些元素。此外,其中一个在构造函数中被标记为私有,而它应该是公开的。

          @NgModule({
              imports: [
                  IonicModule,
                  CommonModule,
                  FormsModule,
                  RouterModule.forChild([{ path: '', component: DevicesPage }]),
                  DevicesPageRoutingModule,
                  TranslateModule.forChild()
              ],
              declarations: [DevicesPage  --> remove BLE, LocationAccuracy, DeviceManagerService  ],
              providers: [BLE, LocationAccuracy, DeviceManagerService]  <--Add
          })
          

          【讨论】:

            【解决方案10】:

            在我意识到问题是我没有正确创建 Ionic Angular 项目之前,我也被困了一段时间。你必须包括 --type=angular
            https://github.com/ionic-team/ionic-cli

            表达式: ionic start myApp tabs --type=angular

            【讨论】:

              【解决方案11】:

              我在 ionic 4 之后遇到了类似的问题,所以我在 app.modules.ts 中添加了 CUSTOM_ELEMENTS_SCHEMA。然后效果很好

              app.module.ts

                providers: [
                  StatusBar,
                  SplashScreen,
                  {provide: ErrorHandler, useClass: IonicErrorHandler},
                  SpineRestServiceProvider,
                  FingerprintAIO
                    ],
                schemas: [CUSTOM_ELEMENTS_SCHEMA]
              

              【讨论】:

                【解决方案12】:

                应该在 app.module.ts 中添加

                import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
                
                
                  providers: [
                    StatusBar,
                    SplashScreen,
                    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
                  ],
                  schemas: [CUSTOM_ELEMENTS_SCHEMA] ==> add line**
                

                【讨论】:

                猜你喜欢
                • 2018-10-07
                • 2016-02-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多