Vue3开发效率总结
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
优雅注册全局组件技巧
学而不思则罔,思而不学则殆!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具