📜  Vue 3 从另一个组件调用方法 - Html 代码示例

📅  最后修改于: 2022-03-11 14:53:05.707000             🧑  作者: Mango

代码示例2
// Register plugin to provide $externalMethods for components
  app.use({
    methods: {},
    install(app) {
      app.config.globalProperties.$externalMethods = {
        set: (method, func) => this.methods[method] = func,
        call: (method, ...args) => this.methods[method](...args)
      }
    }
  });

  app.component('button1', {
    template: '#test-button',
    methods: {
      click(event, text) {
        console.log(text || 'Button 1 default click')
      }
    },
    mounted: function () {
      console.log(this);
      // Register global function
      this.$externalMethods.set('button1/click()', this.click)
    }
  });

  app.component('button2', {
    template: '#test-button',
    methods: {
      click(event) {
        // Call global function
        this.$externalMethods.call('button1/click()', event, 'Hello from Button 2')
      }
    }
  });