派發(fā)器抽離vue2單組件中的大量邏輯技巧
概述
在vue2當中,我們寫的邏輯都是options配置對象,比較固定的寫法,方法,數(shù)據(jù),都對應到各自的配置項當中,但是當一個組件的邏輯變得更加復雜,邏輯也越來越多的時候,這時候,我們一個組件避免不了要寫大量的邏輯代碼,在2版本中,使用官方推薦的mixin可以解決一個組件邏輯過多的問題,現(xiàn)在記錄我在工作用用到的另外方法。
本質(zhì)
在react中,我們使用redux,會接觸到對應的action,reducer,dispatch等等,通過派發(fā)對應事件類型,然后觸發(fā)對應邏輯修改,有點像發(fā)布訂閱,哈哈,在這里,借鑒react當中使用redux的思路,記錄vue2當中抽離邏輯代碼的技巧。
具體實現(xiàn)
舉例就拿最經(jīng)典的toodList來舉例
- .components/ToodList/ToodList.vue
<template> <div> <input type="text" placeholder="請輸入關鍵字" v-model="value" /><button @click="handleAddTood" > 操作按鈕 </button> <tood-list-item v-for="item in list" :key="item.id" :item="item" @handleDel="handleDel" ></tood-list-item> </div> </template> <script> import ToodListItem from "./ToodListItem"; export default { components: { ToodListItem, }, props: { list: { type: Array, default() { return []; }, }, }, data() { return { value: "", }; }, methods: { handleDel(item) { this.$emit("handelDel", item); }, handleAddTood() { this.$emit("addTood", this.value); }, }, }; </script>
- .components/ToodList/ToodListItem.vue
<template> <div> <input type="checkbox" v-model="item.complated" /> <span :style="{ textDecoration: item.complated ? 'line-through' : 'none' }" >{{ item.title }}</span > <button @click="handleDel(item)">刪除</button> </div> </template> <script> export default { props: { item: { type: Object, default() { return {}; }, }, }, methods: { handleDel(item) { this.$emit("handleDel", item); }, }, }; </script>
.components/ToodList/hook文件下:
- actionTypes.js
//主要定義時間觸發(fā)的類型常量 export const ACTION_TYPES = { del: "DEL", add: "ADD", };
.components/ToodList/hook文件下:
- actionTypes.js
//派發(fā)器,通過action找到對應的事件處理邏輯,然后觸發(fā) import { addTood, removeTood } from "./toodReducer"; import { ACTION_TYPES } from "./actionTypes"; //接收參數(shù),vue組件上下文對應 export default function (ctx) { function dispatch(args) { const { type, payLoad } = args; //根據(jù)行為,觸發(fā)reducer中的對應邏輯 switch (type) { case ACTION_TYPES.del: removeTood.bind(ctx)(payLoad); break; case ACTION_TYPES.add: addTood.bind(ctx)(payLoad); break; default: break; } } //返回綁定組件實例的函數(shù) return dispatch.bind(ctx); }
.components/ToodList/hook文件下:
- toodReducer.js
//主要事件處理邏輯文件(對應vue組件中的一個個method方法) //添加一個tood export function addTood(value) { const toodListItme = { title: value, complated: false, id: Date.now(), }; this.list.push(toodListItme); } //刪除一個tood export function removeTood(item) { const { id } = item; this.list = this.list.filter((item) => item.id != id); }
- App.vue文件
<template> <div id="app"> <tood-list :list="list" @handelDel="handelDel" @addTood="addTood" ></tood-list> </div> </template> <script> import ToodList from "@/components/ToodList/ToodList.vue"; import dispatch from "./components/ToodList/hook/dispatch"; import { ACTION_TYPES } from "./components/ToodList/hook/actionTypes"; export default { name: "App", components: { ToodList, }, data() { return { list: [ { title: "第一項", complated: false, id: 1, }, { title: "第二項", complated: false, id: 2, }, { title: "第三項", complated: true, id: 3, }, ], }; }, methods: { //邏輯代碼全部抽離到外部進行處理,當前組件只需要關注視圖即可 handelDel(item) { dispatch(this)({ type: ACTION_TYPES.del, payLoad: item, }); }, addTood(value) { dispatch(this)({ type: ACTION_TYPES.add, payLoad: value, }); }, }, }; </script>
總結
在vue2中在一個組件中寫過多代碼還是很常見的,不好分離邏輯的地方就在this,因此避免一個文件邏輯過多,可以試試這種方法,在vue3更新后,移除單文件組件對this的過渡依賴,對應的compositionApi已經(jīng)非常便于我們抽離單文件邏輯代碼了。
以上就是派發(fā)器抽離vue2單組件中的大量邏輯技巧的詳細內(nèi)容,更多關于派發(fā)器vue2單組件邏輯抽離的資料請關注腳本之家其它相關文章!
相關文章
Vant Uploader實現(xiàn)上傳一張或多張圖片組件
這篇文章主要為大家詳細介紹了Vant Uploader實現(xiàn)上傳一張或多張圖片組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Monaco-editor 的 JSON Schema 配置及使用介紹
這篇文章主要為大家介紹了Monaco-editor 的 JSON Schema 配置及使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10