当前位置: 首页 > news >正文

html 页面引入 vue 组件之 http-vue-loader.js

一、http-vue-loader.js

           http-vue-loader.js 是一个 Vue 单文件组件加载器,可以让我们在传统的 HTML 页面中使用 Vue 单文件组件,而不必依赖 Node.js 等其他构建工具。它内置了 Vue.js 和样式加载器,并能自动解析 Vue 单文件组件中的所有内容,并将其转化为 JavaScript 代码。

ps :注意:httpVueLoader 加载的单文件导出方式不同:module.exports = {},而不是 export default {}

二、示例

这里对了 elementUI 的上传组件做一个封装,使其能可根据业务来配置上传的附件

2.1. vue 组件

<template><div><el-col :span="24" v-for="(template,index) in uploadsTemplates"><el-card style="margin-top: 20px;"><div slot="header" class="clearfix"><el-row style="display: flex;align-items: center;"><el-col :span="20"><span style="font-size: 16px;">{{template.title}}</span></el-col><el-col :span="4" style="text-align: right;"><el-tag v-if="template.required == 'N'" type="info">非必上传</el-tag><el-tag v-if="template.required == 'Y'" type="danger">必须上传</el-tag></el-col></el-row></div><p style="color: #F56C6C;margin-bottom: 5px;">{{template.description}}</p><el-col :span="12" style="padding: 20px 0 "><el-form-item prop="requiredFile"ref="uploadFormItems":rules="template.success||template.required=='N'?[]:[{required: true, message: '请上传必要件', trigger: 'blur'}]"><el-row><el-upload :auto-upload="true"drag:file-list="template.fileList"ref="uploadComponents"name="attachment":on-preview="(file)=>onPreview(file,template)":before-upload="(file)=>onBeforeUpload(file,template)":on-success="(response,file,fileList)=>onUploadSuccess(response,file,fileList,index)":on-remove="(file,fileList)=>onRemoveFile(file,fileList,index,template)":action="uploadsUrl":data="{ywlx:ywlx,applyNo:applyNo,configId:template.configId}"multiple><i class="el-icon-upload"></i><div>将文件拖到此处,或<em>点击上传</em></div><div class="el-upload__tip" slot="tip">{{msg}}</div></el-upload></el-row></el-form-item></el-col></el-card></el-col><div class="demo-image__preview" style="display: none"><el-imagestyle="width: 100px; height: 100px"ref="imagePreview":src="previewSrc":preview-src-list="previewSrcList"></el-image></div></div>
</template><script>
module.exports = {name: "upload-template",props: ['contextPath', 'applyNo', 'ywlx', 'fromedit','msg','beforeUpload'],data() {return {uploadsUrl: this.contextPath + "/system/sysUploadFile/uploadAttachment",//预览图片弹窗imgDialogVisible: false,//预览图片路径previewSrc: '',//预览图片集合previewSrcList: [],// 要件(只是校验用)requiredFile: [],//上传模板uploadsTemplates: [],//上传前钩子onBeforeUpload: this.beforeUpload}},mounted: function () {if(this.onBeforeUpload == null){this.onBeforeUpload = (file,upload)=>{return true;}}else{if (typeof this.onBeforeUpload === 'function') {} else {throw new Error('beforeUpload is not a function');}}// 读取附件模板$.ajax({type: "get",async: false,url: this.contextPath + '/system/sysUploadConfig/getUploadsTemplates',data: {ywlx: this.ywlx},dataType: "json"}).then((response) => {if (response.code == 0) {response.data.forEach(template => {template.success = false;template.fileList = [];});this.uploadsTemplates.push(...response.data);if (this.fromedit) {$.ajax({type: "post",async: false,url: this.contextPath + "/system/sysUploadFile/getFilesByApplyNo",dataType: "json",data: {ywlx:this.ywlx,applyNo: this.applyNo}}).then((response) => {if (response.code == 0) {response.data.forEach(attachemnt => {this.uploadsTemplates.forEach(template => {if (attachemnt.configId === template.configId) {template.fileList.push(attachemnt);template.success = true;}})});} else {this.$message({showClose: true,message: response.msg,type: 'error',offset: 200,duration: 10000});console.log("error:");console.log(response);}});}} else {this.$message({showClose: true,message: response.msg,type: 'error',offset: 200,duration: 10000});console.log("error:");console.log(response);}});},methods: {//预览图片onPreview: function (file, template) {this.previewSrc = this.contextPath+(file.url?file.url:file.response.data.url);template.fileList.forEach((file)=>{this.previewSrcList.push(this.contextPath+(file.url?file.url:file.response.data.url));});console.log(this.previewSrc)console.log(this.previewSrcList)this.$refs.imagePreview.clickHandler();},//文件上传成功的回调onUploadSuccess: function (response, file, fileList, index) {console.log('上传成功需要操作请增加事件:upload-success')if (response.code == 0) {// 把要件列表中的当前这个要件的success置为truethis.uploadsTemplates[index].success = true;this.uploadsTemplates[index].fileList = fileList;}else {this.$message({showClose: true,message: response.msg,type: 'error',offset: 200,duration: 2000});//删除控件里某个文件fileList.splice(fileList.indexOf(file),1);}this.$emit('upload-success',response)//清除控件里所有文件// this.$refs.uploadComponents[index].clearFiles();},//移除某个文件onRemoveFile: function (file, fileList, index, upload) {//调用远程移除附件,传递file.fileId$.ajax({url: this.contextPath + '/system/sysUploadFile/removeAttachment',method: 'post',async: false,data: {fileId: file.fileId?file.fileId:file.response.data.fileId}}).then((response) => {if (response.code == 0) {this.uploadsTemplates[index].fileList = fileList;// 如果已经全部移除,加上校验if(fileList.length===0){this.uploadsTemplates[index].success = false;}} else {console.log("error message:");console.log(response.data);this.$message({showClose: true,message: response.msg,type: 'error',offset: 200,duration: 2000});}});},},
}
</script><style scoped>.el-upload__input{display: none;}
</style>

2.2. html 页面

页面中添加 http-vue-loader.js 后添加组件两种方式
方式一:

// 使用httpVueLoader
Vue.use(httpVueLoader);
var vm = new Vue({el: "#upload-test-form",components: {// 将组建加入组建库'upload-template': 'url:'+ctx+'components/upload-template.vue',},data: {ctx:ctx,fromEdit:true,applyForm: {ywlx:"ywlx1",applyNo:"123456789",},},
});

方式二:

var vm = new Vue({el: "#upload-test-form",components: {// 将组建加入组建库'upload-template': httpVueLoader(ctx+'components/upload-template.vue'),},data: {ctx:ctx,applyForm: {ywlx:"ywlx1",applyNo:"123456789",},},});
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head><th:block th:include="include :: header('新增上传文件信息')" /><th:block th:include="include :: element-ui('新增上传文件信息')" />
</head>
<body class="white-bg"><div class="wrapper wrapper-content animated fadeInRight ibox-content" id="upload-test-form"><div class="row"><el-form  ref="uploadForm" id="uploadForm" :model="applyForm"><upload-template :apply-no="applyForm.applyNo":context-path="ctx":before-upload="beforeUpload"@upload-success="uploadSuccess"ywlx="ywlx1"></upload-template></el-form></div></div><th:block th:include="include :: footer" /><th:block th:include="include :: element-ui-js" /><script th:src="@{/lib/http-vue-loader.js}"></script><script th:inline="javascript">var prefix = ctx + "system/sysUploadFile";var vm = new Vue({el: "#upload-test-form",components: {// 将组建加入组建库'upload-template': httpVueLoader(ctx+'components/upload-template.vue'),},data: {ctx:ctx,applyForm: {ywlx:"ywlx1",applyNo:"123456789",},},methods: {beforeUpload: function(file,template){console.log("上传前操作")console.log(file)console.log(template)//返回 true,继续上传,返回false,终止上传return false;},uploadSuccess: function(file){console.log("上传成功操作")console.log(file)}}});function submitHandler() {vm.$refs.uploadForm.validate((valid) => {if (valid) {alert('文件验证通过!');} else {alert('文件验证失败!');}});}</script>
</body>
</html>

3.3. 效果

业务类型上传文件配置
在这里插入图片描述
业务上传附件页面
在这里插入图片描述
保存的文件
在这里插入图片描述

总结:
通过 http-vue-loader.js,我们可以在普通的 HTML 页面中使用 Vue 单文件组件来实现前端开发的快速迭代。在使用 http-vue-loader.js 时,我们需要引入 Vue.js 和 http-vue-loader.js,并使用 httpVueLoader () 方法加载 Vue 单文件组件,并将其实例化为 Vue 对象。

http://www.lryc.cn/news/433622.html

相关文章:

  • html+css网页设计 旅行 蜘蛛旅行社3个页面
  • 考拉悠然产品发布会丨以悠然远智全模态AI应用平台探索AI行业应用
  • LLM大模型学习:揭秘LLM应用构建:探究文本加载器的必要性及在LangChain中的运用
  • Flutter函数
  • P3565 [POI2014] HOT-Hotels
  • 设计模式 | 单例模式
  • Web安全之CSRF攻击详解与防护
  • IDEA运行Java程序提示“java: 警告: 源发行版 11 需要目标发行版 11”
  • 车载测试| 汽车的五域架构 (含线控技术知识)
  • 【Linux】gcc/g++ 、make/Makefile、git、gdb 的使用
  • Elastic Stack--ES的DSL语句查询
  • ARM基础知识---CPU---处理器
  • 将星 x17 安装ubuntu 20.04 双系统
  • E31.【C语言】练习:指针运算习题集(上)
  • git分支的管理
  • 对于消息队列的一些思考
  • IM即时通讯软件-WorkPlus私有化部署的局域网即时通讯工具
  • AI大模型的饕餮盛宴,系统学习大模型技术,你想要的书都在这里了
  • 支付宝开放平台-开发者社区——AI 日报「9 月 9 日」
  • 将AI与情境定位结合以确保品牌安全
  • OpenAI 联合 SWE 发布 AI 软件工程能力测试集,Gru.ai 荣登榜首
  • 一文读懂SpringMVC的工作原理
  • 【python-斐波那契数列和完美数之间的区别】
  • 【redis】本地windows五分钟快速安装redis
  • arm64高速缓存基础知识
  • 物管王 物业管理系统软件
  • YOLOv10改进:CA注意力机制【注意力系列篇】(附详细的修改步骤,以及代码,目标检测效果优于SE和CBAM注意力)
  • 使用go语言获取海南七星彩历史开奖记录并打印输出
  • 使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统
  • 记录ssl epoll的tcp socket服务端在客户端断开时崩溃的问题