detached
detached
lifecycle hook is triggered when a component is removed from the document. During this stage, you can perform some cleanup operations, such as unregistering event listeners or releasing resources, to prevent memory leaks.
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');
setTimeout(()=>{
$('body').push(ele);
setTimeout(()=>{
$(ele).remove();
},500);
},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);
setTimeout(()=>{
$(ele).remove();
},500);
},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;
},
detached(){
count++;
$("#logger").text = count;
}
};
</script>
</template>