vue组件内部引入远程js文件

8 篇文章 0 订阅

vue组件内部引入远程js文件

之所以要做这个是因为,在一个组件内部需要引入一个js文件来定位。如果放在index.html,这样每个组件都会有这个js。所以需要在组件内单独引入。

第一种操作 Dom引入js:

复制代码

export default {
 mounted() { 
    const s = document.createElement('script');
     s.type = 'text/javascript'; 
    s.src = '你的需要的js文件地址'; 
    document.body.appendChild(s); 
    },
 }
 

复制代码

第二种使用 createElement 方法:

复制代码

export default {
  components: {
    'dingtalk': {
      render(createElement) {
        return createElement(
          'script',
          {
            attrs: {
              type: 'text/javascript',
              src: '你的需要的js文件地址',
            },
          },
        );
      },
    },
  },
}

// 使用 <dingtalk></dingtalk> 在页面中调用

复制代码

第三种封装一个组件:

复制代码

export default {
  components: {
   'remote-js': {
    render(createElement) {
      return createElement('script', { attrs: { type: 'text/javascript', src: this.src }});
    },
    props: {
      src: { type: String, required: true },
    },
  },
  },
}

复制代码

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现Vue在线预览Word远程文件,可以使用第三方库`docxtemplater`和`jszip`来处理Word文档,然后使用`FileSaver.js`库将处理后的文件下载到本地,最后使用`vue-doc-preview`插件进行在线预览。 具体步骤如下: 1. 安装依赖:`npm install docxtemplater jszip file-saver vue-doc-preview` 2. 在Vue组件引入依赖: ```javascript import Docxtemplater from 'docxtemplater'; import JSZip from 'jszip'; import FileSaver from 'file-saver'; import VueDocPreview from 'vue-doc-preview'; ``` 3. 创建Vue组件,包含一个按钮和一个用于预览的`<doc-preview>`标签: ```html <template> <div> <button @click="previewDoc">预览Word文档</button> <doc-preview :file="previewFile" v-if="previewFile" /> </div> </template> ``` 4. 在Vue组件中定义`previewDoc`方法,该方法使用`axios`库获取远程Word文件,然后使用`docxtemplater`和`jszip`处理Word文档,并将处理后的文件下载到本地: ```javascript methods: { previewDoc() { axios.get('https://example.com/remote.docx', { responseType: 'arraybuffer' }).then(response => { const content = response.data; const zip = new JSZip(content); const doc = new Docxtemplater().loadZip(zip); doc.setData({...}); // 设置Word文档中的变量 doc.render(); const output = doc.getZip().generate({ type: 'blob', mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); FileSaver.saveAs(output, 'preview.docx'); this.previewFile = 'preview.docx'; }); } } ``` 5. 在Vue组件中注册`VueDocPreview`插件: ```javascript export default { components: { DocPreview: VueDocPreview }, data() { return { previewFile: null } }, ... } ``` 6. 在Vue组件引入CSS样式: ```javascript import 'vue-doc-preview/dist/style.css'; ``` 这样,当用户点击预览按钮时,会先获取远程Word文件,然后使用`docxtemplater`和`jszip`处理Word文档并下载到本地,最后使用`vue-doc-preview`插件进行在线预览。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值