无界微前端 - 前置知识1(dom与template绑定、生命周期、传递参数)
web-components
tsc -w 时时编译te为js
tes --init 初始化tesconfig.json
index.ts
window.onload = () => {
class Wujie extends HTMLElement {
constructor() {
super();
let dom = this.attachShadow({mode:'open'})
let template = document.querySelector("#wujie") as HTMLTemplateElement
dom.appendChild(template.content.cloneNode(true))
console.log('getAttr',this.getAttr('url'));
}
private getAttr(atrr:string){
return this.getAttribute(atrr)
}
connectedCallback(){
console.log('111');
}
disconnectedCallback(){
console.log('2');
}
attributeChangeCallback(name:any,oldVal:any,newVal:any){
console.log('name',name,'oldVal',oldVal,'newVal',newVal);
}
}
window.customElements.define("wu-jie", Wujie);
};
index,html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<wu-jie url="testURL"></wu-jie>
<div>我是外div</div>
<!-- 把当前id 渲染到 wujie组件里面 -->
<template id="wujie">
<div>111</div>
<style>
div {
color: red;
}
</style>
</template>
</body>
<script src="./index.js"></script>
</html>
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8d7807ac4c1f7e33e3497ff140c8d547.png)