English 中文(简体)
如何使用 WebPack 5 在 React 微前端中调用 Vue 组件
原标题:How to Call Vue component inside React Micro Frontend using WebPack 5
I have started to learn micro frontend. First, I have create a react mf project Micro-Shell-App using npx create-mf-app. Second, I have created again vue mf project that is named SectionPart using same npx code. I want to share an any vue component with react shell app. How can I do it ? I am trying something but i did not find a solution. I am facing error like: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Do you have any advice to me? Thanks from now. Here is folder structure: MyComponent.vue: SectionPart webpack.config.js: // some imports module.exports = (_, argv) => ({ output: { publicPath: "http://localhost:3003/", }, resolve: { extensions: [".tsx", ".ts", ".vue", ".jsx", ".js", ".json"], }, devServer: { port: 3003, historyApiFallback: true, }, module: { rules: [ { test: /.vue$/, loader: "vue-loader", }, { test: /.tsx?$/, use: [ "babel-loader", { loader: "ts-loader", options: { transpileOnly: true, appendTsSuffixTo: ["\.vue$"], happyPackMode: true, }, }, ], }, { test: /.(css|s[ac]ss)$/i, use: ["style-loader", "css-loader", "postcss-loader"], }, ], }, plugins: [ new VueLoaderPlugin(), new ModuleFederationPlugin({ name: "SectionPart", filename: "remoteEntry.js", remotes: { }, exposes: { "./MyComponent":"./src/components/MyComponent.vue", }, shared: require("./package.json").dependencies, }), new HtmlWebPackPlugin({ template: "./src/index.html", }), new Dotenv() ], }); Micro Shell App webpack.config.js: // some imports module.exports = (_, argv) => ({ output: { publicPath: "http://localhost:3000/", }, resolve: { extensions: [".tsx", ".ts", ".jsx", ".js", ".json"], }, devServer: { port: 3000, historyApiFallback: true, }, module: { rules: [ { test: /.m?js/, type: "javascript/auto", resolve: { fullySpecified: false, }, }, { test: /.(css|s[ac]ss)$/i, use: ["style-loader", "css-loader", "postcss-loader"], }, { test: /.(ts|tsx|js|jsx)$/, exclude: /node_modules/, use: { loader: "babel-loader", }, }, { test: /.vue$/, loader: "vue-loader", exclude: /node_modules/ } ], }, plugins: [ new ModuleFederationPlugin({ name: "Micro_Shell_App", filename: "remoteEntry.js", remotes: { section:"SectionPart@http://localhost:3003/remoteEntry.js" }, exposes: {}, shared: { ...deps, react: { singleton: true, requiredVersion: deps.react, }, "react-dom": { singleton: true, requiredVersion: deps["react-dom"], }, }, }), new HtmlWebPackPlugin({ template: "./src/index.html", }), new VueLoaderPlugin(), new Dotenv() ], }); App.jsx at Shell: import React from "react"; import ReactDOM from "react-dom"; import MyComponent from "section/MyComponent" const App = () => ( ); ReactDOM.render(, document.getElementById("app"));
问题回答
The approach to this is simply create a wrapper or function on your Vue application accepting an element that will be the mount point for that component. Look for this video for reference. For example, we want to expose Products to other microfrontends, the idea is to have the consumer application pass a element reference that will be the mount point for our Vue component. ProductsWrapper.js import { createApp } from "vue" import Products from "./Products.vue" export default (el) => { createApp(Products).mount(el) } Update your module federation settings like so: exposes: { "./Products": "./src/Products.vue", "./ProductsWrapper": "./src/ProductsWrapper.js", }, In your react app, simply call the ProductsWrapper function and pass a ref where we will mount the Products vue component. const App = () => { const productsRef = useRef(null); useEffect(() => { ProductsWrapper(productsRef.current) }, []) return <>
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签