vue3+vite使用jsx和tsx详情
作者:Agwenbi 时间:2024-05-10 14:15:47
安装@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(), ...],
? ...
})
使用实战
新建xxx.tsx或者xxx.jsx,注意不再是vue为后缀
第一种写法使用this
使用了this,个人不太推荐
import { defineComponent, ref } from 'vue';
export default defineComponent({
? setup(){
? ? const str = ref<string>('tsx的使用');
? ? const clickFunc1 = () => {
? ? ? console.log('没有参数');
? ? }
? ? const clickFunc2 = (msg: string = '默认值') => {
? ? ? console.log(msg);
? ? }
? ? return {
? ? ? str,
? ? ? clickFunc1,
? ? ? clickFunc2
? ? };
? },
? render(){
? ? return (
? ? ? <>
? ? ? ? <div class='async'>{this.str}</div>
? ? ? ? <button onClick={this.clickFunc1}>不传参数点击</button>
? ? ? ? <button onClick={() => this.clickFunc2('额外参数')}>传参数点击</button>
? ? ? </>
? ? );
? }
})
第二种写法
函数体等价于setup,此方式无法获取到props与emits等等(可能我没有了解到),存在交互性强的也不建议使用,否则可以考虑
import { defineComponent, ref } from 'vue';
export default defineComponent(() => {
? const str = ref<string>('tsx的使用');
? const clickFunc1 = () => {
? ? console.log('没有参数');
? }
? const clickFunc2 = (msg: string = '默认值') => {
? ? console.log(msg);
? }
? const render = (
? ? <>
? ? ? <div class='async'>{str.value}</div>
? ? ? <button onClick={clickFunc1}>不传参数点击</button>
? ? ? <button onClick={() => clickFunc2('额外参数')}>传参数点击</button>
? ? </>
? );
? return () => render;
})
第三种写法
比较推荐这种写法
import { defineComponent, ref } from 'vue';
export default defineComponent({
? props: {
? ? params: {
? ? ? type: Object,
? ? ? default: () => {}
? ? }
? },
? setup(props){
? ? const str = ref<string>('tsx的使用');
? ? const clickFunc1 = () => {
? ? ? console.log('没有参数');
? ? }
? ? const clickFunc2 = (msg: string = '默认值') => {
? ? ? console.log(msg);
? ? ? console.log(props);
? ? }
? ? return () => (
? ? ? <>
? ? ? ? <div class='async'>{str.value}</div>
? ? ? ? <button onClick={clickFunc1}>不传参数点击</button>
? ? ? ? <button onClick={() => clickFunc2('额外参数')}>传参数点击</button>
? ? ? </>
? ? );
? }
})
来源:https://blog.csdn.net/Ag_wenbi/article/details/122210248
标签:vue3,vite,jsx,tsx
0
投稿
猜你喜欢
Mysql5.6启动内存占用过高解决方案
2024-01-20 23:10:25
python实现水仙花数实例讲解
2021-04-05 11:38:05
Go语言的变量定义详情
2024-04-27 15:41:03
Joomla开启SEF的方法
2024-04-30 08:47:39
python 图像处理画一个正弦函数代码实例
2021-08-10 22:17:51
vue3如何实现挂载并使用axios
2023-07-02 16:46:06
Javascript typeof 用法
2013-10-20 20:49:40
Python实现PS图像调整黑白效果示例
2022-08-06 09:15:13
go语言通过结构体生成json示例解析
2024-04-26 17:23:53
利用tkinter实现下拉框联动
2022-02-12 14:55:27
Dreamweaver给你的网页盖个章
2008-02-03 11:35:00
python删除服务器文件代码示例
2023-07-26 15:44:08
Python 常用 PEP8 编码规范详解
2022-09-03 06:21:12
Python:type、object、class与内置类型实例
2023-09-27 08:51:27
一次mysql迁移的方案与踩坑实战记录
2024-01-13 03:34:42
网页设计的12种颜色
2011-05-21 08:40:00
Python使用Pillow实现图像基本变化
2021-08-26 03:30:23
Python Matplotlib绘图基础详细教程
2024-01-16 04:34:41
MS IIS server Frontpage Ext Server漏洞
2008-05-04 09:54:00
MySQL六种约束的示例详解
2024-01-16 19:15:38