欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+vite使用jsx和tsx詳情

 更新時(shí)間:2022年05月31日 09:03:34   作者:Agwenbi  
這篇文章主要介紹了vue3+vite使用jsx和tsx詳情,文章通過(guò)安裝@vitejs/plugin-vue-jsx展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

安裝@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)文章

最新評(píng)論