闭包加组合式API

71 阅读1分钟

首先, 声明一个函数

   const composition = () => {
      // 在该函数中定义数据和方法
      const show = true
      const fn = (type) =>{
         // 公共函数利用传参实现功能
         if(type === '?') {
            show = false
         }
      }
      
      return {   // 将方法和属性return出去
         show,
         fn
      }
   }

如果实在Vue框架中, 可以利用导入导出的思想, 将上述方法导出

export composition

然后在对应文件中引入

   import { compositon } from './'
   // 调用并解构该函数
   const { show, fn } = compositon()
   // 可以直接调用该方法和属性