Vue3中的jsx的babel配置

  • 如果我们希望在项目中使用jsx,那么我们需要添加对jsx的支持:
  1. jsx我们通常会通过Babel来进行转换(React编写的jsx就是通过babel转换的);
  2. 对于Vue来说,我们只需要在Babel中配置对应的插件即可;

** webpack 中安装**

  • 安装Babel支持Vue的jsx插件:
npm install @vue/babel-plugin-jsx -D

在babel.config.js配置文件中配置插件:

module.exports = {
	presets:[
		'@vue/cli-plugin-babel/preset'
	],
	plugins:[
		"@vue/babel-plugin-jsx"
	]
}


Vite环境
如果是Vite环境,需要安装插件:

npm install @vitejs/plugin-vue-jsx -D

在vite.config.js配置文件中配置插件:

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import jsx from '@vitejs/plugin-vue-jsx'  // 引入

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    jsx()  // 使用
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

使用案例

基本使用
<script lang="jsx">
  export default {
    render() {
      return (
        <div class="app">
          <h2>我是标题</h2>
          <p>我是内容, 哈哈哈</p>
        </div>
      )
    }
  }
</script>

<style lang="less" scoped>

</style>

Options-Api中
<script lang="jsx">
  import About from './About.vue'

  export default {
    data(){
      return {
        counter:0
      }
    },
    render(){
      return (
        <div class="app">
          <h2>当前计数:{this.counter}</h2>
          <button onClick={this.increment}>+1</button>
          <button onClick={this.decrement}>-1</button>
          <About />
        </div>
      )
    },
    methods:{
      increment(){
        this.counter++
      },
      decrement(){
        this.counter--
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
Compitions-Api中
<template>
  <jsx />
</template>  

<script lang="jsx" setup>
  import { ref } from 'vue'
  import About from "./About.vue"

  const counter = ref(0)

  const increment = () => {
    counter.value++
  }
  const decrement = () => {
    counter.value--;
  }

  const jsx = () => (
    <div>
      <h2>当前计数:{ counter.value }</h2>
      <button onClick={increment}> +1 </button>
      <button onClick={decrement}> -1 </button>
      <About />
    </div> 
  )
</script>

<style lang="less" scoped>

</style>
setup函数中使用
<script lang="jsx">
  import { ref } from 'vue'
  import About from './About.vue'

  export default {
    setup(){
      const counter = ref(0)

      const increment = () => {
        counter.value++
      }
      const decrement = () => {
        counter.value--
      }

      return () => (
        <div class="app">
          <h2>当前计数:{ counter.value }</h2>  
          <button onClick={increment}>+1</button>
          <button onClick={decrement}>-1</button>
          <About />
        </div>
      )
    }
  }
</script>
<style lang="less" scoped>

</style>

** 感谢大家观看,我们下次见 **

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2019ab

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值