🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] >[success] # 使用 Composition API 开发TodoList 下面用 **composition API** 实现了一个 **TodoList** 效果 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>使用 Composition API 开发TodoList</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ setup(){ const { ref, reactive } = Vue const inputValue = ref('') const list = reactive([]) const handleInputValueChange = (e) => { inputValue.value = e.target.value } const handleSubmit = () => { list.push(inputValue.value) } return { list, inputValue, handleSubmit, handleInputValueChange } }, template: ` <div> <input :value="inputValue" @input="handleInputValueChange"/> <button @click="handleSubmit">提交</button> <ul> <li v-for="(item, index) in list" :key="index">{{item}}</li> </ul> </div> `, }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## 封装TodoList 上面的写法虽然是完成了功能,但是 **代码零散**,其实我们可以做一个 **封装** ,把 **逻辑相同的代码进行归类封装** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>使用 Composition API 开发TodoList</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 关于 list 操作的内容进行了封装 const listRelativeEffect = () => { const { reactive } = Vue const list = reactive([]) const addItemToList = (item) => { list.push(item) } return { list, addItemToList } } // 关于 inputValue 操作的内容进行了封装 const inputRelativeEffect = () => { const { ref } = Vue const inputValue = ref('') const handleInputValueChange = (e) => { inputValue.value = e.target.value } return { inputValue, handleInputValueChange } } const app = Vue.createApp({ setup(){ // 流程调度中转 const { list, addItemToList } = listRelativeEffect() const { inputValue, handleInputValueChange } = inputRelativeEffect() return { list, addItemToList, inputValue, handleInputValueChange } }, template: ` <div> <input :value="inputValue" @input="handleInputValueChange"/> <button @click="() => addItemToList(inputValue)">提交</button> <ul> <li v-for="(item, index) in list" :key="index">{{item}}</li> </ul> </div> `, }) const vm = app.mount('#root') </script> </html> ~~~ 上面的代码中,我们把对 **list 操作的相关逻辑进行了封装** , **input 相关的逻辑也进行了封装** ,代码中的 **addItemToList** 用 **箭头函数处理逻辑** ,如果觉得不太好懂,也可以像下面这样封装一个函数 ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>使用 Composition API 开发TodoList</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 关于 list 操作的内容进行了封装 const listRelativeEffect = () => { const { reactive } = Vue const list = reactive([]) const addItemToList = (item) => { list.push(item) } return { list, addItemToList } } // 关于 inputValue 操作的内容进行了封装 const inputRelativeEffect = () => { const { ref } = Vue const inputValue = ref('') const handleInputValueChange = (e) => { inputValue.value = e.target.value } return { inputValue, handleInputValueChange } } const app = Vue.createApp({ setup(){ // 流程调度中转 const { list, addItemToList } = listRelativeEffect() const { inputValue, handleInputValueChange } = inputRelativeEffect() const handleSubmit = () => { addItemToList(inputValue.value) } return { list, addItemToList, inputValue, handleInputValueChange, handleSubmit } }, template: ` <div> <input :value="inputValue" @input="handleInputValueChange"/> <button @click="handleSubmit">提交</button> <ul> <li v-for="(item, index) in list" :key="index">{{item}}</li> </ul> </div> `, }) const vm = app.mount('#root') </script> </html> ~~~ 但是还是 **建议使用箭头函数的方式,没必要单独封装一个函数** 。