詳解如何在Vue3使用<script lang=“ts“ setup>語法糖
Vue 3.2 引入了語法,這是一種稍微不那么冗長的聲明組件的方式。您可以通過向 SFC 的元素添加屬性來啟用它,然后可以刪除組件中的一些樣板。script setupsetupscript
讓我們舉一個實際的例子,并將其遷移到這個語法!
遷移組件
以下組件有兩個道具(要顯示的和一個標志)?;谶@兩個道具,計算模板中顯示的小馬圖像的URL(通過另一個組件)。該組件還會在用戶單擊它時發(fā)出一個事件。PonyponyModelisRunningImageselected
Pony.vue
<template> <figure @click="clicked()"> <Image :src="ponyImageUrl" :alt="ponyModel.name" /> <figcaption>{{ ponyModel.name }}</figcaption> </figure> </template> <script lang="ts"> import { computed, defineComponent, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; export default defineComponent({ components: { Image }, props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, setup(props, { emit }) { const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } return { ponyImageUrl, clicked }; } }); </script>
第一步,將屬性添加到元素中。然后,我們只需要保留函數(shù)的內(nèi)容:所有的樣板都可以消失。您可以刪除 和 中的函數(shù):setupscriptsetupdefineComponentsetupscript
Pony.vue
<script setup lang="ts"> import { computed, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; components: { Image }, props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } return { ponyImageUrl, clicked }; </script>
隱式返回
我們還可以刪除末尾的:在模板中聲明的所有頂級綁定(以及所有導(dǎo)入)都自動可用。所以這里和可用,無需返回它們。returnscript setupponyImageUrlclicked
聲明也是如此!導(dǎo)入組件就足夠了,Vue 知道它在模板中使用:我們可以刪除聲明。componentsImagecomponents
Pony.vue
<script setup lang="ts"> import { computed, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } </script>
我們差不多做到了:我們現(xiàn)在需要遷移 和 聲明。propsemits
defineProps
Vue 提供了一個助手,你可以用它來定義你的道具。它是一個編譯時幫助程序(一個宏),所以你不需要在代碼中導(dǎo)入它:Vue 在編譯組件時會自動理解它。defineProps
defineProps返回道具:
const props = defineProps({ ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } });
defineProps將前一個聲明作為參數(shù)接收。但是我們可以為TypeScript用戶做得更好!props
defineProps是一般類型化的:你可以在沒有參數(shù)的情況下調(diào)用它,但指定一個接口作為道具的“形狀”。沒有更可怕的寫!我們可以使用正確的 TypeScript 類型,并添加以將 prop 標記為不需要 ?? 。Object as PropType<Something>?
const props = defineProps<{ ponyModel: PonyModel; isRunning?: boolean; }>();
不過我們丟失了一些信息。在以前的版本中,我們可以指定其默認值為 .為了具有相同的行為,我們可以使用幫助程序:isRunningfalsewithDefaults
interface Props { ponyModel: PonyModel; isRunning?: boolean; } const props = withDefaults(defineProps<Props>(), { isRunning: false });
要遷移的最后一個剩余語法是聲明。emits
defineEmits
Vue 提供了一個幫助程序,與幫助程序非常相似。 返回函數(shù):defineEmitsdefinePropsdefineEmitsemit
const emit = defineEmits({ selected: () => true });
或者更好的是,使用TypeScript:
const emit = defineEmits<{ (e: 'selected'): void; }>();
完整組件聲明縮短了 10 行。對于~30行組件來說,這不是一個糟糕的減少!它更容易閱讀,并且與TypeScript配合得更好。讓所有內(nèi)容自動暴露到模板中確實感覺有點奇怪,但是沒有寫回車符,但是您已經(jīng)習(xí)慣了。
Pony.vue
<template> <figure @click="clicked()"> <Image :src="ponyImageUrl" :alt="ponyModel.name" /> <figcaption>{{ ponyModel.name }}</figcaption> </figure> </template> <script setup lang="ts"> import { computed } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; interface Props { ponyModel: PonyModel; isRunning?: boolean; } const props = withDefaults(defineProps<Props>(), { isRunning: false }); const emit = defineEmits<{ (e: 'selected'): void; }>(); const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } </script>
默認關(guān)閉和defineExpose
聲明組件的兩種方法之間有一個更細微的區(qū)別:組件是“默認關(guān)閉的”。這意味著其他組件看不到組件內(nèi)部定義的內(nèi)容。script setup
例如,組件可以訪問該組件(通過使用 refs,我們將在下一章中看到)。如果定義為 ,則函數(shù)返回的所有內(nèi)容對于父組件 () 也是可見的。如果 用 定義,則父組件不可見。 可以通過添加助手來選擇暴露的內(nèi)容。然后,公開的將作為 訪問。PonyImageImagedefineComponentsetupPonyImagescript setupImagedefineExpose({ key: value })valuekey
現(xiàn)在,此語法是聲明組件的推薦方法,使用起來非常棒!
到此這篇關(guān)于詳解如何在Vue3使用<script lang=“ts“ setup>語法糖的文章就介紹到這了,更多相關(guān)Vue3使用<script lang=“ts“ setup>內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue/react項目刷新頁面出現(xiàn)404報錯的原因及解決辦法
Vue項目打包部署到線上后,刷新頁面會提示404,下面這篇文章主要給大家介紹了關(guān)于vue/react項目刷新頁面出現(xiàn)404報錯的原因及解決辦法,文中將解決的辦法介紹的很詳細,需要的朋友可以參考下2023-05-05Vue腳手架學(xué)習(xí)之項目創(chuàng)建方式
這篇文章主要給大家介紹了關(guān)于Vue腳手架學(xué)習(xí)之項目創(chuàng)建方式的相關(guān)資料,文中通過介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Nuxt3項目中問題匯總之刷新頁面useFetch無返回解決
Nuxt.js是一個基于 Vue.js 的服務(wù)端渲染應(yīng)用框架,這篇文章主要給大家介紹了關(guān)于Nuxt3項目中問題匯總之刷新頁面useFetch無返回解決辦法的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-03-03vue+element-ui+axios實現(xiàn)圖片上傳
這篇文章主要為大家詳細介紹了vue+element-ui+axios實現(xiàn)圖片上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08