当前位置: 首页 > news >正文

使用veaury,在vue项目中运行react组件

网上的信息太少了,记录一下
我的项目是vue3+webpack
使用:veaury
Veaury 是基于React和Vue3的工具库,主要用于React和Vue在一个项目中公共使用的场景,主要运用在项目迁移、技术栈融合的开发模式、跨技术栈使用第三方组件的场景。
参考:
https://github.com/gloriasoft/veaury/tree/master/dev-project-vue3

建议:把这个项目拉下来运行,如果自己项目存在依赖报错,可以在这个项目上修改对比测试

如果是用vite直接看项目主页的配置:
https://github.com/gloriasoft/veaury/tree/master


前提: 安装
    "react": "^19.1.0","react-dom": "^19.1.0","veaury": "^2.6.2","babel-preset-react-app": "^10.1.0",

因为react版本高于19.0,在main.js中配置:

// react-dom >= 19
import { createRoot } from 'react-dom/client'
import { setVeauryOptions } from 'veaury'
setVeauryOptions({react: {createRoot}
})

写了一个测试用的TestComponent.jsx

import React from 'react'
console.log('React========>?', React)
const TestComponent = () => {return (<div style={{ padding: '20px', border: '1px solid #ccc', borderRadius: '5px' }}><h1>Hello from React!</h1><p>This is a test React component rendered inside a Vue app.</p></div>)
}export default TestComponent

在vue代码中使用:

<template><div><iconContent/></div>
</template>
<script>
import { applyPureReactInVue } from 'veaury'
import TestComponent from '@/views/ai/react/base/app-icon/TestComponent.jsx'
export default {components: {iconContent: applyPureReactInVue(TestComponent)},
}

运行报错:
Uncaught Error: Objects are not valid as a React child (found: object with keys {__v_isVNode, __v_skip, type, props, key, ref, scopeId, slotScopeIds, children, component, suspense, ssContent, ssFallback, dirs, transition, el, anchor, target, targetStart, targetAnchor, staticCount, shapeFlag, patchFlag, dynamicProps, dynamicChildren, appContext, ctx}). If you meant to render a collection of children, use an array instead.

*在这里插入图片描述*

我最开始把这段配置漏了
在这里插入图片描述

加上之后运行又报错:

‘loose’ mode configuration must be the same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods and @babel/plugin-transform-private-property-in-object (when they are enabled).
在这里插入图片描述
我的配置里用了
plugins: [
‘@babel/plugin-transform-private-methods’
]
@babel/plugin-transform-private-methods 插件,来处理依赖中类中的私有方法(以 # 开头的方法)。
如果你没有用,应该不会报错了


最终babel.config.js文件:

const path = require('path')
function resolve (dir) {return path.join(__dirname, dir)
}module.exports = {presets: ['@vue/cli-plugin-babel/preset',['veaury/babel/ReactPreset', {// for dev onlytest: function (filename) {// The files in the following paths are compiled with React's jsx// 我的react代码都放在react目录下if (filename?.startsWith(resolve('src')) && filename.match(/[/\\]react[\\/$]+/) || filename?.startsWith(resolve('../src'))) return filename}}]],plugins: [['@babel/plugin-transform-class-properties', { loose: true }],['@babel/plugin-transform-private-methods', { loose: true }],['@babel/plugin-transform-private-property-in-object', { loose: true }]]
}

重启运行
在这里插入图片描述
ok,最基础的完成了

因为我的项目没有用ts,所以没有用ts的写法
但我要集成的react组件全是用ts写的
后面看看是自行转化成js还是装ts依赖库以支持运行

相关文章:

  • android的配置检查 查看安卓设备配置
  • 力扣面试150题--两数之和 和 快乐数
  • 第七篇:系统分析师第三遍——1、2章
  • 23种设计模式-创建型模式之抽象工厂模式(Java版本)
  • 从零到一:管理系统设计新手如何快速上手?
  • 掌握MySQL:基本查询指令与技巧
  • Kafka消费者端重平衡流程
  • 《软件设计师》复习笔记(14.2)——统一建模语言UML、事务关系图
  • 遨游科普:三防平板除了三防特性?还能实现什么功能?
  • 工业触摸显示器助力智慧工业实验室发展
  • OpenStack Yoga版安装笔记(22)Swift笔记20250418
  • vue3 Element-plus修改内置样式复现代码
  • (7)VTK C++开发示例 --- 使用交互器
  • Java 2025:解锁未来5大技术趋势,Kotlin融合AI新篇
  • 【dify实战】agent结合deepseek实现基于自然语言的数据库问答、Echarts可视化展示、Excel报表下载
  • 什么是线程安全
  • 软件详细设计说明书模板
  • 《Learning Langchain》阅读笔记3-基于 Gemini 的 Langchain如何从LLMs中获取特定格式
  • 【Mamba】MambaVision论文阅读
  • MCP(Model Context Protocol 模型上下文协议)科普
  • 不断深化“数字上海”建设!上海市数据发展管理工作领导小组会议举行
  • 关于沪泰创新合作,泰州市委书记姜冬冬谈到了三个“合”
  • 九部门:将符合条件的家政从业人员纳入公租房等保障范围
  • 多米尼加俱乐部屋顶坍塌事故死亡人数升至232人
  • 南向资金年内净买入额超6000亿港元,创历史同期新高
  • 云南景洪回应“游客打车遭临时加价并拒载”:对司机顶格罚款500元并停运学习