首页 > 其他分享 >Vue3开发效率总结

Vue3开发效率总结

时间:2023-02-27 16:25:59浏览次数:59  
标签:总结 count vue return computed Vue3 const 效率 store

https://zhuanlan.zhihu.com/p/601715098

依赖注入

依赖注入:将实例变量传入到一个对象中去
在Vue中父组件中声明依赖,将他们注入到子孙组件实例中去,很大程度上代替全局状态管理的存在

// 代码案例
// ========================= parent.vue ========================= 
<template>
    <child @setColor="setColor"></child>
    <button @click="count++">添加</button>
</template>

<script >
import { defineComponent, provide, ref } from "vue";
import Child from "./child.vue";
export default defineComponent({
    components: {
        Child
    },
    setup() {
        const count = ref(0);
        const color = ref('#000')
        provide('count', count)
        provide('color', color)
        function setColor(val) {
            color.value = val
        }
        return {
            count,
            setColor
        }
    }
})
</script>
// ========================= child.vue ========================= 
//使用inject 注入
<template>
    <div>这是注入的内容{{ count }}</div>
    <child1 v-bind="$attrs"></child1>
</template>

<script>
import { defineComponent, inject } from "vue";
import child1 from './child1.vue'
export default defineComponent({
    components: {
        child1
    },
    setup(props, { attrs }) {
        const count = inject('count');
        console.log(count)
        console.log(attrs)
        return {
            count
        }
    }
})
</script>

Vue3基础使用

<template>
  <div ref='composition'>测试Composition API</div>
</template>
<script>
  import {inject, ref, onMounted, computed, watch} from 'vue';
  export default {
    set(props, {attrs, emit, slots, expose}) {
      // 获取页面元素
      const composition = ref(null);
      // 依赖注入
      const count = inject('foo', '1');
      // 响应式结合
      const num = ref(0);
      // 钩子函数
      onMounted(() => {
        console.log('这是个钩子');
      }); 
      // 计算属性
      computed(() => num.value + 1)
      // 监听值的变化
      watch(count, (count, preCount) => {
         console.log('这个值改变了');
      });
      return {
        num,
        count,
      }
    }
  }
</script>

通过getCurrentInstance获取组件实例

getCurrentInstance支持访问内部组件实例,通常情况下被放在setup中获取组件实例
getCurrentInstance只暴露给高阶使用场景。
getCurrentInstance的主要作用:【逻辑提取】替代Mixin
=> 抽取通用逻辑提高系统内聚性,降低代码耦合度。
如下element-plus代码中利用getCurrentInstance 获取父组件parent中的数据,分别保存到不同的变量中,我们只需要调用当前useMapState即可拿到数据

// 保存数据的逻辑封装
function useMapState<T>() {
  const instance = getCurrentInstance()
  const table = instance.parent as Table<T>
  const store = table.store
  const leftFixedLeafCount = computed(() => {
    return store.states.fixedLeafColumnsLength.value
  })
  const rightFixedLeafCount = computed(() => {
    return store.states.rightFixedColumns.value.length
  })
  const columnsCount = computed(() => {
    return store.states.columns.value.length
  })
  const leftFixedCount = computed(() => {
    return store.states.fixedColumns.value.length
  })
  const rightFixedCount = computed(() => {
    return store.states.rightFixedColumns.value.length
  })

  return {
    leftFixedLeafCount,
    rightFixedLeafCount,
    columnsCount,
    leftFixedCount,
    rightFixedCount,
    columns: store.states.columns,
  }
}

善于使用$attrs 组件的事件以及props透传

https://juejin.cn/post/7080875763162939429#heading-12

优雅注册全局组件技巧

https://juejin.cn/post/7080875763162939429#heading-18

标签:总结,count,vue,return,computed,Vue3,const,效率,store
From: https://www.cnblogs.com/openmind-ink/p/17160114.html

相关文章

  • 2023.2.27周一每日总结
    今天上课老师讲解了程序的组成,以及在我们以后的编程过程中最重要的是什么,通过课堂练习帮助我们理解如何逐步优化自己的代码,进而使程序更加简单易懂,举了飞机失事时程序出......
  • 基础知识总结02
    Day05方法方法是一种语法结构,就是一段功能结构封装在一个方法中。方便重复调用。具有特殊功能的代码块特殊功能:方法里面的代码写了啥,这个方法就具备什么功能。代码块:......
  • 算法刷题 Day 57 | ● 647. 回文子串 ● 516.最长回文子序列 ● 动态规划总结篇
    详细布置647.回文子串动态规划解决的经典题目,如果没接触过的话,别硬想直接看题解。https://programmercarl.com/0647.%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.htm......
  • javascript 高级编程 之 Array 用法总结
    引用类型是一种数据结构,用于将数据和功能联系起来。创建对象的方式:1.new操作符vararray=newArray();2.字面量表示法创建vararray=[];Array检测数组:检测数组......
  • Go组件库总结之介入式链表
    本篇文章我们用Go封装一个介入式的双向链表,目的是将链表的实现和具体元素解耦。文章参考自:https://github.com/brewlin/net-protocol1.元素的接口typeElementinterface......
  • 推荐系统[八]算法实践总结V0:腾讯音乐全民K歌推荐系统架构及粗排设计
    1.前言:召回排序流程策略算法简介推荐可分为以下四个流程,分别是召回、粗排、精排以及重排:召回是源头,在某种意义上决定着整个推荐的天花板;粗排是初筛,一般不会上复杂模型;......
  • Redis高频面试题总结
    前言大家好,我是小卷聊开发。春暖花开即将到来,整理了13道Redis高频面试题,有些不全面还请谅解,感谢观看!!!1.Redis过期键的删除策略定时删除:在设置键的过期时间的同时,创建......
  • vue3 门户网站搭建4-mockjs
    在后端接口没做好之前,为了更好的模拟接口返回,引入mockjs。它可以拦截ajax请求,生成伪数据。 1、安装:npmimokjs-D、npmi vite-plugin-mock-D2、在vite.confi......
  • 推荐系统[八]算法实践总结V0:腾讯音乐全民K歌推荐系统架构及粗排设计
    1.前言:召回排序流程策略算法简介推荐可分为以下四个流程,分别是召回、粗排、精排以及重排:召回是源头,在某种意义上决定着整个推荐的天花板;粗排是初筛,一般不会上复杂模型......
  • vue3 + vite4 + vue-router4动态路由存在的问题
    使用vite4、vue3、vue-router4搭建动态路由的时候遇到的问题及解决方法解决!!我使用的是登录接口返回菜单,使用pinia存储菜单数据,使用pinia-plugin-persist加js-cookie进行......