Vue?項(xiàng)目中選擇?TSX?而非傳統(tǒng)?.vue?文件的原因分析
近年來,Vue 項(xiàng)目中使用 TSX(TypeScript JSX)的寫法逐漸增多,尤其在 TypeScript 項(xiàng)目中。
1. TSX 與 Vue 的結(jié)合背景
1、Vue 3 和 TypeScript
Vue 3 從設(shè)計(jì)之初便更好地支持 TypeScript。Vue 3 使用了 TypeScript 重寫核心,增強(qiáng)了類型推斷和類型安全的支持,使得 TypeScript 更適合與 Vue 3 配合使用。
2、組合式 API
Vue 3 推出組合式 API,使代碼邏輯更加模塊化,也更接近于函數(shù)式編程的風(fēng)格,這讓代碼結(jié)構(gòu)更貼近 TSX 的寫法,方便在 TypeScript 和 JSX 中組織邏輯。
2. 簡(jiǎn)單的示例 ??
下面結(jié)合幾個(gè)具體的 ?? 詳細(xì)說明在 Vue 中使用 TSX 和傳統(tǒng) .vue 文件之間的差異。
2.1 基礎(chǔ)組件
假設(shè)需要?jiǎng)?chuàng)建一個(gè)簡(jiǎn)單的用戶卡片組件,用于顯示用戶的姓名和年齡。
1、使用 .vue 文件編寫
.vue 文件的模板語法非常適合這種簡(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 編寫
在 tsx 中寫同樣的組件,代碼結(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 中常見的功能,在 .vue 文件和 .tsx 文件中實(shí)現(xiàn)會(huì)略有不同。
1、使用 .vue 文件編寫
<!-- 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 編寫
tsx 中通過直接傳遞插槽內(nè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 寫法的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1、類型支持與代碼提示:TSX 能夠利用 TypeScript 的類型推斷功能,減少開發(fā)中的類型錯(cuò)誤。
2、靈活性與復(fù)用性:TSX 的寫法更貼近 JavaScript 和函數(shù)式編程的范式,因此能更靈活地編寫高階組件和傳遞 props。許多復(fù)雜邏輯可以通過更純粹的函數(shù)式寫法實(shí)現(xiàn)。
3、易于集成第三方庫(kù):TSX 更符合主流 JavaScript 庫(kù)(例如 React)的寫法。
4、更好的代碼組織:對(duì)于團(tuán)隊(duì)開發(fā)中的大型項(xiàng)目,TSX 組件更容易分離邏輯和樣式管理,提升代碼模塊化水平。
缺點(diǎn):
1、學(xué)習(xí)成本增加:TSX 更貼近 JavaScript 函數(shù)式寫法,Vue 開發(fā)者需要理解 JSX 語法。
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 深度開發(fā)者來說,TSX 正成為常見選擇。Vue 團(tuán)隊(duì)未來可能會(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-router項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
vue-router 是 Vue.js 官方的路由庫(kù).這篇文章主要介紹了vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié),需要的朋友可以參考下2018-02-02
在vue項(xiàng)目中引用Antv G2,以餅圖為例講解
這篇文章主要介紹了在vue項(xiàng)目中引用Antv G2,以餅圖為例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue2+elementui的el-table固定列會(huì)遮住橫向滾動(dòng)條及錯(cuò)位問題解決方案
這篇文章主要介紹了vue2+elementui的el-table固定列會(huì)遮住橫向滾動(dòng)條及錯(cuò)位問題解決方案,主要解決固定列錯(cuò)位后, 接下來就是把固定列往上提滾動(dòng)條的高度就不會(huì)影響了,需要的朋友可以參考下2024-01-01
使用mint-ui開發(fā)項(xiàng)目的一些心得(分享)
下面小編就為大家?guī)硪黄褂胢int-ui開發(fā)項(xiàng)目的一些心得(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue使用el-upload實(shí)現(xiàn)文件上傳的實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了vue使用el-upload實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴們可以參考一下2024-01-01
遇到vue前端npm?i報(bào)錯(cuò)多個(gè)版本不一致問題及解決
這篇文章主要介紹了遇到vue前端npm?i報(bào)錯(cuò)多個(gè)版本不一致問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue?ElementUI在el-table中使用el-popover問題
這篇文章主要介紹了Vue?ElementUI在el-table中使用el-popover問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue element-ui table表格滾動(dòng)加載方法
下面小編就為大家分享一篇vue element-ui table表格滾動(dòng)加載方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03

