首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vite-Plugin-Federation不能用钩子导入反应组件

Vite-Plugin-Federation不能用钩子导入反应组件
EN

Stack Overflow用户
提问于 2022-09-25 10:13:58
回答 1查看 112关注 0票数 1

我想使用虚拟联邦来建立一个微观的前端结构。但是,当远程组件有钩子时,主机网站会出现错误。想问问我的环境有什么问题吗?

已使用的插件:@源/vite-plugin-联会: v1.1.9

错误信息:

https://i.stack.imgur.com/GVsmM.png

主机

vite.config.js

代码语言:javascript
复制
export default defineConfig(({ mode }) => {
  plugins: [
      react(),
      federation({
        name: 'host',
        filename: 'remoteEntry.js',
        remotes: {
          imbee_workflow: 'http://localhost:5001/assets/remoteEntry.js',
        },
        shared: ['react', 'react-dom'],
      }),
    ],
    build: {
      outDir: path.join(__dirname, 'build'),
      target: 'esnext',
      minify: false,
      cssCodeSplit: false,
    },
}

代码

代码语言:javascript
复制
import { Box } from '@mui/material';
import React from 'react';

const Button = React.lazy(() => import('remote/Button'));

function hostPage() {
  return (
    <Box height="100%" width="100%" overflow="hidden">
      <Button caption="Hello World" />
    </Box>
  );
}

export default hostPage;

远程

vite.config.js

代码语言:javascript
复制
export default defineConfig({
  plugins: [
    react(),
    federation({
      name: 'remote',
      filename: 'remoteEntry.js',
      exposes: {
        './Button': './src/Button.tsx',
        './Hello': './src/Hello.tsx',
      },
      shared: ['react', 'react-dom'],
    }),
  ],
  build: {
    target: 'esnext',
    minify: false,
    cssCodeSplit: false,
  },
});

代码

代码语言:javascript
复制
import React from 'react';

function Button({ caption = 'Home Button' }) {
  // Without hooks, there won't have any problem.
  const [test, setTest] = React.useState('test');
  return <button>{`${caption}-${test}`}</button>;
}

export default Button;
EN

回答 1

Stack Overflow用户

发布于 2022-10-28 16:07:18

在为简单钩子修复的1.1.11中,尝试:

代码语言:javascript
复制
npm install @originjs/vite-plugin-federation@1.1.11 --save-dev
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73843683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档