vue组件转换成dom元素_通过dom元素有角度地访问组件实例

news/2024/7/10 1:29:46 标签: vue, react

vue组件转换成dom元素

The thought of accessing the component instance through the DOM Element has been keeping my head busy since Angular 4. When I researched again these days, I learned that this can be done.

自从Angular 4以来,通过DOM Element访问组件实例的想法一直困扰着我。当我最近进行研究时,我知道可以做到这一点。

A question was first asked about this problem 4 years ago on Reddit, but no solution other than @ViewChild was offered.

4年前在Reddit上首次提出了有关此问题的问题,但是除了@ViewChild之外没有其他解决方案。

However, with a commit made to the Angular Repo 8 months ago, the way to access the related component via the dom element was opened.

但是,随着8个月前对Angular Repo的提交 ,打开了通过dom元素访问相关组件的方式。

怎么做? (How is it done?)

When any click event on the screen is listened to with @HostListener, if your target element has the variable ‘__ngContext__’, it means that you can access the component instance from within this information.

当使用@HostListener监听屏幕上的任何单击事件时,如果目标元素具有变量'__ngContext__',则意味着您可以从此信息中访问组件实例。

@HostListener('click', ['$event.target'])clickAnyWhere(elem: any) {}

This means that within the ‘__ngContext__’ property, if index 0 is empty, it probably found “app-root”. If the 0th index is not empty, the 8th index will give us the component’s instance information.

这意味着在'__ngContext__'属性中,如果索引0为空,则可能找到“ app-root”。 如果第0个索引不为空,则第8个索引将为我们提供组件的实例信息。

findAngularComponent(element: any) {
    const MONKEY_PATCH_KEY_NAME = '__ngContext__';
    let comp;
    let process = true;
    while(process) {
      if(element[MONKEY_PATCH_KEY_NAME]){
        if(element[MONKEY_PATCH_KEY_NAME][0]) {
          comp = element[MONKEY_PATCH_KEY_NAME][8];
        } else {
          // app-root component
          comp = null;
        }
        process = false;
      } else {
        element = element.parentNode;
      }
    }
    return comp;
  }
Image for post
Image for post

I have written a small application that includes these explanations. As seen in the pictures above, there are 5 different components. Green, Red, Blue, Purple and Yellow Components. If you click on any component, you can access that component and change the information in it. Even when you click the “label” element in the red component, you can still access the component.

我已经编写了一个包含这些解释的小应用程序。 如上图所示,有5个不同的组件。 绿色,红色,蓝色,紫色和黄色组件。 如果单击任何组件,则可以访问该组件并更改其中的信息。 即使单击红色组件中的“标签”元素,您仍然可以访问该组件。

结果是 (As A Result)

By using this feature of Angular, you can dynamically select your components and make transactions through it. I think the following can be done using this feature.

通过使用Angular的此功能,您可以动态选择组件并通过它进行事务。 我认为使用此功能可以完成以下操作。

  • Application Usage Guides can be prepared.

    可以准备应用程序使用指南。
  • Security and Authorization Transactions can be dynamically managed without hard coded.

    安全和授权事务可以动态管理,而无需硬编码。

Demo Page : https://mcantonbul.github.io/Access-Component-Instance-Via-Dom/

演示页面 : https : //mcantonbul.github.io/Access-Component-Instance-Via-Dom/

If you want to review the sample project,Github: https://github.com/mcantonbul/Access-Component-Instance-Via-Dom

如果您想查看示例项目,请访问Github: https : //github.com/mcantonbul/Access-Component-Instance-Via-Dom

翻译自: https://medium.com/swlh/angular-accessing-the-component-instance-through-dom-element-d58a901a6c

vue组件转换成dom元素


http://www.niftyadmin.cn/n/1276370.html

相关文章

mysql计算2个日期的月份差和天数差

目录 ?一、计算两个日期的相隔月份 ? 二、计算2个日期的相隔天数 一、计算两个日期的相隔月份 格式: PERIOD_DIFF(startDate,endDate) 返回结果为月份数,格式必须是连续的数字,否则计算的结果可能会出错。 SELECT PERIOD_DIFF(201710, 201703);如果…

mysql 5.7 windows下root用户修改密码

新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码,输入update mysql.user set passwordpassword(root) where userroot时提示ERROR 1054 (42S22): Unknown column password in f…

hdfs dfs -du -h 或者 hadoop fs -du -h 输出三列数据的含义

第一列表示该目录下总文件大小 第二列表示该目录下所有文件在集群上的总存储大小 与你的副本数相关,我的副本数是3 , 所以第二列的是第一列的三倍 (第二列内容文件大小*副本数) 第三列表示你查询的目录

mysql远程登录配置

允许root用户在任何地方进行远程登录,并具有所有库任何操作权限,具体操作如下: 1)在本机先使用root用户登录mysql: 命令行执行:mysql -u root -p 输入密码(第7步中设置的密码)&am…

HDFS中block的大小

block的大小与修改 Hadoop2.x/3.x版本中Block的默认大小为128M&#xff0c;早前版本中默认为64M&#xff0c;开发人员可以在hdfs-site.xml中添加如下标标签进行修改。 <!-- hdfs-site.xml --> <property><name>dfs.blocksize</name>#value值代表blo…

springBoot整合SqlSessionTemplate使用

实际开发中我们操作数据库持久化&#xff0c;总是需要写重复的mapper&#xff0c;service&#xff0c;xml浪费了我们大量的时间&#xff0c;在这里推荐大家使用SqlSessionTemplate 1.application.xml配置 server.port8081 #server.servlet.context-path/recordLog#数据源必填…

mysql 5.7 invalid default value for timestamp field

这是因为sql_mode中的NO_ZEROR_DATE导制的。 在strict mode中不允许0000-00-00作为合法日期&#xff0c;可以使用 show variables like sql_mode查看sql_mode&#xff0c;一种是把sql_modeSTRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUT‌​ION 另一种设置AL…

vue nodejs 构建_如何使用nodejs后端打字稿版本开发和构建vue js应用

vue nodejs 构建There are so many ways we can build Vue.js apps and ship for production. One way is to create the Vue app with NodeJS or Java, and another way is to develop and serve that static content with the NGINX web server. With NodeJS, we have to dea…