每當頁面上發生按鈕或其他操作時,我都會嘗試在 svelte 中創建同一組件的新實體,而不必在它們之上創建一個串列和 {each}。我只想做一些類似的事情new Component(some context data)然后忘記。另一個問題是我不希望組件在洗掉父級時消失。
謝謝
uj5u.com熱心網友回復:
只需將其直接安裝在document.body:
new Component({ target: document.body })
但請注意,如果您自己不$destroy這樣做,最終會導致記憶體泄漏。
當我做這樣的事情時,例如通知,我從組件中調度一個事件,告訴呼叫代碼何時可以安全地銷毀組件。就像是:
const notification = new Notification({ target: document.body, ... });
notification.$on('close', () => notification.$destroy());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/497967.html
標籤:javascript 苗条
