vue 动态添加方法 问题

当前场景需要等待一个 promise 的完成后 ,给 vue 的选项动态添加 方法
let func = new Function(`  const code = \`
    let a = 1
    export function getA(){
      return a
    }
  \`
  const objectURL = URL.createObjectURL(new Blob([code], { type: 'text/javascript' }))
  return import(objectURL);
`)
func().then(module=>{
    console.log(module.getA) // 可以拿到getA
})
// func 内部有我需要的方法 getA
// 我怎么把它弄到vue的method中呢
// 上文 code 代码块必须要执行 而且getA方法必须能访问到a
实现目标: 
主要是为了实现 vue 组件的事件绑定,vue组件事件绑定的方法必须要在 vue 的 option中声明过。
回答:
事件绑定可以不直接绑定getA,由中间函数中转一次,比如click事件
<div @click="handClick"></div>handClick(){
    if(typeof this.xxx == 'function'){
        this.xxx()
    }
}
func().then(module=>{
    console.log(module.getA) // 可以拿到getA
    this.xxx = module.getA
})
以上是 vue 动态添加方法 问题 的全部内容, 来源链接: utcz.com/p/936109.html








