• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

浏览器插件动态插入Vue远程组件

武飞扬头像
大晒啦
帮助1

浏览器插件

配置文件

inject: [
	{ url:"组件链接", el: "插入的选择器" }
]

调用

injectRemoteComponents(config.inject || []);

实现

async function injectRemoteComponents(Components) {
  if (Components && Components.length) {
    for (let item of Components) {
      try {
      	// 参数 url,params,responseType
        const results = await ajaxGet(item.url, { t: Date.now() }, "text");
        new Function(results)();
        const vm = new Vue({
          store,
          // 传递数据
          provide: {},
          render: (h) => h(window.MyLib.default),
        }).$mount(document.createElement("div"));
        document.querySelector(item.el).appendChild(vm.$el);
      } catch (e) {
        console.error("组件加载失败");
      }
    }
  }
}

学新通

生成组件项目

webpack.config.js

const VueLoaderPlugin = require("vue-loader/lib/plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");

module.exports = {
  entry: {
    button: "./components/Button/index.vue"
  },
  output: {
    filename: "js/[name].js",
    path: path.resolve(__dirname, "./dist"),
    library: "MyLib",
    libraryTarget: "umd",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.vue$/,
        use: "vue-loader",
      },
      // sass预处理器
      {
        test: /\.s[ac]ss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
    ],
  },
  plugins: [new CleanWebpackPlugin(), new VueLoaderPlugin()],
  resolve: {
    extensions: [".js", ".vue", ".json"],
  },
  mode: "production",
};

学新通

package.json

"clean-webpack-plugin": "^4.0.0-alpha.0",
"css-loader": "^5.2.4",
"style-loader": "^2.0.0",
"vue-loader": "^15.9.6",
"vue-template-compiler": "^2.6.12",
"webpack": "^5.37.0",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2",
"sass-loader": "^6.0.7",
"node-sass": "^4.14.1"

学新通
可以把生成的button.js可以放到oss上

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiaeggf
系列文章
更多 icon
同类精品
更多 icon
继续加载