Vue3.2中setup語(yǔ)法糖的使用教程分享
提示:vue3.2 版本開始才能使用語(yǔ)法糖!
在 Vue3.0 中變量必須 return 出來(lái), template 中才能使用;而在 Vue3.2 中只需要在 script 標(biāo)簽上加上 setup 屬性,無(wú)需 return , template 便可直接使用,非常的香啊!
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
1、如何使用setup語(yǔ)法糖
只需在 script 標(biāo)簽上寫上 setup 代碼如下(示例):
<template> </template> <script setup> </script> <style scoped lang="less"> </style>
2、data數(shù)據(jù)的使用
由于 setup 不需寫 return ,所以直接聲明數(shù)據(jù)即可 代碼如下(示例):
<script setup>
import {
ref,
reactive,
toRefs,
} from 'vue'
const data = reactive({
patternVisible: false,
debugVisible: false,
aboutExeVisible: false,
})
const content = ref('content')
//使用toRefs解構(gòu)
const { patternVisible, debugVisible, aboutExeVisible } = toRefs(data)
</script>3、method方法的使用
代碼如下(示例):
<template >
<button @click="onClickHelp">系統(tǒng)幫助</button>
</template>
<script setup>
import {reactive} from 'vue'
const data = reactive({
aboutExeVisible: false,
})
// 點(diǎn)擊幫助
const onClickHelp = () => {
console.log(`系統(tǒng)幫助`)
data.aboutExeVisible = true
}
</script>4、watchEffect的使用
代碼如下(示例):
<script setup>
import {
ref,
watchEffect,
} from 'vue'
let sum = ref(0)
watchEffect(()=>{
const x1 = sum.value
console.log('watchEffect所指定的回調(diào)執(zhí)行了')
})
</script>5、watch的使用
代碼如下(示例):
<script setup>
import {
reactive,
watch,
} from 'vue'
//數(shù)據(jù)
let sum = ref(0)
let msg = ref('你好啊')
let person = reactive({
name:'張三',
age:18,
job:{
j1:{
salary:20
}
}
})
// 兩種監(jiān)聽(tīng)格式
watch([sum,msg],(newValue,oldValue)=>{
console.log('sum或msg變了',newValue,oldValue)
},{immediate:true})
watch(()=>person.job,(newValue,oldValue)=>{
console.log('person的job變化了',newValue,oldValue)
},{deep:true})
</script>6、computed計(jì)算屬性的使用
computed 計(jì)算屬性有兩種寫法(簡(jiǎn)寫和考慮讀寫的完整寫法) 代碼如下(示例):
<script setup>
import {
reactive,
computed,
} from 'vue'
//數(shù)據(jù)
let person = reactive({
firstName:'小',
lastName:'叮當(dāng)'
})
// 計(jì)算屬性簡(jiǎn)寫
person.fullName = computed(()=>{
return person.firstName + '-' + person.lastName
})
// 完整寫法
person.fullName = computed({
get(){
return person.firstName + '-' + person.lastName
},
set(value){
const nameArr = value.split('-')
person.firstName = nameArr[0]
person.lastName = nameArr[1]
}
})
</script>7 、props父子傳值的使用
子組件代碼如下(示例):
<template>
<span>{{props.name}}</span>
</template>
<script setup>
import { defineProps } from 'vue'
// 聲明props
const props = defineProps({
name: {
type: String,
default: '11'
}
})
// 或者
//const props = defineProps(['name'])
</script>父組件代碼如下(示例):
<template>
<child :name='name'/>
</template>
<script setup>
import {ref} from 'vue'
// 引入子組件
import child from './child.vue'
let name= ref('小叮當(dāng)')
</script>8 、emit子父?jìng)髦档氖褂?/h2>
子組件代碼如下(示例):
<template>
<a-button @click="isOk">
確定
</a-button>
</template>
<script setup>
import { defineEmits } from 'vue';
// emit
const emit = defineEmits(['aboutExeVisible'])
/**
* 方法
*/
// 點(diǎn)擊確定按鈕
const isOk = () => {
emit('aboutExeVisible');
}
</script>父組件代碼如下(示例):
<template>
<AdoutExe @aboutExeVisible="aboutExeHandleCancel" />
</template>
<script setup>
import {reactive} from 'vue'
// 導(dǎo)入子組件
import AdoutExe from '../components/AdoutExeCom'
const data = reactive({
aboutExeVisible: false,
})
// content組件ref
// 關(guān)于系統(tǒng)隱藏
const aboutExeHandleCancel = () => {
data.aboutExeVisible = false
}
</script>9、獲取子組件ref變量和defineExpose暴露
即 vue2 中的獲取子組件的 ref ,直接在父組件中控制子組件方法和變量的方法
子組件代碼如下(示例):
<template>
<p>{{data }}</p>
</template>
<script setup>
import {
reactive,
toRefs
} from 'vue'
/**
* 數(shù)據(jù)部分
* */
const data = reactive({
modelVisible: false,
historyVisible: false,
reportVisible: false,
})
defineExpose({
...toRefs(data),
})
</script>父組件代碼如下(示例):
<template>
<button @click="onClickSetUp">點(diǎn)擊</button>
<Content ref="content" />
</template>
<script setup>
import {ref} from 'vue'
// content組件ref
const content = ref('content')
// 點(diǎn)擊設(shè)置
const onClickSetUp = ({ key }) => {
content.value.modelVisible = true
}
</script>
<style scoped lang="less">
</style>10、路由useRoute和us eRouter的使用
代碼如下(示例):
<script setup>
import { useRoute, useRouter } from 'vue-router'
// 聲明
const route = useRoute()
const router = useRouter()
// 獲取query
console.log(route.query)
// 獲取params
console.log(route.params)
// 路由跳轉(zhuǎn)
router.push({
path: `/index`
})
</script>11、store倉(cāng)庫(kù)的使用
代碼如下(示例):
<script setup>
import { useStore } from 'vuex'
import { num } from '../store/index'
const store = useStore(num)
// 獲取Vuex的state
console.log(store.state.number)
// 獲取Vuex的getters
console.log(store.state.getNumber)
// 提交mutations
store.commit('fnName')
// 分發(fā)actions的方法
store.dispatch('fnName')
</script>12、await的支持
setup 語(yǔ)法糖中可直接使用 await ,不需要寫 async , setup 會(huì)自動(dòng)變成 async setup
代碼如下(示例):
<script setup> import api from '../api/Api' const data = await Api.getData() console.log(data) </script>
13、provide 和 inject 祖孫傳值
父組件代碼如下(示例)
<template>
<AdoutExe />
</template>
<script setup>
import { ref,provide } from 'vue'
import AdoutExe from '@/components/AdoutExeCom'
let name = ref('Jerry')
// 使用provide
provide('provideState', {
name,
changeName: () => {
name.value = '小叮當(dāng)'
}
})
</script>
子組件代碼如下(示例):
<script setup>
import { inject } from 'vue'
const provideState = inject('provideState')
provideState.changeName()
</script>以上就是Vue3.2中setup語(yǔ)法糖的使用教程分享的詳細(xì)內(nèi)容,更多關(guān)于Vue setup語(yǔ)法糖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- vue3中setup語(yǔ)法糖下父子組件間傳遞數(shù)據(jù)的方式
- vue3中<script?setup>?和?setup函數(shù)的區(qū)別對(duì)比
- Vue3?setup的注意點(diǎn)及watch監(jiān)視屬性的六種情況分析
- vue3在setup中使用mapState解讀
- Vue3中關(guān)于setup與自定義指令詳解
- Vue3中的setup語(yǔ)法糖、computed函數(shù)、watch函數(shù)詳解
- Vue3?setup?的作用實(shí)例詳解
- Vue3?setup添加name的方法步驟
- Vue3的setup在el-tab中動(dòng)態(tài)加載組件的方法
- vue3.0?setup中使用vue-router問(wèn)題
- vue3中setup語(yǔ)法糖下通用的分頁(yè)插件實(shí)例詳解
- vue3?setup語(yǔ)法糖各種語(yǔ)法新特性的使用方法(vue3+vite+pinia)
- vue3 setup的使用和原理實(shí)例詳解
相關(guān)文章
vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用)
這篇文章主要介紹了vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Vue自定義全局Toast和Loading的實(shí)例詳解
這篇文章主要介紹了Vue自定義全局Toast和Loading,需要的朋友可以參考下2019-04-04
vue分頁(yè)組件table-pagebar使用實(shí)例解析
這篇文章主要為大家詳細(xì)解析了vue分頁(yè)組件table-pagebar使用實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑)
這篇文章主要介紹了vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue升級(jí)帶來(lái)的elementui沖突警告:Invalid prop: custom va
在頁(yè)面渲染的時(shí)候,控制臺(tái)彈出大量警告,嚴(yán)重影響控制臺(tái)的信息獲取功能,但是頁(yè)面基本能正常顯示,這是因?yàn)閂ue升級(jí)帶來(lái)的elementui沖突警告: Invalid prop: custom validator check failed for prop “type“.的解決方案,本文給大家介紹了詳細(xì)的解決方案2025-04-04
一文詳解Vue中內(nèi)存泄漏的場(chǎng)景與預(yù)防技巧
即便是功能強(qiáng)大的?Vue.js?也無(wú)法完全避免內(nèi)存泄漏的問(wèn)題,內(nèi)存泄漏不僅會(huì)影響應(yīng)用的性能,還可能導(dǎo)致瀏覽器崩潰,下面我們來(lái)看看Vue?中常見(jiàn)的內(nèi)存泄漏場(chǎng)景以及如何避免這些問(wèn)題吧2024-12-12
解決微信瀏覽器緩存站點(diǎn)入口文件(IIS部署Vue項(xiàng)目)
這篇文章主要介紹了解決微信瀏覽器緩存站點(diǎn)入口文件(IIS部署Vue項(xiàng)目),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

