簡單聊聊vue3.0 sfc中setup的變化
前言
在vue中,sfc(單文件組件)指的是文件后綴名為.vue的特殊文件格式,它允許將 Vue 組件中的模板、邏輯 與 樣式封裝在單個文件中。
以下是一個基本的sfc
<script>
export default {
data() {
return {
greeting: 'Hello World!'
}
}
}
</script>
<template>
<p class="greeting">{{ greeting }}</p>
</template>
<style>
.greeting {
color: red;
font-weight: bold;
}
</style>
vue3.0在最新的sfc提案中推出了setup的寫法,下面讓我們來看看,新的提案都有哪些變化。
標(biāo)準(zhǔn)的sfc寫法
在使用TS的情況下,標(biāo)準(zhǔn)的sfc需要借助defineComponent來進(jìn)行類型推斷。
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
return {
// 暴露給template的屬性
}
}
})
</script>
script-setup
script setup的推出,目的是為了讓開發(fā)者更高效率的開發(fā)組件,減少樣板內(nèi)容,減輕開發(fā)負(fù)擔(dān)。僅僅需要給script標(biāo)簽添加一個setup屬性,就能將script變成setup函數(shù),同時定義的變量,函數(shù),導(dǎo)入的組件都會默認(rèn)暴露給模板。
變量暴露
標(biāo)準(zhǔn)的寫法
<script>
import { defineComponent, ref} from 'vue'
export default defineComponent({
setup() {
const count = ref(0)
return {
count
}
}
})
</script>
<template>
<div>
parent{{count}}
</div>
</template>
setup 寫法
<script setup lang="ts">
import {ref} from 'vue'
const count = ref(0)
</script>
<template>
<div>
parent{{count}}
</div>
</template>
組件掛載
標(biāo)準(zhǔn)的寫法
<script lang="ts">
import { defineComponent } from 'vue'
import child from './childComponent'
export default defineComponent({
components: {
child
},
setup() {
// ...
}
})
</script>
<template>
<div>
parent
<child />
</div>
</template>
setup 寫法
<script setup lang="ts"> import child from './childComponent' </script> <template> <div> parent <child /> </div> </template>
無需再手動掛載組件,即可在模板中使用,高效快捷。 其他的變量,以及頂級API,比如compute、watch等屬性,和原來的標(biāo)準(zhǔn)寫法一樣。
props
在setup中,子組件在接收props時,需要借助defineProps,這是一個只能在setup語法中才能使用的API。我們先來看看標(biāo)準(zhǔn)的寫法,props是如何接收的。
標(biāo)準(zhǔn)寫法
// parent.vue
<template>
<child :count={count} />
</template>
<script lang="ts">
impor {defineComponent,ref} from 'vue'
import child from './childComponent'
export default defineComponent({
components: {
child
},
setup() {
const count = ref(0)
return {
count
}
}
})
</script>
// child.vue
<template>
child{{count}}
</template>
<script lang="ts">
impor {defineComponent} from 'vue'
export default defineComponent({
props: ['count'],
setup(props) {
return {}
}
})
</script>
setup 寫法,使用defineProps
// parent.vue
<template>
<child :count={count} />
</template>
<script setup lang="ts">
impor {ref} from 'vue'
import child from './childComponent'
const count = ref<number>(0)
</script>
// child.vue
<template>
child{{count}}
</template>
<script setup>
defineProps(['count'])
</script>
❝
注意:使用sfc-setup語法,不需要引入defineProps
❞
在這里我們只需要簡單的聲明props,寫法簡潔了不少。
❝
那如何給props做類型檢查呢?
❞
<script setup>
defineProps({
count: Number,
title: {
type: String,
default: 'header'
},
data: {
type: Object,
defualt () {
return {}
}
}
})
</script>
❝
如何使用TS進(jìn)行類型注解呢?
❞
<script lang="ts" setup>
interface d {
name: string
}
defineProps<{
count: number // Number要換成ts的語法
title: string
data: d
}>()
</script>
❝
我們發(fā)現(xiàn),props沒有被賦予默認(rèn)值,在TS的寫法中,給props設(shè)置默認(rèn)值有2種方式
❞
ES6的變量解構(gòu)賦值
defineProps返回一個對象,我們可以在解構(gòu)返回的對象,同時賦予默認(rèn)值。
<script lang="ts" setup>
interface d {
name: string
}
const {count = 0, title = 'header', date = { name: 'a' }} = defineProps<{
count: number // Number要換成ts的語法
title: string
data: d
}>()
</script>
withDefaults
❝
官方后續(xù)推出了withDefaults來給props提供默認(rèn)值;withDefaults會對默認(rèn)值進(jìn)行類型檢查。
❞
<script lang="ts">
// 別忘記了引入 withDefaults
impor { withDefaults } from 'vue'
interface d {
name: string
}
const props = withDefaults(defineProps<{
count: number // Number要換成ts的語法
title: string
data: d
}>(), {
count: 0,
title: 'header',
data: () => ({name: '王小二'})
})
</script>
自定義事件
要在setup中,使用事件,需要借助defineEmits,這也是是一個僅能在sfc-setup語法中使用的編譯器宏。
<script setup lang="ts">
// 定義事件,同時做類型注解
// 非TS寫法:const emits = defineEmits(['create'])
const emits = defineEmits<{
(e: 'create', value: string): void
}>()
// 觸發(fā)事件
const addTodo = () => {
emits('create', 'hi')
}
</script>
補(bǔ)充一個用Vue3 + ts 重構(gòu)的官方TodoMVC例子:codesandbox.io/s/vibrant-w…
總結(jié)
到此這篇關(guān)于vue3.0 sfc中setup變化的文章就介紹到這了,更多相關(guān)vue3.0 sfc中setup變化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用threeJs導(dǎo)入obj模型并實現(xiàn)添加標(biāo)注
這篇文章主要介紹了vue使用threeJs導(dǎo)入obj模型并實現(xiàn)添加標(biāo)注方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
vue中this.$router.push()路由傳值和獲取的兩種常見方法匯總
這篇文章主要介紹了vue中this.$router.push()路由傳值和獲取的兩種常見方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
vueJs函數(shù)toRaw?markRaw使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)toRaw?markRaw使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Vue3+Vite實現(xiàn)動態(tài)路由的詳細(xì)實例代碼
我們在開發(fā)大型系統(tǒng)的時候一般都需要動態(tài)添加路由,下面這篇文章主要給大家介紹了關(guān)于Vue3+Vite實現(xiàn)動態(tài)路由的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
在Vue中實現(xiàn)圖表數(shù)據(jù)的動態(tài)展示的示例代碼
隨著數(shù)據(jù)可視化技術(shù)的發(fā)展,圖表在前端開發(fā)中扮演著越來越重要的角色,Vue.js 作為一個流行的前端框架,以其靈活性和易用性,成為了實現(xiàn)圖表動態(tài)展示的理想選擇,在這篇博客中,我們將學(xué)習(xí)如何在 Vue 3 中實現(xiàn)動態(tài)展示圖表數(shù)據(jù),需要的朋友可以參考下2024-11-11
Vue使用mounted和created時,this無法指向data中的數(shù)據(jù)問題
這篇文章主要介紹了Vue使用mounted和created時,this無法指向data中的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
vue + element動態(tài)多表頭與動態(tài)插槽
這篇文章主要介紹了vue + element動態(tài)多表頭與動態(tài)插槽,下面文章圍繞vue + element動態(tài)多表頭與動態(tài)插槽的相關(guān)資料展開文章的內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對大家有所幫助2021-12-12

