我的编程笔记记录我的编程笔记记录
首页
文章
分类
速记
相册
归档
RSS
首页
文章
分类
速记
相册
归档
RSS
  • 全部文章
  • 开发 (25)

    • 编程语言 (9)

      • css (1)

        • 【布局】css布局一本通【转载】
      • javascript (2)

        • canvas (2)

          • canvas处理视频
          • canvas 通过图像数据点阵重新绘制图像
      • php (3)

        • 调试 (1)

          • Xdebug的工作原理及在phpstorm下的配置
        • 源码 (1)

          • PHP实现ZIP流式打包下载(Zip on the fly)
        • composer常用指令
      • vuejs (3)

        • 2.x (1)

          • 封装一个三方组件的通用套路
        • vue中使用jsx开发组件
        • vite小技巧
    • 基础 (1)

      • patterns (1)

        • 结构型 (1)

          • 代理
    • 量化 (1)

      • qmt (1)

        • 大QMT关闭文件IO和Socket后,如何从外部获取特色数据和信号?如何得心应手的使用QMT呢?
    • 数据库 (4)

      • mongodb (1)

        • MongoDB快速入门
      • mysql (3)

        • 基础知识 (2)

          • mysql分页的两种方式
          • mysql取随机数字
        • sql性能监控工具
    • linux (1)

      • 常用 (1)

        • wget下载整个网站或者特定目录
    • tools (9)

      • 开发环境 (2)

        • vscode中的代码片段
        • linux下搭建samba用来和windows共享文件
      • 运维 (1)

        • 安装minio做图床
      • ide (2)

        • IDEA必装插件
        • IDEA文件模板变量
      • DOS常用指令
      • nginx 之 proxy_pass详解
      • nginx下websocket反向代理
      • github快速找项目

vue中使用jsx开发组件

import { defineComponent } from "vue";

// 定义子组件
const ChildComponent = defineComponent({
  name: "ChildComponent",
  setup() {
    return () => <div>ChildComponent</div>;
  },
});
// 也可以使用 render 函数
const ChildComponent = defineComponent({
  name: "ChildComponent",
  render() {
    return <div>ChildComponent</div>;
  },
});
// 定义非受控组件(就是组件内部没有状态)
const UncontrolledComponent = defineComponent({
  name: "UncontrolledComponent",
  setup() {
    return () => <input type="text" />;
  },
});
// 定义组件函数并用闭包保存调用时的参数 这种方式可以用于创建组件库
function createComponent(name, props) {
  return defineComponent({
    name,
    setup() {
      {
        props.name;
      }
      return () => <div>{name}</div>;
    },
  });
}

export default defineComponent({
  name: "App",
  // 定义 props
  props: {
    msg: String,
  },
  // 定义事件
  emits: ["update:msg"],
  setup(props, { slots, attrs, emit, expose }) {
    // 对外暴露的方法
    expose({
      // ...
    });
    // 使用 createComponent
    const ChildComponent = createComponent("ChildComponent", {
      name: "ChildComponent",
    });

    // 返回 jsx
    return () => (
      <div>
        <ChildComponent />
        {h(ChildComponent)}
        <h1>{props.msg}</h1>
        <button onClick={() => emit("update:msg", "Hello Vite + Vue + JSX")}>
          Change msg
        </button>
        {slots.default?.()}
      </div>
    );
  },
});

v-slot 在 jsx 中的使用

export default defineComponent({
  name: "App",
  setup(props, { slots }) {
    // 此处的 slots 是一个对象 可以通过解构获取到具名插槽
    const { default: defaultSlot } = slots;
    // 下面的方式可以将插槽传递给子组件
    return () => (
      <div>
        <h1>{props.msg}</h1>
        <button onClick={() => emit("update:msg", "Hello Vite + Vue + JSX")}>
          Change msg
        </button>
        {slots.default?.()}
        <ChildComponent v-slots={slots} />
      </div>
    );
  },
});

inject 和 provide 在 jsx 中的使用


import { ref, provide } from 'vue'

const Child = ()=>{
	return <GrandChild />
}

const GrandChild = defineComponent({
	name: "App",
	setup(props, { slots }) {
		// 这里注入之后就完全是一个响应数据了,在孙组件修改这个状态其他组件中也会发生变化。也就是说这种行为同样破坏了单向数据流
	    const message = inject('message')
		const add = ()=>{
			message.value = message.value + "12"
		}
    return () => (
      <>
		<p>
		Message to grand child: {{ message }}
		<button @click="add" >+</button>
		</p>
      </>
    );
  },
})

export default defineComponent({
  name: "App",
  setup(props, { slots }) {
    const message = ref('hello')
	provide('message', message)
    return () => (
      <>
        <input v-model="message">
		<Child />
      </>
    );
  },
})


参考资料: 依赖注入 | Vue.js (vuejs.org)

component 在 jsx 中的使用

component 在 jsx 中可以使用 resolveComponent 函数来获取组件

import { defineComponent, resolveComponent } from "vue";

export default defineComponent({
  name: "App",
  setup() {
    const ChildComponent = resolveComponent("ChildComponent");
    return () => (
      <div>
        <ChildComponent />
      </div>
    );
  },
});

参考资料: https://juejin.cn/post/7291463601704255545

inheritAttrs

import { defineComponent } from "vue";

export default defineComponent({
  name: "App",
  // 加入 inheritAttrs: false 后 attrs 就不会被渲染到组件的根元素上
  inheritAttrs: false,
  setup(props, { attrs }) {
    return () => (
      <div>
        <h1>{props.msg}</h1>
        <button onClick={() => emit("update:msg", "Hello Vite + Vue + JSX")}>
          Change msg
        </button>
        {slots.default?.()}
      </div>
    );
  },
});

参考资料: https://cn.vuejs.org/api/options-misc.html#inheritattrs

参考资料

https://cn.vuejs.org/guide/extras/render-function.html https://juejin.cn/post/6844903710229790734

最后更新: 2026/10/5 16:34
下一篇
vite小技巧
← 上一篇mysql分页的两种方式下一篇 →composer常用指令

相关文章

  • vite小技巧开发 / 编程语言 / vuejs
  • 封装一个三方组件的通用套路开发 / 编程语言 / vuejs / 2.x