Vue没有提供一个内置的方式来观察插槽的变化,但是你可以使用 MutationObserver API
来对插槽内容的变化做出调整。它是一个内置的浏览器API,因此与框架无关。
MutationObserver 接口提供了监听对 DOM 树所更改的能力。它被设计为旧的 Mutation Events 功能的替代品,该功能是 DOM3 Events 规范的一部分。
export default {
mounted(){
const observer = new MutationObserver(this.update);
observer.oserve(this.$el, {
childLis: true,
subtree: true,
})
}
}