为什么在 Vue plug 或 Mixin 中挂载了几次?

Why does mounted in Vue plug or in Mixin work several times?

为什么它会重复自己以及如何防止它并使其只发生一次?是错误吗?在插件中:

const globala = {
  install(Vue) {
    Vue.mixin({
      mounted() {
        console.log('hi')
      }
    })
  }
}

这里只是 mixin:

Vue.mixin({
  mounted() {
    console.log('hi')
  }
})

mixin 可以是全局的,也可以是组件局部的。如果你定义它globally,它将被应用到之后创建的每个组件:

Vue.mixin({
  mounted() {
    console.log('hi')
  }
})

如果您定义它locally,它将只应用于您手动添加它的组件:

const myMixin = {
  mounted() {
    console.log('hi')
  }
}

new Vue({
  el: "#app",
  mixins: [myMixin] // Only added to this component
});

您已经定义了全局混合,因此之后创建的每个组件都将实现 mounted 挂钩。