深色模式
最近在团队中落地Vue 3.2 script-setup 实践,积累了不少经验。整理出来供参考,希望对做类似工作的同学有所帮助。
核心概念
我们可以通过以下方式来改进:
javascript
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}
return { ...toRefs(state), doubled, increment }
}这套方案已经在线上稳定运行了半年以上,经过了实际验证。
深度解析
先来看基本的实现方式:
javascript
import { ref, computed, watch, onMounted } from 'vue'
export default {
setup() {
const count = ref(0)
const doubled = computed(() => count.value * 2)
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`)
})
onMounted(() => { console.log('组件已挂载') })
return { count, doubled }
}
}这段代码展示了基本的使用方式。实际项目中还需要考虑错误处理和边界条件。
落地经验
在这个基础上,我们可以进一步优化:
javascript
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}
return { ...toRefs(state), doubled, increment }
}这种模式在大型项目中非常实用,能显著降低维护成本。
调优策略
实际项目中的用法会更复杂一些:
javascript
import { ref, computed, watch, onMounted } from 'vue'
export default {
setup() {
const count = ref(0)
const doubled = computed(() => count.value * 2)
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`)
})
onMounted(() => { console.log('组件已挂载') })
return { count, doubled }
}
}通过这种方式,代码的可测试性和可扩展性都得到了提升。
注意事项
以下是一个完整的示例:
javascript
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}
return { ...toRefs(state), doubled, increment }
}注意边界条件处理,这在生产环境中至关重要。
小结
- 团队协作中约定和文档比技术本身更重要
- 关注社区动态,技术方案需要持续迭代
- 不要为了用新技术而用新技术
- 代码示例仅供参考,需根据业务场景调整
- Vue 3.2 script-setup 实践不是银弹,需要根据项目规模和技术栈选择