在导出的组件中使用“ElementRef”时出错

阿马杜贝

ElementRef在 CommentComponent 中使用,它被导出到其他模块,如 ArticleModule、ProductModule 等......

// CommentModule
@NgModule({
    exports: [ CommentComponent ],
    declarations: [ CommentComponent ],
    providers: [ CommentService]
})

export class CommentModule { }


// Comment Component (belongs to CommentModule)
@Component({
    selector: 'comments',
    templateUrl: 'comment.component.html',
    styleUrls: ['comment.component.scss']
})
export class CommentComponent implements OnInit {

    suggOnTyping: string = 'hidden';
    constructor(private _eref: ElementRef){}

    @HostListener('document:click', ['$event'])
    onClickOutside(event: any) {
        event.preventDefault();
        event.stopPropagation();
        if (!this._eref.nativeElement.contains(event.target))
            this.suggOnTyping = 'hidden';
    }
}


// Article Module
@NgModule({
    imports: [
        CommonModule,
        RouterModule,
        CommentModule,
        ArticleRoutingModule],
    declarations: [ ArticleComponent ],
    providers: [ArticleService, CommentService, CommentComponent],
    schemas: [ NO_ERRORS_SCHEMA ]
})

ArticleComponent 像这样从视图中调用 CommentComponent:

<div class="article">
    .......
    <comments></comments>
</div>

现在,当我尝试通过 ArticleComponent 路由时,我得到:

core.js:1673 错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)[NgClass -> ElementRef]:StaticInjectorError(Platform:core)[NgClass -> ElementRef]:NullInjectorError:没有提供 ElementRef!错误:StaticInjectorError(AppModule)[NgClass -> ElementRef]: StaticInjectorError(Platform: core)[NgClass -> ElementRef]: NullInjectorError: No provider for ElementRef!

似乎 ElementRef 无法通过“提供者”,因为当我从 CommentComponent 中删除它时,一切正常。

问题是什么 ?

顺便说一下,我正在使用 Angular 6

苏尼尔·辛格

CommentComponent从 的提供者列表中删除AritcleModuleCommentComponent已经在CommentModule.

// Article Module

@NgModule({
    declarations: [ ArticleComponent], 
    providers: [ArticleService, CommentService, CommentComponent ], //<-- remove from here
    schemas: [ NO_ERRORS_SCHEMA ]
})

ElementRef从中删除constructor抛出此错误,如果您想访问该元素,则可以将元素的引用放在 ts 文件中并使用 @ViewChild 装饰器。

例子 :

html

<div #ele>hello/div> <!-- element reference goes here -->

ts

@ViewChild("ele") _eref: ElementRef;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用GraphQL查询组件时出现导出错误?

在 python 代码中使用“导出”命令时出错

在模块中使用导出的组件

在Intellibot Studio中使用readfromexcel组件时出错

在React功能组件中使用useEffect时出错

“警告:无法给函数组件提供引用。”在 nextjs 中使用自定义组件时出错

在R包中使用S3覆盖基元时导出错误

在不导入的情况下导出组件时出错

在宏中使用“#”时出错

React hooks & Context:在带有 useEffect 的子组件中使用上下文时出错

尝试在 React 组件中使用 Redux 动作调度器时出错?

当我在 React 类组件的样式中使用 theme.breakpoints.up 时出错

尝试在 Angular 9 中使用共享模块时出错,使我的组件无法识别我的材料模块

尝试在动态创建的组件angular 5.1中使用服务时出错

在asp.net core spas中使用angular cli生成新组件时出错

在 React 中导出组件时如何避免使用默认导出?

使用CakePHP的Auth组件登录时出错

使用 Angular 添加新组件时出错

使用ART中的组件文本时出错

在属性指令中使用ElementRef?

如何在 reactjs 组件中使用使用 scalajs 导出的函数?

使用记忆化的 React 组件重新绘制组件时出错

使用LibGDX和ProGuard导出Android APK时出错

使用从phpMyAdmin导出/导入SQL文件时出错

使用节点和猫鼬导出时出错

使用python导出后删除Excel工作表时出错

在 summarise_at() 中使用 n() 时出错

在VBA中使用数组时出错

在VB中使用isNumeric()时出错