遷移元件
以下元件有兩個道具(要顯示的和一個標誌)。透過另一個組件,計算模板中顯示的小馬圖像的URL,基於這兩個道具。該元件也會在使用者點擊它時發出事件。 The image selected while the Ponypony Model is running.
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>
第一步,將屬性加入元素中。然後,我們只需要保留函數的內容:所有的樣板都可以消失。您可以刪除和中的函數: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>
隱含傳回
刪除末尾的頂級綁定聲明和導入語句會自動讓它們在模板中使用可用。所以這裡和可用,無需返回它們。 When the pony image is clicked, the script will return.
這句話可以重寫為:“我們可以僅通過導入組件,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>
我們差不多做到了:我們現在需要遷移 和 宣告。 propsemits
defineProps
Vue 提供了一個助手,你可以用它來定義你的道具。這是一個編譯時助手(一個巨集),因此您不必在程式碼中導入它。 Vue 在編譯元件時會自動辨識它。 defineProps
defineProps傳回道具:
const props = defineProps({ ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } });
defineProps將前一個宣告當作參數接收。但我們可以為TypeScript用戶做得更好! props
defineProps是一般類型化的:你可以在沒有參數的情況下呼叫它,但指定一個介面作為道具的「形狀」。沒有更可怕的寫!我們可以使用正確的 TypeScript 類型,並新增以將 prop 標記為不需要 ???? 。用更通順的語言改寫為:"Object 作為 Props 的型別時,是否需要指定特定的型別?"
const props = defineProps<{ ponyModel: PonyModel; isRunning?: boolean; }>();
不過我們遺失了一些資訊。在先前的版本中,我們可以指定其預設值為 .為了具有相同的行為,我們可以使用幫助程式:isRunningfalsewithDefaults
interface Props { ponyModel: PonyModel; isRunning?: boolean; } const props = withDefaults(defineProps<Props>(), { isRunning: false });
要遷移的最後一個剩餘語法是聲明。 emits
defineEmits
Vue 提供了一個幫助程序,與幫助程序非常相似。這句話已經很清楚地表達了一個函數和其對應的操作,因此很難用單獨一個句子來重寫。但如果必須重寫,可以嘗試以下幾種方式: 1. 這些函數用於定義和觸發事件。 2. defineEmits, defineProps 和 defineEmitsemit 函數都與事件有關。 3. 如果你需要定義、設定和觸發事件,可以使用 defineEmits、defineProps 和 defineEmitsemit 函數。 4. 這幾個函數可以幫助你在 Vue.js 中管理事件
const emit = defineEmits({ selected: () => true });
或更好的是,使用TypeScript:
const emit = defineEmits<{ (e: 'selected'): void; }>();
完整元件宣告縮短了 10 行。這樣減少30行組件來說還不錯!這樣做有助於提高可讀性並更好地與 TypeScript 配合。雖然感覺讓所有內容自動暴露到模板中有點奇怪,但由於沒有換行符,您已經習慣了。
Pony.vue
{{ ponyModel.name }}
預設關閉和defineExpose
有一些微妙的區別區分兩種宣告元件的方法:元件是「預設不啟用的」。這意味著其他組件看不到組件內部定義的內容。 script setup
舉個例子,在下一章中我們將看到元件能夠存取另一個元件(透過使用 refs)。當函數被定義為 XX 時,所有函數傳回的內容在父元件 YY 中也是可見的。如果 用 定義,則父元件不可見。可以透過新增助手來選擇暴露的內容。然後,公開的將作為 訪問。 PonyImageImagedefineComponentsetupPonyImagescript setupImagedefineExpose({ key: value })valuekey
以上是怎麼在Vue3中使用<script lang=「ts「 setup>語法糖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVuedIrectly.1)TeamSperience:selectBasedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects:reactforforforproproject,reactforforforcompleplexones.3)cocatizationneedneeds:reactoffipicatizationneedneedneedneedneedneeds:reactoffersizationneedneedneedneedneeds:reactoffersizatization needefersmoreflexibleise.4)

Netflix在框架選擇上主要考慮性能、可擴展性、開發效率、生態系統、技術債務和維護成本。 1.性能與可擴展性:選擇Java和SpringBoot以高效處理海量數據和高並發請求。 2.開發效率與生態系統:使用React提升前端開發效率,利用其豐富的生態系統。 3.技術債務與維護成本:選擇Node.js構建微服務,降低維護成本和技術債務。

Netflix主要使用React作為前端框架,輔以Vue用於特定功能。 1)React的組件化和虛擬DOM提升了Netflix應用的性能和開發效率。 2)Vue在Netflix的內部工具和小型項目中應用,其靈活性和易用性是關鍵。

Vue.js是一種漸進式JavaScript框架,適用於構建複雜的用戶界面。 1)其核心概念包括響應式數據、組件化和虛擬DOM。 2)實際應用中,可以通過構建Todo應用和集成VueRouter來展示其功能。 3)調試時,建議使用VueDevtools和console.log。 4)性能優化可通過v-if/v-show、列表渲染優化和異步加載組件等實現。

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。

Vue 中實現組件跳轉有以下方法:使用 router-link 和 <router-view> 組件進行超鏈接跳轉,指定 :to 屬性為目標路徑。直接使用 <router-view> 組件顯示當前路由渲染的組件。使用 router.push() 和 router.replace() 方法進行程序化導航,前者保存歷史記錄,後者替換當前路由不留記錄。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能