Vue?項(xiàng)目中選擇?TSX?而非傳統(tǒng)?.vue?文件的原因分析
近年來(lái),Vue 項(xiàng)目中使用 TSX(TypeScript JSX)的寫(xiě)法逐漸增多,尤其在 TypeScript 項(xiàng)目中。
1. TSX 與 Vue 的結(jié)合背景
1、Vue 3 和 TypeScript
Vue 3 從設(shè)計(jì)之初便更好地支持 TypeScript。Vue 3 使用了 TypeScript 重寫(xiě)核心,增強(qiáng)了類型推斷和類型安全的支持,使得 TypeScript 更適合與 Vue 3 配合使用。
2、組合式 API
Vue 3 推出組合式 API,使代碼邏輯更加模塊化,也更接近于函數(shù)式編程的風(fēng)格,這讓代碼結(jié)構(gòu)更貼近 TSX 的寫(xiě)法,方便在 TypeScript 和 JSX 中組織邏輯。
2. 簡(jiǎn)單的示例 ??
下面結(jié)合幾個(gè)具體的 ?? 詳細(xì)說(shuō)明在 Vue 中使用 TSX 和傳統(tǒng) .vue 文件之間的差異。
2.1 基礎(chǔ)組件
假設(shè)需要?jiǎng)?chuàng)建一個(gè)簡(jiǎn)單的用戶卡片組件,用于顯示用戶的姓名和年齡。
1、使用 .vue 文件編寫(xiě)
.vue 文件的模板語(yǔ)法非常適合這種簡(jiǎn)單的展示性組件,因?yàn)榻Y(jié)構(gòu)清晰,模板代碼直觀。
<!-- UserCard.vue --> <template> <div class="user-card"> <p>Name: {{ name }}</p> <p>Age: {{ age }}</p> </div> </template> <script setup lang="ts"> import { defineProps } from 'vue'; const props = defineProps<{ name: string; age: number; }>(); </script>
2、使用 tsx 編寫(xiě)
在 tsx 中寫(xiě)同樣的組件,代碼結(jié)構(gòu)會(huì)略有不同,因?yàn)槟0搴瓦壿嬍墙y(tǒng)一在一起的:
// UserCard.tsx import { defineComponent } from 'vue'; export default defineComponent({ name: 'UserCard', props: { name: String, age: Number, }, setup(props) { return () => ( <div class="user-card"> <p>Name: {props.name}</p> <p>Age: {props.age}</p> </div> ); }, });
2.2 復(fù)雜組件:帶插槽和事件處理
插槽和事件處理是 Vue 中常見(jiàn)的功能,在 .vue 文件和 .tsx 文件中實(shí)現(xiàn)會(huì)略有不同。
1、使用 .vue 文件編寫(xiě)
<!-- Modal.vue --> <template> <div v-if="visible" class="modal"> <div class="modal-content"> <slot></slot> <button @click="close">Close</button> </div> </div> </template> <script setup lang="ts"> import { defineProps, defineEmits } from 'vue'; const props = defineProps<{ visible: boolean }>(); const emit = defineEmits<{ (e: 'close'): void }>(); function close() { emit('close'); } </script> <style scoped> .modal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background: white; padding: 1rem; margin: auto; } </style>
2、使用 .tsx 編寫(xiě)
tsx 中通過(guò)直接傳遞插槽內(nèi)容來(lái)實(shí)現(xiàn)類似的功能。
// Modal.tsx import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'Modal', props: { visible: Boolean, }, emits: ['close'], setup(props, { emit, slots }) { const close = () => emit('close'); return () => ( props.visible && ( <div class="modal" style={{ position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, background: 'rgba(0, 0, 0, 0.5)', display: 'flex', justifyContent: 'center', alignItems: 'center' }}> <div class="modal-content" style={{ background: 'white', padding: '1rem' }}> {slots.default && slots.default()} <button onClick={close}>Close</button> </div> </div> ) ); }, });
展示為:
2.3 使用組件
1、使用 .vue 文件
<template> <div> <UserCard name="Alice" :age="25" /> <Modal :visible="isModalVisible" @close="toggleModal"> <p>This is the modal content!</p> </Modal> <button @click="toggleModal">Toggle Modal</button> </div> </template> <script setup> import UserCard from './components/UserCard.vue'; import Modal from './components/Modal.vue'; import { ref } from 'vue'; const isModalVisible = ref(false); const toggleModal = () => (isModalVisible.value = !isModalVisible.value); </script>
2、使用 .tsx 文件
import { defineComponent, ref } from 'vue'; import UserCard from './components/UserCard'; import Modal from './components/Modal'; export default defineComponent({ name: 'App', setup() { const isModalVisible = ref(false); const toggleModal = () => (isModalVisible.value = !isModalVisible.value); return () => ( <div> <UserCard name="Alice" age={25} /> <Modal visible={isModalVisible.value} onClose={toggleModal}> <p>This is the modal content!</p> </Modal> <button onClick={toggleModal}>Toggle Modal</button> </div> ); }, });
3、TSX 寫(xiě)法的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1、類型支持與代碼提示:TSX 能夠利用 TypeScript 的類型推斷功能,減少開(kāi)發(fā)中的類型錯(cuò)誤。
2、靈活性與復(fù)用性:TSX 的寫(xiě)法更貼近 JavaScript 和函數(shù)式編程的范式,因此能更靈活地編寫(xiě)高階組件和傳遞 props。許多復(fù)雜邏輯可以通過(guò)更純粹的函數(shù)式寫(xiě)法實(shí)現(xiàn)。
3、易于集成第三方庫(kù):TSX 更符合主流 JavaScript 庫(kù)(例如 React)的寫(xiě)法。
4、更好的代碼組織:對(duì)于團(tuán)隊(duì)開(kāi)發(fā)中的大型項(xiàng)目,TSX 組件更容易分離邏輯和樣式管理,提升代碼模塊化水平。
缺點(diǎn):
1、學(xué)習(xí)成本增加:TSX 更貼近 JavaScript 函數(shù)式寫(xiě)法,Vue 開(kāi)發(fā)者需要理解 JSX 語(yǔ)法。
2、可讀性降低:TSX 將模板、邏輯和樣式混合在一個(gè)文件中,盡管提升了靈活性,但可讀性不如傳統(tǒng)的 .vue 文件結(jié)構(gòu)。
4. 趨勢(shì)與發(fā)展方向
對(duì)于復(fù)雜的企業(yè)級(jí)應(yīng)用,TSX 的靈活性、類型安全性更符合需求。雖然 Vue 社區(qū)仍然以 .vue 文件為主流,但對(duì)于某些 TS 和 JS 深度開(kāi)發(fā)者來(lái)說(shuō),TSX 正成為常見(jiàn)選擇。Vue 團(tuán)隊(duì)未來(lái)可能會(huì)繼續(xù)增強(qiáng)對(duì) TSX 的支持,以適應(yīng)不同的編程習(xí)慣。
到此這篇關(guān)于Vue 項(xiàng)目中選擇 TSX 而非傳統(tǒng) .vue 文件的原因分析的文章就介紹到這了,更多相關(guān)vue內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue學(xué)習(xí)教程之帶你一步步詳細(xì)解析vue-cli
這篇文章的主題是vue-cli的理解?;蛟S,很多人在開(kāi)發(fā)vue的時(shí)候,我們會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題——只會(huì)去用,而不明白它的里面的東西?,F(xiàn)在的框架可以說(shuō)是足夠的優(yōu)秀,讓開(kāi)發(fā)者不用為搭建開(kāi)發(fā)環(huán)境而煩惱。但是有時(shí)候,我們還是得回到原始生活體驗(yàn)一下,才能夠讓自己更上層樓。2017-12-12vue 3 中watch 和watchEffect 的新用法
本篇文章主要通過(guò) Options API 和 Composition API 對(duì)比 watch 的使用方法,讓大家快速掌握 vue3 中 watch 新用法,需要的朋友可以參考一下哦,希望對(duì)大家有所幫助2021-11-11Vite性能優(yōu)化之分包策略的實(shí)現(xiàn)
本文主要介紹了Vite性能優(yōu)化之分包策略的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vue或react項(xiàng)目生產(chǎn)環(huán)境去掉console.log的操作
這篇文章主要介紹了vue或react項(xiàng)目生產(chǎn)環(huán)境去掉console.log的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09