reactjs - 从 url 加载反应组件
问题描述
我想导入我使用 web pack 捆绑的反应组件。
我可以通过将其本地复制到该文件夹然后导入它来完成任务,
import Any from '.dist/index'
它工作正常。但我想做的是将此 index.js 文件上传到某个地方,例如 Amazon s3。现在我无法以与上述相同的方式导入组件。
我的 webpack.config.js 文件,我曾经通过复制 index.js 和 index.css 文件来导出我在另一个项目中使用的 webpack 生成的捆绑组件
var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index_bundle.js",
libraryTarget: "commonjs2"
},
module: {
rules: [
{ test: /\.(js)$/, use: "babel-loader" },
{ test: /\.css$/, use: ["style-loader", "css-loader"] }
]
},
externals: {
react: "commonjs react"
},
mode: "production",
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
})
]
};
我想从上传到 s3 的文件 url 导入组件。
解决方案
你可以用微应用做你描述的事情。微型应用程序基本上只是一个在运行时从 url 延迟加载到主机应用程序中的组件。无需在设计时安装或导入组件。有一个可用的库可让您使用 HOC 组件执行此操作。
import React from 'react';
import ReactDOM from 'react-dom';
import MicroApp from '@schalltech/honeycomb-react-microapp';
const App = () => {
return (
<MicroApp
config={{
View: {
Name: 'redbox-demo',
Scope: 'beekeeper',
Version: 'latest'
}
}}
/>
);
});
export default App;
您可以在此处找到有关其工作原理的更多信息。
推荐阅读
- javascript - Angular 7 + Firebase: Access to image at from origin has been blocked by CORS policy
- python - SWIG typemap to return an output param char*/size_t
- c++ - clang-tidy duplicate errors when invoked by CMake
- flutter - 如何使用颤振的 NestedScrollView?
- ios - 从 Parse 获取关系用户
- apache-poi - 如何创建 POI VBAMacroReader(POIFSFileSystem fs)
- unix - How exactly are anonymous pipes implemented in Solaris?
- css - 在不使用填充和边距的情况下垂直对齐拉伸的 flex
- javascript - api 正在返回一个带有编号和命名键的数组
- macos - echo $path 没有显示任何其他内容。新笔记本电脑