[英]Could Web components (JavaScript native) be registered in the end of HTML's body?
Can I build my JavaScript application to single bundle file including Web Components and link it to end of body
?我可以将我的 JavaScript 应用程序构建为包含 Web 组件的单个捆绑文件并将其链接到
body
末尾吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My application</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- must work -->
<local-example_component></local-example_component>
<!-- includes "local-example_component" implementation -->
<script src="Application.js"></script>
</body>
</html>
It does NOT matter WHEN the customeElements.define()
script is executed执行
customeElements.define()
脚本时无关紧要
Source:来源:
https://developers.google.com/web/fundamentals/web-components/customelements#upgrades https://developers.google.com/web/fundamentals/web-components/customelements#upgrades
Custom elements can be used before their definition is registered.
可以在注册定义之前使用自定义元素。
Progressive enhancement is a feature of custom elements.
渐进增强是自定义元素的一个特性。
You can declare a bunch of elements on the page and never invokecustomElements.define('my-element', ...)
until much later.您可以在页面上声明一堆元素
customElements.define('my-element', ...)
直到很久以后才调用customElements.define('my-element', ...)
。
This is because the browser treats potential custom elements differently thanks to unknown tags .这是因为由于未知标签,浏览器会以不同的方式对待潜在的自定义元素。
The process of callingdefine()
and enriching an existing element with a class definition is called "element upgrades" .调用
define()
并使用类定义丰富现有元素的过程称为“元素升级” 。
You can load/inject/execute script anytime:您可以随时加载/注入/执行脚本:
function upgrade() { customElements.define('my-element', class extends HTMLElement { connectedCallback() { this.innerHTML = this.nodeName + ' defined!!'; } }) }
body { font-family: Arial; font-size: 1.5em; } *:not(:defined) { color: red; } my-element { color: green; }
<my-element><my-element>unknown Element</my-element></my-element> <br> <button onclick="upgrade()"> Upgrade </button>
Script can be loaded any where in html.脚本可以在 html 中的任何位置加载。 However, It is suggested to before body inside
head
with defer attribute statement.但是,建议在
head
body 之前加上defer 属性声明。
The defer attribute is a boolean attribute. When present, it specifies that the script is executed when the page has finished parsing.
Basic def: https://w3schools.com/tags/att_script_defer.asp基本定义: https : //w3schools.com/tags/att_script_defer.asp
There few more supporting docs and best practice can be found
可以找到更多的支持文档和最佳实践
https://developers.google.com/web/fundamentals/web-components/customelements https://developers.google.com/web/fundamentals/web-components/customelements
<script defer src="main.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Life cycle callbacks test</title>
<script defer src="main.js"></script>
</head>
<body>
<h1>Life cycle callbacks test</h1>
</body>
</html>
Look at the example:'看例子:'
https://github.com/mdn/web-components-examples/blob/master/life-cycle-callbacks/index.html https://github.com/mdn/web-components-examples/blob/master/life-cycle-callbacks/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.