Here we take the vite project as an example. If we want to use JSX in the project, we need to install a plug-in@vitejs/plugin-vue-jsx. This plug-in can Let's use JSX/TSX in the project
npm i @vitejs/plugin-vue-jsx -D
After the installation is complete, invite.config.tsJust make a configuration
import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import vueJsx from "@vitejs/plugin-vue-jsx"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue(), vueJsx()], });
Next let’s take a look at how to use JXS?
First of all, the first way is to use it in the.vuefile, which requires Set the lang in the script totsx, return the template
in the setup function or change.vueto.tsx, please note : If the suffix is.tsx, you need to remove the suffix
import { defineComponent } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { return () => Hello World; }, });
//main.ts中引入 import { createApp } from "vue"; import "./style.css"; import App from "./App"; createApp(App).mount("#app");
from the path introduced to this component. At this time, aHello World
//App.tsx export default (props, ctx) =>Hello World;
const Component1 = () =>Component1; const Component2 = () =>Component2; export default () => ();

{{}}for wrapping, while in JSX we use{}for wrapping, such as
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { const text = ref("Hello World"); return () => {text.value}; }, });
.valuein the SFC template, but you need to add.value
v-iffor conditional rendering, such as
yesno
v-if, but Use a writing method that is closer to native
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { const showyes = ref(true); return () => {showyes.value ? yes : no}; }, });
v-if, you may also think of another conditional rendering methodv-show, which is supported in JSXv-show
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { const showyes = ref(true); return () => ( yes no ); }, });
v-forfor list loop rendering, such as
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { const list = ref(["one", "two", "three"]); return () => ( {list.value.map((item, index) => ( {item} ))} ); }, });
clickas an example, use@clickorv-on:click in SFCFor event binding, in JSX, useonClickfor event binding
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { return () => ( { console.log("我被点击"); }} > 点击 ); }, });
withModifiersprovided by vue, such as.self
import { defineComponent, ref, withModifiers } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { return () => ( { console.log("我被点击"); }, ["self"])} > 点击 ); }, });
withModifiersdoes not take effect. It can be set in the form of chained camel case. It is not as good as.once
import { defineComponent } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { return () => ( { console.log("我被点击"); }} > 点击 ); }, });
modelValueor using it in theinputtag
import { defineComponent, ref } from "vue"; export default defineComponent({ name: "app", setup(props, ctx) { const num = ref(0); return () => ; }, });
msg, in SFC you can directly usev-model:msg, but in JSX you need to use an array. If we look directly at the following two examples, you will understand that if our component is namedea_button, this component sends aupdate:changeMsgmethod, the parent component’smsgVariables need to accept the parameters passed by theupdate:changeMsgfunction
import { defineComponent, ref } from "vue"; const Child = (props, { slots }) => { return {slots.default()}; }; export default defineComponent({ name: "app", setup(props, ctx) { const num = ref(0); return () => 默认插槽 ; }, });
import { defineComponent, ref } from "vue"; //@ts-ignore const Child = (props, { slots }) => { return ( {slots.slotOne()} {slots.slotTwo()} {slots.slotThree()} ); }; export default defineComponent({ name: "app", setup(props, ctx) { const num = ref(0); return () => ( {{ slotOne: () => 插槽1, slotTwo: () => 插槽2, slotThree: () => 插槽3, }} ); }, });
import { defineComponent, ref } from "vue"; //@ts-ignore const Child = (props, { slots }) => { const prama1 = "插槽1"; return ( {slots.slotOne(prama1)} {slots.slotTwo()} {slots.slotThree()} ); }; export default defineComponent({ name: "app", setup(props, ctx) { return () => ( {{ slotOne: (pramas: string) => {pramas}, slotTwo: 插槽2, slotThree: 插槽3, }} ); }, });
prama1=slot 1is a variable in the child component, we pass it to the slot content of the parent component throughslots.slotOne(prama1)
The above is the detailed content of How to use jsx/tsx in Vue3. For more information, please follow other related articles on the PHP Chinese website!