vue3中setup-script的應用實例
新特性的產(chǎn)生背景
在了解它怎么用之前,可以先了解一下它被推出的一些背景,可以幫助你對比開發(fā)體驗上的異同點,以及了解為什么會有這一章節(jié)里面的新東西。
在 Vue 3.0 的 .vue 組件里,遵循 SFC 規(guī)范要求(注:SFC,即 Single-File Component,.vue 單組件),標準的 setup 用法是,在 setup 里面定義的數(shù)據(jù)如果需要在 template 使用,都需要 return 出來。
如果你使用的是 TypeScript ,還需要借助 defineComponent 來幫助你對類型的自動推導。
<!-- 標準組件格式 --> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ setup () { // ... return { // ... } } }) </script>
關(guān)于標準 setup 和 defineComponent 的說明和用法,可以查閱 全新的 setup 函數(shù) 一節(jié)。
script-setup 的推出是為了讓熟悉 3.0 的用戶可以更高效率的開發(fā)組件,減少一些心智負擔,只需要給 script 標簽添加一個 setup 屬性,那么整個 script 就直接會變成 setup 函數(shù),所有頂級變量、函數(shù),均會自動暴露給模板使用(無需再一個個 return 了)。
上次寫了關(guān)于自己初次使用vue3的一些感受,同時也獲取了一眾大佬的教導,其中最重要的是方法的setup的語法糖,為了搞清楚這個語法糖,我自己有把之前的頁面,又重新重構(gòu)了一次。果然得到真香定律,使用以后發(fā)現(xiàn)原來vue3還可以像react那樣簡潔的處理方法和傳值,話不多說上代碼看下吧
內(nèi)部變量
首先看下內(nèi)部變量變化,這是單純之前使用setup
<template> <div> <div> 子組件內(nèi)String:{{infor}} </div> <div> 子組件內(nèi)obj:{{obj.data}} </div> <div> 子組件內(nèi)arry:{{arry[0]}} </div> <div @click="changeInfor"> 改變obj </div> </div> </template> <script> import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue"; export default { setup(){ const infor = ref('infor') const obj = reactive({ data:'obj' }) const arry = reactive([111,222,333]) const changeInfor = () =>{ obj.data = 'changeObj' } return { infor, obj, arry, changeInfor } } } </script> <style> div{ line-height: 40px; } </style>
這是改成語法糖之后的代碼
<template> <div> <div> 子組件內(nèi)String:{{infor}} </div> <div> 子組件內(nèi)obj:{{obj.data}} </div> <div> 子組件內(nèi)arry:{{arry[0]}} </div> <div @click="changeInfor"> 改變obj </div> </div> </template> <script setup> import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue"; const infor = ref('infor') const obj = reactive({ data:'obj' }) const arry = reactive([111,222,333]) const changeInfor = () =>{ infor.value = '32323' obj.data = 'changeObj' } </script> <style> div{ line-height: 40px; } </style>
這里可以明顯看出來,未使用setup-script的方式,跟傳統(tǒng)的還是有很大區(qū)別的, 結(jié)構(gòu)簡單明了,不需要把大量的變量和方法都寫在setup這個函數(shù)里面,自由度很高,有點像react的類里面的使用方法
子父級傳值
這里面主要涉及到三個方法 defineEmits,defineProps,defineExpose
// 父組件 <template> <div> 父組件 <children ref="child" @getData="sentData" :data="data"/> <div>{{childData || '我還沒收到值'}}</div> <div @click="makeClid">點擊調(diào)用子組件方法</div> </div> </template> <script setup> import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue"; import children from './components/children.vue' const childData = ref('') const data = ref('父組件給的值prop傳值') const sentData = (data) =>{ childData.value = data } const child = ref(null) // 獲取子組件ref const makeClid = () =>{ child.value.setData('子組件已調(diào)用') } </script> // 子組件 <template> <div> {{fatherData || '父組件還未調(diào)用我'}} <div @click="getData">觸發(fā)父組件</div> <div>fatherProps:{{fatherProps}}</div> </div> </template> <script setup> import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue"; const fatherData = ref('') const fatherProps = ref('') const props = defineProps({ // 父組件傳值 data:String }) fatherProps.value = props.data const emit = defineEmits(['getData']) // 接受父組件數(shù)據(jù) const getData = () =>{ emit('getData','給父組件的值') // 觸發(fā)父組件的方法 } const setData = (val) =>{ // 父組件調(diào)用 fatherData.value = val } defineExpose({ // 拋出方法 getData, setData }) </script>
- 子組件調(diào)用父組件:這點很好理解,跟vue2差不多的形式,父組件里面掛載@getData,子組件里面通過defineEmits這個方法獲取,最后調(diào)用方式跟之前也是一樣的
- 父組件調(diào)用子組件:這點區(qū)別還是很大的,需要子組件先定義好方法,然后通過defineExpose暴露出去,父組件創(chuàng)建ref,這里需要創(chuàng)建的變量名字和子組件的ref名字一直,否者獲取不到,最后通過獲取拋出的value找到對應的方法。
總結(jié)
到此這篇關(guān)于vue3中setup-script應用的文章就介紹到這了,更多相關(guān)vue3 setup-script應用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3 vite pinia配置動態(tài)路由、解決刷新頁面路由消失的問題
這篇文章主要介紹了vue3 vite pinia配置動態(tài)路由、解決刷新頁面路由消失的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10淺談Vue2.4.0 $attrs與inheritAttrs的具體使用
這篇文章主要介紹了淺談Vue2.4.0 $attrs與inheritAttrs的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取)
這篇文章主要介紹了vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Vuex中實現(xiàn)數(shù)據(jù)狀態(tài)查詢與更改
今天小編就為大家分享一篇Vuex中實現(xiàn)數(shù)據(jù)狀態(tài)查詢與更改,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue antd的from表單中驗證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗證rules中type的坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue 解決setTimeOut和setInterval函數(shù)無效報錯的問題
這篇文章主要介紹了vue 解決setTimeOut和setInterval函數(shù)無效報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue 實現(xiàn)點擊空白處隱藏某節(jié)點的三種方式(指令、普通、遮罩)
最近小編接到這樣的需求:彈出框(或Popover)在 show 后,點擊空白處可以將其 hide。針對這個需求,小編整理了三種實現(xiàn)方式,如果大家對vue 點擊空白隱藏節(jié)點問題感興趣的朋友跟隨小編一起看看吧2019-10-10