香草网状组件结构

编程入门 行业动态 更新时间:2024-10-10 04:19:16
本文介绍了香草网状组件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究构建香草网络组件。我以前使用过Polymer,你可以将模板,样式和JavaScript放在一个文件中。如果可能的话,我希望用'vanilla'网页组件实现这一目标,但无法解决问题。我从此处获取了代码并将其添加到文件中我使用如下:

I am looking into structuring vanilla web-components. I have previously used Polymer and like the fact that you can have the template, styles and JavaScript in one file for your component. I want to achieve this with 'vanilla' web components if possible but can't work out how. I've taken the code from here and added it to a file which I am using as follows:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Component test</title> <link rel="import" href="x-foo-from-template.html"> </head> <body> <x-foo-from-template></x-foo-from-template> </body> </html>

这是失败的,因为当我们尝试选择模板时它不存在,因为此时模板是不在DOM中(对吧?)。

This fails because when we try to select the template it does not exist because at that point the template is not in the DOM (right?).

有没有办法实现这个目标?我个人更喜欢这种方法在JavaScript中使用 document.createElement 创建HTML。

Is there any way to achieve this? I personally prefer this approach to creating the HTML in the JavaScript using document.createElement.

推荐答案

从导入的文档中获取模板有两种主要方法:

There are 2 main methods to get the template from the imported document:

1。来自< link> 元素的 import 属性

1. From the the import property of the <link> element

< link rel = import> 元素拥有 import 包含导入文档的属性。 您可以执行 querySelector 调用来获取< template> :

The <link rel=import> elements own an import property that contains the imported document. You can perform a querySelector call on it to fetch the <template>:

var doc = document.querySelector( 'link[href$="x-foo-from-template.html"]').import var template = doc.querySelector( 'template' )

然后在自定义中导入模板元素(或在其Shadow DOM中)使用 importNode()或 cloneNode()。

Then import the template in the custom element (or in its Shadow DOM) using either importNode() or cloneNode().

2。形成 currentScript的 ownerDocument 属性

解析脚本时,全局值 文档.currentScript 引用正在解析的脚本,因此它的属性 ownerDocument 是对拥有该脚本的文档的引用。您可以对其执行 querySelector 调用:

When a script is parsed, the global value document.currentScript references the script being parsed, and therefore its propery ownerDocument is a reference to the document that owns the script. You can perform a querySelector call on it:

var template = document.currentScript.ownerDocument.querySelector( 'template' )

注意: currentScript 值设置为暂时,因此在后续调用中不再有效,例如 connectedCallback()或 attachedCallback(),因此您必须在解析时将其记忆在持久变量中,以便在以后需要时重复使用。

Note: the currentScript value is set temporarily, so it won't work any more in subsequent calls, like connectedCallback() or attachedCallback(), so you'll have to memorize it in a persistent variable at parse time, to reuse it later when needed.

更多推荐

香草网状组件结构

本文发布于:2023-11-22 22:19:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网状   香草   组件   结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!