从Web组件内部访问html标记

伊万

我正在学习js Web组件的第一个概念。我非常喜欢这些内容,并尝试举一个简单的例子。我的组件只是一个假装将颜色更改为div的按钮。

我的示例工作是我所期望的,但是我注意到,如果我尝试更改的元素位于Web组件中而不是Web组件之外,那么我的方法并不是太多的“组件方式”。

这是我的html文件:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <div class="main">
      <h1>Yeah Web Components!</h1>
      <my-component></my-component>
    </div>

    <script src="myComponent.js"></script>
  </body>
</html>

这是我的组件.js:

const template = `
  <style>
    .container{
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .box{
        width: 100px;
        height: 100px;
        background: red;
    }
    .hi-button{
      margin-top: 10px;
    }
    
  </style>
  <div class="container">
    <div class="box"></div>
    <button class="hi-button">Change</button>
  </div>
 
`;

class MyFirstTest extends HTMLElement{
  constructor(){
    super()
    const shadowRoot = this.attachShadow({ mode: 'open' }); 
    shadowRoot.innerHTML = template; 
  }

  changeButtonColor(){
      const box = this.shadowRoot.querySelector(".box");
      if(box.style.background === 'red'){
        box.style.background = 'blue';
      }else{
        box.style.background = 'red';
      }
  }

  connectedCallback(){
      const event = this.shadowRoot.querySelector(".hi-button");
      event.addEventListener('click', () => this.changeButtonColor());
  }

  disabledCallback(){
    const event = this.shadowRoot.querySelector(".button-test");
    event.removeEventListener();
  }

}

customElements.define('my-component', MyFirstTest);

正如我所说的,该功能可以正常工作,但我不希望div位于Web组件中,而将html文件和mi组件仅作为按钮。

例如,我的html文件将如下所示:

.......

  <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <div class="main">
      <h1>Yeah Web Components!</h1>
      <div class="my-div-to-change"></div>
      <my-component></my-component>
    </div>
  </body>

.......

Web组件是否可以这种方式工作?

JF

维护组件之间独立性的最佳方法是使用events

Web组件调度一个由父容器侦听的事件。然后,父母做必要的动作。

如果您想在组件和父组件之间“直接”对话,则该系统耦合度很高,不是一个好方法。当然可以,不推荐使用bur。

您可以检查答案是否更清楚。

另外,使用事件回答您的问题。就这么简单:

首先,必须以这种方式将事件分派到组件中:

this.dispatchEvent(new CustomEvent("button-clicked", { 
    bubbles: true,
}));

您可以在这里检查用法。

此外,家长将使用以下方法收听:

document.addEventListener("button-clicked", changeColor);

因此,单击按钮时,父级将changeColor使用所需的内部逻辑触发功能

这样,您正在使用Web组件对父容器执行操作,但系统未耦合。父母可以没有孩子工作,孩子可以没有父母的工作。两者都可以单独使用。当然,该事件不会被分派或侦听,但是组件之间没有依赖关系。

这也是一个例子。

const template = `

  <div class="container">

    <button class="hi-button">Change</button>
  </div>
 
`;

class MyFirstTest extends HTMLElement{
  constructor(){
    super()
    const shadowRoot = this.attachShadow({ mode: 'open' }); 
    shadowRoot.innerHTML = template; 
  }

  changeButtonColor(){
      //Call parent
      this.dispatchEvent(new CustomEvent("button-clicked", { 
            bubbles: true,
        }));
  }

  connectedCallback(){
      const event = this.shadowRoot.querySelector(".hi-button");
      event.addEventListener('click', () => this.changeButtonColor());
  }

  disabledCallback(){
    const event = this.shadowRoot.querySelector(".button-test");
    event.removeEventListener();
  }

}

customElements.define('my-component', MyFirstTest);
<!DOCTYPE html>
<html lang="en">
  <head>
   <style>
    .container{
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .box{
        width: 100px;
        height: 100px;
        background: red;
    }
    .hi-button{
      margin-top: 10px;
    }
    
  </style>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <div class="main">
      <h1>Yeah Web Components!</h1>
      <div class="box"></div>
      <my-component></my-component>
    </div>

    <script src="myComponent.js"></script>
    <script>
    //Add listener to do the action
    document.addEventListener("button-clicked", changeColor);
    function changeColor(){
      var box = document.querySelector(".box");
      if(box.style.backgroundColor === 'red'){
        box.style.backgroundColor = 'blue';
      }else{
        box.style.backgroundColor = 'red';
      }
    }
    </script>
  </body>
</html>

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

访问不是组件或html标签的组件标签的内部文本?

来自分类Dev

访问不是组件或html标签的组件标签的内部文本?

来自分类Dev

ReactJS:访问组件“父级”内部的组件

来自分类Dev

访问HTML标记的不同元素

来自分类Dev

Beautifulsoup访问嵌套HTML标记

来自分类Dev

从自身访问Web组件元素

来自分类Dev

如何正确访问组件内部的道具

来自分类Dev

访问VusJS中子组件内部的插槽功能

来自分类Dev

如何从内部框架访问JFrame中的组件

来自分类Dev

在1.5中访问组件$ scope内部指令

来自分类Dev

如何在组件类内部访问formGroup

来自分类Dev

如何访问外部组件的内部元素

来自分类Dev

如何从HTML标记中提取内部文本?

来自分类Dev

仅删除锚标记,但保留内部html?

来自分类Dev

访问 ... 子组件的父组件的父组件的 html 元素

来自分类Dev

在ember中访问Controller的根html标记

来自分类Dev

如何访问HTML标记中的JavaScript变量?

来自分类Dev

在ember中访问Controller的根html标记

来自分类Dev

Web组件-服务/非HTML组件

来自分类Dev

html 不会从 angular 2 组件内部呈现

来自分类Dev

无法使用Android从测试源访问内部组件

来自分类Dev

Vue JS-访问组件内部的根计算属性

来自分类Dev

使用函数内部的逻辑条件访问全局矢量组件

来自分类Dev

从Watcher内部的Vue类组件装饰器访问方法

来自分类Dev

Mule:从foreach内部的组件访问计数器变量

来自分类Dev

从标记帮助器内部,如何访问不是标记帮助器属性的属性?

来自分类Dev

如何从组件访问父HTML元素?

来自分类Dev

访问React组件内的HTML元素

来自分类Dev

在HTML窗口之间传递COM并访问组件

Related 相关文章

热门标签

归档