当前位置: 首页 > 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依赖库以支持运行

http://www.xdnf.cn/news/14149.html

相关文章:

  • 汉诺塔专题:P1760 通天之汉诺塔 题解 + Problem D: 汉诺塔 题解
  • AI写程序: 多线程网络扫描网段ip工具
  • STM32使用rand()生成随机数并显示波形
  • 【最后203篇系列】028 FastAPI的后台任务处理
  • JVM之经典垃圾回收器
  • C++数据结构与二叉树详解
  • Kubernetes》》k8s》》Namespace
  • ProfibusDP转ModbusRTU网关,流量计接入新方案!
  • React 中如何获取 DOM:用 useRef 操作非受控组件
  • 珈和科技:无人机技术赋能智慧农业,精准施肥与病虫害监控全面升级
  • Perf学习
  • 使用最新threejs复刻经典贪吃蛇游戏的3D版,附完整源码
  • Spring Boot配置文件优先级全解析:如何优雅覆盖默认配置?
  • 盲超分-双循环对比学习退化网络(蒸馏)
  • Cursor 生成java测试用例
  • k8s低版本1.15安装prometheus+grafana进行Spring boot数据采集
  • npx 的作用以及延伸知识(.bin目录,npm run xx 执行)
  • AI 推理框架详解,包含如COT、ReAct、LLM+P等的详细说明和分类整理,涵盖其原理、应用场景及对比分析
  • Linux 线程互斥
  • Power BI 中 EXCEPT() 函数的使用指南
  • 悟空CRM系统部署+迁移
  • Vue.directive自定义v-指令
  • 【AI部署】腾讯云GPU-常见故障—SadTalker的AI数字人视频—未来之窗超算中心 tb-lightly
  • JAVA中多线程的经典案例
  • 4.黑马学习笔记-SpringMVC(P43-P47)
  • 学习设计模式《一》——简单工厂
  • 算法驱动光场革命:SLM技术引领智能光学新时代
  • 用 NLP + Streamlit,把问卷变成能说话的反馈
  • 红宝书第五十一讲:Web Components:创造你自己的HTML标签
  • 习题2.3 数列求和-加强版