attached
attached
生命周期鉤子會在組件被添加到文檔中時觸發。在這個階段,適合獲取組件內元素的尺寸相關信息,進行數據綁定和全局事件的操作。
示例代碼
<div id="logger">-</div>
<div style="color:red;">shadow html : <span id="shadowHtml"></span></div>
<script>
setTimeout(()=>{
const ele = document.createElement('test-ready');
setTimeout(()=>{
$('body').push(ele);
},500);
},500);
</script>
<div id="logger">-</div>
<div style="color:red;">shadow html : <span id="shadowHtml"></span></div>
<script>
setTimeout(()=>{
const ele = document.createElement('test-ready');
setTimeout(()=>{
$('body').push(ele);
},500);
},500);
</script>
<template component>
<div>test ready</div>
<script>
let count = 0;
export default {
tag: "test-ready",
ready(){
count++;
$("#logger").text = count;
$('#shadowHtml').text = this.shadow ? this.shadow.html : 'null';
},
attached(){
count++;
$("#logger").text = count;
}
};
</script>
</template>