简体   繁体   English

我可以将自定义打字稿组件转换为 Angular 中的 HTMLElement 吗?

[英]Can I convert a custom typescript component to a HTMLElement in Angular?

If I have a custom component in Angular.如果我在 Angular 中有一个自定义组件。 Ex.前任。 'my-component' “我的组件”

Is there a way to convert this component to a HTMLElement to pass to a function, if the function takes a HTMLElement as a parameter?如果函数将 HTMLElement 作为参数,有没有办法将此组件转换为 HTMLElement 以传递给函数?

I know the HTMLElement comes from this library: lib.dom.d.ts -> located in node_modules -> typescript -> lib -> lib.dom.d.ts -> HTMLElement我知道 HTMLElement 来自这个库:lib.dom.d.ts -> 位于 node_modules -> typescript -> lib -> lib.dom.d.ts -> HTMLElement

Here is an image of the element:这是元素的图像: 在此处输入图像描述

I believe the Angular Component class doesn't represent an actual element in the DOM, it's nothing more than a blueprint.我相信 Angular Component 类并不代表 DOM 中的实际元素,它只不过是一个蓝图。 In order to create the actual element, you first need to to create it as part of the Angular Component tree and then get a reference to the actual element in the dom through the ElementRef .为了创建实际元素,您首先需要将其创建为 Angular 组件树的一部分,然后通过ElementRef对 dom 中实际元素的引用。

Of course this is only possible if everything is running in the Angular Context.当然,这只有在一切都在 Angular 上下文中运行时才有可能。 The only way to use an Angular Component without Angular is to convert it into a Custom Element , here's a pretty neat tutorial on how to do it https://medium.com/@lavanya.kakimallaiah/creating-a-custom-element-in-angular-aa2a794fd041在没有 Angular 的情况下使用 Angular 组件的唯一方法是将其转换为自定义元素,这是一个关于如何做到这一点的非常简洁的教程https://medium.com/@lavanya.kakimallaiah/creating-a-custom-element-内角aa2a794fd041

If you inject in constructor ElementRef, you can "reach" in elementRef.nativeElement如果注入构造函数 ElementRef,则可以在 elementRef.nativeElement 中“到达”

htmlRef:any
constructor(private elementRef:ElementRef)
{
    this.htmlRef=elementRef.nativeElement
}

But the really important question is: What is the reason to need the htmlRef in Angular?但真正重要的问题是:在 Angular 中需要 htmlRef 的原因是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM