vue3+vite使用jsx和tsx詳情
安裝@vitejs/plugin-vue-jsx
yarn add -D @vitejs/plugin-vue-jsx npm i -D @vitejs/plugin-vue-jsx
配置vite.config.js
... import vueJsx from '@vitejs/plugin-vue-jsx'; export default defineConfig({ ? plugins: [vueJsx(), ...], ? ... })
使用實(shí)戰(zhàn)
新建xxx.tsx或者xxx.jsx,注意不再是vue為后綴
第一種寫(xiě)法使用this
使用了this,個(gè)人不太推薦
import { defineComponent, ref } from 'vue'; export default defineComponent({ ? setup(){ ? ? const str = ref<string>('tsx的使用'); ? ? const clickFunc1 = () => { ? ? ? console.log('沒(méi)有參數(shù)'); ? ? } ? ? const clickFunc2 = (msg: string = '默認(rèn)值') => { ? ? ? console.log(msg); ? ? } ? ? return { ? ? ? str, ? ? ? clickFunc1, ? ? ? clickFunc2 ? ? }; ? }, ? render(){ ? ? return ( ? ? ? <> ? ? ? ? <div class='async'>{this.str}</div> ? ? ? ? <button onClick={this.clickFunc1}>不傳參數(shù)點(diǎn)擊</button> ? ? ? ? <button onClick={() => this.clickFunc2('額外參數(shù)')}>傳參數(shù)點(diǎn)擊</button> ? ? ? </> ? ? ); ? } })
第二種寫(xiě)法
函數(shù)體等價(jià)于setup,此方式無(wú)法獲取到props與emits等等(可能我沒(méi)有了解到),存在交互性強(qiáng)的也不建議使用,否則可以考慮
import { defineComponent, ref } from 'vue'; export default defineComponent(() => { ? const str = ref<string>('tsx的使用'); ? const clickFunc1 = () => { ? ? console.log('沒(méi)有參數(shù)'); ? } ? const clickFunc2 = (msg: string = '默認(rèn)值') => { ? ? console.log(msg); ? } ? const render = ( ? ? <> ? ? ? <div class='async'>{str.value}</div> ? ? ? <button onClick={clickFunc1}>不傳參數(shù)點(diǎn)擊</button> ? ? ? <button onClick={() => clickFunc2('額外參數(shù)')}>傳參數(shù)點(diǎn)擊</button> ? ? </> ? ); ? return () => render; })
第三種寫(xiě)法
比較推薦這種寫(xiě)法
import { defineComponent, ref } from 'vue'; export default defineComponent({ ? props: { ? ? params: { ? ? ? type: Object, ? ? ? default: () => {} ? ? } ? }, ? setup(props){ ? ? const str = ref<string>('tsx的使用'); ? ? const clickFunc1 = () => { ? ? ? console.log('沒(méi)有參數(shù)'); ? ? } ? ? const clickFunc2 = (msg: string = '默認(rèn)值') => { ? ? ? console.log(msg); ? ? ? console.log(props); ? ? } ? ? return () => ( ? ? ? <> ? ? ? ? <div class='async'>{str.value}</div> ? ? ? ? <button onClick={clickFunc1}>不傳參數(shù)點(diǎn)擊</button> ? ? ? ? <button onClick={() => clickFunc2('額外參數(shù)')}>傳參數(shù)點(diǎn)擊</button> ? ? ? </> ? ? ); ? } })
到此這篇關(guān)于vue3+vite使用jsx和tsx詳情的文章就介紹到這了,更多相關(guān)vue3 jsx/tsx內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue源碼中要const _toStr = Object.prototype.toString的原因分析
這篇文章主要介紹了Vue源碼中要const _toStr = Object.prototype.toString的原因分析,在文中給大家提到了Object.prototype.toString方法的原理,需要的朋友可以參考下2018-12-12vue中如何動(dòng)態(tài)獲取剩余區(qū)域的滾動(dòng)高度
這篇文章主要介紹了vue中如何動(dòng)態(tài)獲取剩余區(qū)域的滾動(dòng)高度問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Vue狀態(tài)模式實(shí)現(xiàn)窗口??抗δ?靈動(dòng)、自由, 管理后臺(tái)Admin界面)
這篇文章主要介紹了Vue狀態(tài)模式實(shí)現(xiàn)窗口停靠功能(靈動(dòng)、自由, 管理后臺(tái)Admin界面),本文通過(guò)實(shí)例代碼文字說(shuō)明給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Vue3訪問(wèn)頁(yè)面時(shí)自動(dòng)獲取數(shù)據(jù)的方法實(shí)現(xiàn)
本文介紹了在Vue3中如何利用生命周期鉤子函數(shù)和定時(shí)器實(shí)現(xiàn)訪問(wèn)頁(yè)面時(shí)自動(dòng)獲取數(shù)據(jù)的方法,這種方法適用于需要在頁(yè)面加載時(shí)即時(shí)更新數(shù)據(jù)顯示的場(chǎng)景,感興趣的可以了解一下2024-11-11Vue高性能列表GridList組件及實(shí)現(xiàn)思路詳解
這篇文章主要為大家介紹了Vue高性能列表GridList組件及實(shí)現(xiàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Vue Router路由無(wú)法跳轉(zhuǎn)問(wèn)題匯總
這篇文章主要介紹了Vue Router路由無(wú)法跳轉(zhuǎn)問(wèn)題匯總,在這里我整理了部分Vue Router路由無(wú)法跳轉(zhuǎn)問(wèn)題,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09