Vue3的组合式api的那些不太方便的地方

Vue3的组合式api很香,但是也有一些不太方便的地方。下面给出一种解决方案,整理自codewhy老是的视频课。关于watch的原理还不太明白,后期补上

  1. watch

    在vue3中观察一个响应式对象时,得到的新值和旧值是一样的,这在有时候就不方便。当然也有变通方法,如下:

const objWatch = reactive({ name: 'parade', child: { age: 23 } })//响应式对象
const changeReactive = () => {
  objWatch.child.age++
}//改变响应式对象
//观察
watch(
  () => {
    return JSON.stringify(objWatch)
  },
  (newValue, oldValue) => {
    console.log(JSON.parse(newValue), JSON.parse(oldValue))
  }
)
  1. vuex中的map系列函数

    在vue3中组合式api中使用vuex的map系列函数时,是无法使用展开语法的。当然也有变通方法,如下经过封装过的使用方法:

    //useMappers.js
    import { useStore, mapMutations, mapActions } from 'vuex'
    import { computed } from 'vue'
    export function useMapper(mapper, mapperFn) {
      const store = useStore()
      //获取到对应的对象的function,{name:function,age:function}
      const storeStateFns = mapperFn(mapper)
    
      const storeState = {}
      Object.keys(storeStateFns).forEach((fnkey) => {
        const fn = storeStateFns[fnkey].bind({ $store: store })
        if (mapMutations === mapperFn || mapActions === mapperFn) {
          storeState[fnkey] = fn//mapMutations和mapActions的处理不太一样,不需要使用计算属性重新计算
        } else {
          storeState[fnkey] = computed(fn)
        }
      })
    
      return storeState
    }
    //useGetters.js
    import { useMapper } from './useMapper'
    import { mapGetters, createNamespacedHelpers } from 'vuex'
    export function useGetters(mapper, moduleName) {//第二个参数为vuex的模块名称,没有的可以不传
      let mapperFn = mapGetters
      if (moduleName) {
        mapperFn = createNamespacedHelpers(moduleName).mapGetters
      }
      return useMapper(mapper, mapperFn)
    }
    //useMutations.js
    import { mapMutations, createNamespacedHelpers } from 'vuex'
    import { useMapper } from './useMapper'
    export function useMutations(mapper, moduleName) {
      let mapperFn = mapMutations
      if (moduleName) {
        mapperFn = createNamespacedHelpers(moduleName).mapMutations
      }
      return useMapper(mapper, mapperFn)
    }
    //index.js
    import { useGetters } from './useGetters'
    import { useState } from './useState'
    import { useMutations } from './useMutations'
    import { useActions } from './useActions'
    export { useGetters, useState, useMutations, useActions }
    
    //组件中使用
    <script setup>
    import { useState, useGetters, useMutations, useActions } from '@/hook'
    import { INCREMENT_N } from '@/store/mutation-type'
    const { incre, decre, increN, homeIncrement } = useMutations({
      incre: 'increment',
      decre: 'decrement',
      increN: INCREMENT_N,
      homeIncrement: 'home/homeIncrement'
    })
    const { incrementAction, ['home/homeActions']: homeActions } = useActions([
      'incrementAction',
      'home/homeActions'
    ])
    const { name, age } = useState(['name', 'age'])
    const { sName, sAge } = useState({
      sName: (state) => state.name,
      sAge: (state) => state.age,
      homeCount1: (state) => state.home.homeCount
    })
    const { homeCount } = useState(['homeCount'], 'home')
    const {
      nameInfo,
      ageInfo,
      ['home/homeDobule']: homeDobule
    } = useGetters(['nameInfo', 'ageInfo', 'home/homeDobule'])
    const { sNameInfo, sAgeInfo, homeGetters } = useGetters({
      sNameInfo: 'nameInfo',
      sAgeInfo: 'ageInfo',
      homeGetters: 'home/homeDobule'
    })
    </script>
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,可以使用`emit`指令来实现组件之间的通信。通过`emit`指令,子组件可以向父组件发送自定义事件,并且可以传递数据给父组件。在使用`emit`指令时,需要在子组件中定义一个事件,并且在需要的地方触发这个事件,父组件通过监听这个事件来获取子组件传递的数据。 具体的使用方法如下: 1. 在子组件中,使用`$emit`方法来触发事件,并且传递需要传递的数据。例如,子组件中定义了一个事件名为`update`,可以在需要的地方使用`this.$emit('update', data)`来触发这个事件,并且传递`data`数据给父组件。 2. 在父组件中,通过在子组件上使用`@事件名`的方来监听子组件触发的事件。例如,如果子组件的事件名为`update`,可以在父组件中使用`<子组件 @update="handleUpdate"></子组件>`来监听这个事件,并且在`handleUpdate`方法中处理子组件传递的数据。 通过使用`emit`指令,可以实现父组件向子组件传递数据(通过props),同时子组件也可以通过触发事件向父组件传递数据,实现了双向通信。这种用法在Vue3中被称为组合API的一部分,可以更方便地实现组件之间的交互和数据传递。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [使用 Vue3 开发了四个月,回顾 emit 的用法](https://blog.csdn.net/fang_my/article/details/127759757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值