首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >离子3指令

离子3指令
EN

Stack Overflow用户
提问于 2017-07-19 08:52:21
回答 5查看 6.8K关注 0票数 3

在应用程序上,离子2指令在app.module.ts中声明。

但是在Ionic 3(懒惰负载)中,这个指令不起作用。我试图在组件模块中导入指令,例如:

代码语言:javascript
复制
...
import { TabindexDirective } from '../../../app/tabindex.directive';

@NgModule({
  declarations: [
    ...
    TabindexDirective,
  ],
  imports: [
   ...
  ],
  exports: [
    ...
  ],
})
export class SignupModule {}

这段代码工作正常,但是我在另一个组件模块中导入了这个指令,我有错误:

类型GoComponent是两个模块声明的一部分: SignupModule和AddPageModule!请考虑将GoComponent移到导入的更高模块上

如何在Ionic 3中修复它并使用指令?

EN

回答 5

Stack Overflow用户

发布于 2018-03-15 09:05:28

同样的事情也发生在我最近的项目上。这个问题的解决方案是在directives.module.ts中导入指令,例如:

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { XYZDirective } from './XYZ/XYZ';
@NgModule({
    declarations: [XYZDirective],
    imports: [],
    exports: [XYZDirective]
})
export class DirectivesModule {}

并且,在需要指令的页面中导入XYZDirective。

票数 2
EN

Stack Overflow用户

发布于 2017-07-19 08:55:54

在Ionic 3内部使用的角2及以上,不能在两个模块中声明指令或组件。

您需要创建一个公共模块,在该模块中,需要在其他模块中重用的所有组件都应该声明。然后,您可以在您希望使用组件的任何模块中导入此公共模块。

票数 1
EN

Stack Overflow用户

发布于 2018-10-18 07:07:27

在组件中声明指令在离子型-3中发生了变化,实际上它将被模块本身绑定。

解决此问题的方法如下

解决方案- 1

在一个模块中声明和导出所有指令,这样您就可以在模块/离子页面中使用它

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { TabindexDirective } from '../../../app/tabindex.directive';
@NgModule({
    declarations: [TabindexDirective],
    imports: [],
    exports: [TabindexDirective]
})
export class SharedDirectives {}

在您的模块中您可以导入SharedDirectives

解决方案- 2

绑定模块中的指令并使用.forchild()导入到子组件;

代码语言:javascript
复制
import { TabindexDirective } from '../../../app/tabindex.directive';
@NgModule({
  declarations: [
    TabindexDirective,
  ],
  imports: [
    ...
    IonicPageModule.forChild(TabindexDirective)
  ],

})
export class SignupModule {}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45185537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档