闽公网安备 35020302035485号
const activeName = ref('firstTab')
// 查询列表
getList().then((res)=> {...})
// 查询当前时间
getCurrentTime((res)=> {...})
// 设置状态管理
setStore({'curr': activeName})
// 显示一个弹窗
if(...) {
dialog(...)
}
其实可能当时开始开发的时候,这么一看是没有问题的,但后面加了一个变量,就变成了const activeName = ref('first')
const currentType = store.state.cate
// 堆代码 duidaima.com
// 查询列表
getListApi().then((res) => {
if(currentType)
... // 各种逻辑和序列化
})
// 查询tab的状态
getCurrentTabStatusApi((res) => {
... // 各种逻辑和序列化
})
// 设置状态管理
if(!currentType) {
setStore({'curr': activeName})
} else {
setStore(...)
}
// 显示一个提示弹窗
dialog(() => {
if(...){}else{}
})
后面再加一个变量,这代码就没办法控制了,事实上这里的代码量比我描述要恐怖很多。在vue开发过程中, MVVM 的设计模式下,如果模块化做得不够细,会让 viewModel 变得非常复杂,变得复杂的同时无法复用或者移植。一般来说在比较复杂的应用中,页面级别的模块只做对各个子组件的调用,流程控制以及页面级别的变量控制(单一职责)。另外,在钩子函数(或其他 控制器 )不应该写具体的代码实现(单一职责),而应该只是调用 具体/抽象 的实现;
const activeName = ref('first')
const currentType = store.state.cate
onMounted(() => {
setUserState()
showDialog()
})
// 设置用户当前状态
function setUserState() {
if(!currentType) {
setStore({'curr': activeName})
} else {
setStore(...)
}
}
// 是否显示弹窗, 显示的弹窗类型
function showDialog() {
dialog(() => {
if(currentType){}else{}
})
}
<template>
<Tab :activeName="activeName" />
</template>
依赖倒置// dialog.component.vue 三种弹窗类型, normal/newUser/coupon
<script setup>
const props = defineProps({
dialogType: {
type: String,
default: 'normal'
},
})
</script>
<template>
<el-dialog>...</<el-dialog>
</template>
数据源过多<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const currentId = computed(() => route.query.id)
</script>
但后面为了方便(不用每个页面都用url参数传递),又将路由参数写入了 pinia 中给其他 页面/组件 读取;后面的 页面/组件 有时候又将参数封装到响应式对象中:import { reactive } from 'vue'
import { useStore } from '@/store/modules/group'
const store = useStore()
const data = reactive({
id: store.gInfo.id
})
不管是对自己或者其他协同开发的人来说都很难处理(用哪个数据读?用哪个数据写?几个数据源如何同步?).<script setup> // 第一部分是 引入依赖,变量声明 // 第二部分是 watch api/vue生命周期钩子等 // 第三部分是 自定义方法 </script> <template> </template> <style lange='less' scoped> </style>