// 父 <template> <div> <JSXDemo1 /> </div> </template> <script> import JSXDemo1 from "@/components/JSXDemo1.vue" export default { name: "HomeView", components: { JSXDemo1 } } </script> // JSXDemo1.vue <script> import { ref } from "vue" export default { setup () { const countRef = ref(200) const render = () => { return <p>DEMO1--{countRef.value}</p> // jsx就是js语法,所以要加 .value } return render } } </script>
// 父组件 import { defineComponent, ref } from "vue" import JSXChild from "./JSXChild.jsx" export default defineComponent(() => { // 传入 setup 函数 const countRef = ref(300) const render = () => { return <> <p>DEMO2--{countRef.value}</p> <JSXChild a={countRef.value + 100}></JSXChild> </> } return render }) // 子组件 JSXChild.jsx import { defineComponent } from "vue" export default defineComponent({ // 传入组件配置 props: ["a"], setup (props) { const render = () => { return <> <p>child {props.a}</p> </> } return render } })
// template <template> <p>{{ name }} -- {{ age }}</p> </template> // jsx const render = () => { return <> <p>child {props.a}</p> </> }
// template <template> <div> <watch-effect :msg="msgRef"/> </div> </template> <script> import { ref } from "vue" import WatchEffect from "@/components/WatchEffect.vue" export default { name: "HomeView", components: { WatchEffect, }, setup () { const msgRef = ref("123") return { msgRef } } } </script> // jsx 组件名称不可变,要和引入名字保持一致 import { defineComponent, ref } from "vue" import JSXChild from "./JSXChild.jsx" export default defineComponent(() => { const countRef = ref(300) const render = () => { return <> <p>DEMO2--{countRef.value}</p> <JSXChild a={countRef.value + 100}></JSXChild> </> } return render })
// jsx 属性和事件的写法一样 import { defineComponent, ref } from "vue" import JSXChild from "./JSXChild.jsx" export default defineComponent(() => { const countRef = ref(300) function onChange () { console.log("onChange") } const render = () => { return <> <p>DEMO2--{countRef.value}</p> <JSXChild a={countRef.value + 100} change={onChange}></JSXChild> </> } return render })
// template v-if <template> <p v-if="flagRef">template demo</p> <button @click="changeFlagRef">click</button> </template> <script> import { ref } from "vue" export default { setup () { const flagRef = ref(true) function changeFlagRef () { flagRef.value = !flagRef.value } return { flagRef, changeFlagRef } } } </script> // jsx &&符号判断 import { defineComponent, ref } from "vue" import JSXChild from "./JSXChild.jsx" export default defineComponent(() => { const flagRef = ref(true) function changeFlagRef () { flagRef.value = !flagRef.value } const render = () => { return <> <p onClick={changeFlagRef}>DEMO2--{flagRef.value.toString()}</p> {flagRef.value && <JSXChild a={flagRef.value}></JSXChild>} </> } return render })循环 template 使用 v-for 指令,jsx 使用数组的 .map 函数
// template v-for <template> <ul> <li v-for="item in state.list" :key="item">{{ item }}</li> </ul> </template> <script> import { reactive } from "vue" export default { setup () { const state = reactive({ list: ["a", "b", "c"] }) return { state } } } </script> // jsx 数组 .map 函数 import { defineComponent, reactive } from "vue" export default defineComponent(() => { const state = reactive({ list: ["a1", "b1", "c1"] }) const render = () => { return <> <ul> {state.list.map(item => <li>{item}</li>)} </ul> </> } return render })
到此这篇关于Vue3使用JSX的文章就介绍到这了,更多相关Vue3使用JSX内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!