|
| 1 | +<template> |
| 2 | + <div style="text-align: center"> |
| 3 | + <a-button type="primary" @click="showModal">配置 NPM 组件列表</a-button> |
| 4 | + <a-modal |
| 5 | + title="NPM 组件列表配置信息" |
| 6 | + :visible="visible" |
| 7 | + @ok="handleOk" |
| 8 | + :confirmLoading="confirmLoading" |
| 9 | + @cancel="handleCancel" |
| 10 | + > |
| 11 | + <div> |
| 12 | + <a-textarea v-model="text" placeholder="Basic usage" :rows="20" /> |
| 13 | + </div> |
| 14 | + </a-modal> |
| 15 | + </div> |
| 16 | +</template> |
| 17 | +<script> |
| 18 | +import Vue from 'vue' |
| 19 | +export default { |
| 20 | + data () { |
| 21 | + return { |
| 22 | + visible: false, |
| 23 | + confirmLoading: false, |
| 24 | + text: JSON.stringify( |
| 25 | + [ |
| 26 | + { |
| 27 | + 'package': '@luban-h5/lbp-slide', |
| 28 | + 'version': '0.0.7', |
| 29 | + 'name': 'lbp-slide', |
| 30 | + 'icon': 'photo', |
| 31 | + 'i18nTitle': { |
| 32 | + 'en-US': 'Carousel', |
| 33 | + 'zh-CN': '轮播图' |
| 34 | + }, |
| 35 | + title: '轮播图', |
| 36 | + visible: true |
| 37 | + } |
| 38 | + ], null, 2) |
| 39 | + } |
| 40 | + }, |
| 41 | + methods: { |
| 42 | + showModal () { |
| 43 | + this.visible = true |
| 44 | + }, |
| 45 | + handleOk (e) { |
| 46 | + const createjs = window.createjs |
| 47 | +
|
| 48 | + // eslint-disable-next-line no-new-func |
| 49 | + let npmPackages = new Function(`return ${this.text}`.replace('\n', ''))() |
| 50 | + npmPackages = npmPackages.map(pluginInfo => ({ |
| 51 | + ...pluginInfo, |
| 52 | + // src: `https://cdn.jsdelivr.net/npm/${pluginInfo}/dist/${pluginInfo.name}.umd.js` |
| 53 | + // src: `https://unpkg.com/${pluginInfo}/dist/${pluginName}.umd.js` |
| 54 | + src: `https://cdn.jsdelivr.net/npm/${pluginInfo.package}@${pluginInfo.version}/dist/${pluginInfo.name}.umd.js` |
| 55 | + })) |
| 56 | +
|
| 57 | + const queue = new createjs.LoadQueue() |
| 58 | + queue.on('fileload', handleFileLoad, this) |
| 59 | + queue.on('complete', handleComplete, this) |
| 60 | +
|
| 61 | + queue.loadManifest(npmPackages) |
| 62 | + function handleComplete (e) { |
| 63 | + // 可以直接使用 this 的原因: query。on 最后一个参数用来做做 bind this 操作 |
| 64 | + this.visible = false |
| 65 | + this.confirmLoading = false |
| 66 | + this.$emit('loadComplete', npmPackages) |
| 67 | + } |
| 68 | +
|
| 69 | + function handleFileLoad (event) { |
| 70 | + const { name } = event.item |
| 71 | + Vue.component(name, window[name]) |
| 72 | + } |
| 73 | + }, |
| 74 | + handleCancel (e) { |
| 75 | + this.visible = false |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +</script> |
0 commit comments