Vue项目中使用React组件(antd组件库也可以),React中使用Vue组件

在Vue项目中使用React组件(antd组件库也可以),在React中使用Vue组件

  • 下面我写个demo教大家如何在Vue组件库中使用React组件甚至antd组件
  • 触类旁通,在React中使用Vue组件,自行解决.
  • 不推荐企业级项目中使用。

使用vue-cli创建项目

下载依赖

  • 下载React
    • npm install --save react react-dom
    • 自行下载React相关组件库
  • 下载vuera,这样就可以在Vue项目中使用React组件(antd组件库也可以),在React中使用Vue组件
    • npm i -S vuera
      • https://www.npmjs.com/package/vuera
      • https://github.com/akxcv/vuera
  • 下载识别react-jsx语法的依赖
    • npm install @babel/plugin-transform-react-jsx //识别 jsx 语法
    • npm install @vue/cli-plugin-babel
      • 如果package.json没有@vue/cli-plugin-babel再下
      • //解决You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. -》 <\div className=“App”>

配置

  • main.js文件
    import { VuePlugin } from "vuera";
    Vue.use(VuePlugin);
    
  • .babelrc文件
    {
      "plugins": ["@babel/transform-react-jsx"]
    }
    
  • babel.config.js
    module.exports = {
      plugins: ["@babel/plugin-transform-react-jsx"]
    };
    
  • vue.config.js文件
    • css module
      • requireModuleExtension: true//react的css,采用style-module,然后需要开启

项目中的使用

  • MyReactComponent.js文件
    import React from "react";
    import Mode from "./mode.js";//react 组件
    import Header from "./header.js";//react 组件
    import "../assets/style/reset.css";
    import "./App.css";
    
    export default () => {
      return (
        <div className="App">
          <Header></Header>
          <div className="c_mode">
            <Mode></Mode>
          </div>
        </div>
      );
    };
    
  • HelloWorld.vue
    <template>
      <div class="hello">
        <my-react-component @sendValue="sendValue"/>
      </div>
    </template>
    
    <script>
    import MyReactComponent from "./MyReactComponent.js";
    export default {
      name: "HelloWorld",
      props: {
        msg: String,
      },
      components: { "my-react-component": MyReactComponent },
      methods: {
        sendValue(value) {
          console.log(value, "3");
        },
      },
    };
    </script>
    
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值