Home >Web Front-end >Vue.js >How to use setup() and reactive() functions in vue3
In vue2
1. Advantages: Easy to learn and use, the location for writing code has been agreed upon.
2. Disadvantages: For large projects, it is not conducive to code reuse, management and maintenance.
3. Explanation: The data and business logic of the same function are scattered in N places in the same file. As the business complexity increases, we need to often use data similar to data() And back-and-forth processing in methods
##In vue3
return To return, you can use directly in the template (generally, setup cannot be an asynchronous function).
<template> <h2 @click="say()">{{ msg }}</h2> </template> <script> export default { setup() { const msg = 'Hello Vue3' const say = () => { console.log(msg) } return { msg, say } }, } </script>Effect view:
Note: Similar to data() and methods in vue2, they need to be written in return before they can be called as results. [Small interview questions supplement] Must the return in setup be only one object? (setup can also return a rendering function) App.vue
<script> import { h } from 'vue' export default { name: 'App', setup() { return () => h('h3', 'Hello Vue3') }, } </script>The console prints Hello Vue3 with the h3 tag.
For example, when I have a requirement: click to delete the current row informationView through vueTools, the data is deleted after I click it. But there is no actual rendering on the pageApp.vue
<template> <ul> <li v-for="(item, index) in arr" :key="item" @click="removeItem(index)">{{ item }}</li> </ul> </template> <script> export default { name: 'App', setup() { const arr = ['a', 'b', 'c'] const removeItem = (index) => { arr.splice(index, 1) } return { arr, removeItem, } }, } </script>
At this time, use reactive() to wrap the array to make it responsive data. Don’t forget to import
<template> <ul> <li v-for="(item, index) in arr" :key="item" @click="removeItem(index)">{{ item }}</li> </ul> </template> <script> import { reactive } from 'vue' export default { name: 'App', setup() { const arr = reactive(['a', 'b', 'c']) const removeItem = (index) => { arr.splice(index, 1) } return { arr, removeItem, } }, } </script>
Now the page is responsive, deleted when clicked, and the page is responsive
<template> <form @submit.prevent="handleSubmit"> <input type="text" v-model="user.id" /> <input type="text" v-model="user.name" /> <input type="submit" /> </form> <ul> <li v-for="(item, index) in state.arr" :key="item.id" @click="removeItem(index)">{{ item.name }}</li> </ul> </template> <script> import { reactive } from 'vue' export default { name: 'App', setup() { const state = reactive({ arr: [ { id: 0, name: 'ifer', }, { id: 1, name: 'elser', }, { id: 2, name: 'xxx', }, ], }) const removeItem = (index) => { // 默认是递归监听的,对象里面任何一个数据的变化都是响应式的 state.arr.splice(index, 1) } const user = reactive({ id: '', name: '', }) const handleSubmit = () => { state.arr.push({ id: user.id, name: user.name, }) user.id = '' user.name = '' } return { state, removeItem, user, handleSubmit, } }, } </script>
Interpretation of the above code:
Do you have a clearer understanding of the use of setup() now? Let’s simplify our writing method below.
<template> <form @submit.prevent="handleSubmit"> <input type="text" v-model="user.id" /> <input type="text" v-model="user.name" /> <input type="submit" /> </form> <ul> <li v-for="(item, index) in state.arr" :key="item.id" @click="removeItem(index)">{{ item.name }}</li> </ul> </template> <script> import { reactive } from 'vue' function useRemoveItem() { const state = reactive({ arr: [ { id: 0, name: 'ifer', }, { id: 1, name: 'elser', }, { id: 2, name: 'xxx', }, ], }) const removeItem = (index) => { state.arr.splice(index, 1) } return { state, removeItem } } function useAddItem(state) { const user = reactive({ id: '', name: '', }) const handleSubmit = () => { state.arr.push({ id: user.id, name: user.name, }) user.id = '' user.name = '' } return { user, handleSubmit, } } export default { name: 'App', setup() { const { state, removeItem } = useRemoveItem() const { user, handleSubmit } = useAddItem(state) return { state, removeItem, user, handleSubmit, } }, } </script>Extract the method in a way similar to importing,
Put the data and methods together to facilitate our unified management.
App.vue
<template> <form > <input type="text" v-model="user.id" /> <input type="text" v-model="user.name" /> <button type="submit" @click.prevent="submit">提交</button> </form> <ul> <li v-for="(item, index) in state.arr" :key="item.id" @click="removeItem(index)">{{ item.name }}</li> </ul> </template> <script> import {useRemoveItem,handleSubmit} from './hooks' export default { name: 'App', setup() { const { state, removeItem } = useRemoveItem() const { user, submit } = handleSubmit(state) return { state,removeItem,user,submit } }, } </script>
hooks/index.js
import { reactive } from 'vue' export const useRemoveItem=()=> { const state= reactive( { arr: [ { id: 0, name: 'ifer', }, { id: 1, name: 'elser', }, { id: 2, name: 'xxx', }, ] }) const removeItem=(index)=>{ state.arr.splice(index,1) console.log(state.arr); } return { state, removeItem } } export const handleSubmit=(state)=>{ const user = reactive({ id: '', name: '', }) console.log(1); const submit = () => { state.arr.push({ ...user }) user.id = '' user.name = '' } return { user, submit } }
The above is the detailed content of How to use setup() and reactive() functions in vue3. For more information, please follow other related articles on the PHP Chinese website!