vue中 把vue页面导出为html页面

vue点击下载按钮将输入的url链接导出后缀为html的文件

<div>
  <el-form ref="updateAgreementContentRef"
           :model="updateAgreementContentForm"
           :rules="updateAgreementContentRules"
           label-width="100px"
  >
    <el-row>
      <el-col>
        <el-form-item prop="extUrl">
          <el-input v-model="updateAgreementContentForm.extUrl" type="url" placeholder="请输入URL链接" />
        </el-form-item>
      </el-col>
    </el-row>

   <el-form-item>
      <el-button @click="batchDownload">下载导出/el-button>
   </el-form-item>
  </el-form>
</div>

  data() {
    return {
      updateAgreementContentForm: {
        extUrl: ''
      },
      updateAgreementContentRules: {
        extUrl: [
          { required: true, message: '请输入URL链接', trigger: 'blur' },
          { validator: this.validateHttpsUrl, trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    // 校验只能输入http或者https格式的url链接地址
    validateHttpsUrl(rule, value, callback) {
      const httpsUrlPattern = /^(https?:\/\/[^\s$.?#].[^\s]*)$/i
      if (!httpsUrlPattern.test(value)) {
        callback(new Error('请输入URL链接'))
      } else {
        callback()
      }
    },

    // 下载文件
      batchDownload() {
        let a = document.createElement('a')
        let url = window.URL.createObjectURL(
          new Blob([this.getHtmlContent()], {
              type: ''
          })
        )
        a.href = url
        a.download = `download.html`  // 下载文件名称
        a.click()
        window.URL.revokeObjectURL(url)
      },

      // 获取文件内容转为html
      getHtmlContent() {
        // style="border: none;"  去除iframe黑色边框
        const template = `<iframe src="${this.updateAgreementContentForm.extUrl}" style="border: none;"></iframe>`
        const html = `
          <!DOCTYPE html>
          <html>
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width,initial-scale=1.0">
            <title>协议与政策</title>
          </head>
          <body>
            <div class="page">
              ${template}
            </div>
          </body>
          </html>`
        return html
      }
  }    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值