watch
Once the ready
stage is completed, the listening function in the associated watch
object will be triggered once. Afterwards, when a value of the data changes, the corresponding key's listening function will be triggered again.
Example code
<div id="logger">-</div>
<div style="color:red;">shadow html : <span id="shadowHtml"></span></div>
<script>
setTimeout(()=>{
const ele = document.createElement('test-ready');
},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');
},500);
</script>
<template component>
<div>test watch</div>
<script>
let count = 0;
export default {
tag: "test-ready",
data:{
val: "I am val"
},
watch:{
val(val){
count++;
$("#logger").text = count;
$("#shadowHtml").push(`<div>${val}</div>`);
}
},
ready(){
setTimeout(()=>{
this.val = 'change val';
},500);
}
};
</script>
</template>