南京网站制作学校,个人网站做公司网站,网站商城app建设方案,教育培训机构招生方案第045个 查看专栏目录: VUE ------ element UI 专栏目标
在vue和element UI联合技术栈的操控下#xff0c;本专栏提供行之有效的源代码示例和信息点介绍#xff0c;做到灵活运用。 #xff08;1#xff09;提供vue2的一些基本操作#xff1a;安装、引用#xff0c;模板使… 第045个 查看专栏目录: VUE ------ element UI 专栏目标
在vue和element UI联合技术栈的操控下本专栏提供行之有效的源代码示例和信息点介绍做到灵活运用。 1提供vue2的一些基本操作安装、引用模板使用computedwatch生命周期(beforeCreatecreated,beforeMountmounted, beforeUpdateupdated, beforeDestroydestroyedactivateddeactivatederrorCapturedcomponents)、 $root , $parent , $children , $slots , $refs , props, $emit , eventbus ,provide / inject, Vue.observable, $listeners, $attrs, $nextTick , v-for, v-if, v-else,v-else-ifv-onv-prev-cloakv-oncev-model v-html, v-text, keep-aliveslot-scope filters, v-bind.stop, .native, directivesmixinrender国际化Vue Router等 2提供element UI的经典操作安装引用国际化el-rowel-colel-buttonel-linkel-radioel-checkbox el-inputel-select, el-cascader, el-input-number, el-switch,el-slider, el-time-picker, el-date-picker, el-upload, el-rate, el-color-picker, el-transfer, el-form, el-table, el-tree, el-pagination,el-badge,el-avatar,el-skeleton, el-empty, el-descriptions, el-result, el-statistic, el-alert, v-loading, $message, $alert, $prompt, $confirm , $notify, el-breadcrumb, el-page-header,el-tabs ,el-dropdown,el-steps,el-dialog, el-tooltip, el-popover, el-popconfirm, el-card, el-carousel, el-collapse, el-timeline, el-divider, el-calendar, el-image, el-backtopv-infinite-scroll el-drawer等 本文章目录 专栏目标将数组或者文件倒出为JSON格式插件安装导出方法 上传JSON文件解析命令扩展 使用vue做项目时候有时候要上传加载json文件获取到内容然后用于分析应用。有的时候需要将一些文件导出来。
将数组或者文件倒出为JSON格式
插件安装 import FileSaver from ‘file-saver’ 导出方法 const data JSON.stringify(‘存放JS数组或者对象’) const blob new Blob([data], { type: ‘application/json’ }) FileSaver.saveAs(blob, 文件名称.json) 上传JSON文件解析
// 由于我是 vue项目使用的 element-ui的组件库,所以用到 el-upload
el-upload drag :limit1 // 上传一个文件 actionhttps://jsonplaceholder.typicode.com/posts/ // 此处链接随便填写 refupload accept.json // 文件格式 :file-listfileList // 文件列表 :on-successonSuccess // 文件上传成功时响应 :on-removeonRemove // 点击文件后面的小叉叉响应 i classel-icon-upload/i div classel-upload__text将文件拖到此处或em点击上传/em/div div classel-upload__tip slottip上传json文件且只能上传 1 个文件/div /el-upload el-button typeprimary clicksave确定/el-button script export default { data() { return { fileList: [], uploadData: [] } }, methods: { onSuccess(res, file, fileList) { let reader new FileReader() reader.readAsText(file.raw) reader.onload ((e) { this.uploadData [] this.uploadData JSON.parse(e.target.result) }) }, onRemove(file) { this.fileList [] }, save() { // 把数据发送给 父元素 this.$emit(uploadParent, this.uploadData) } } }
/script 命令扩展 JSON.stringify() 将Javascipt值转换为 JSON 字符串 JSON.parse 将JSON字符串转换为对象 Bolb 对象表示一个不可变原始数据的类文件对象。它的数据可以按文本或二进制的格式进行读取也可以转换成ReadableStream来用于数据操作 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件或原始数据缓冲区的内容使用file或Blob对象指定要读取的文件或数据 FileReader.readAsText() 开始读取指定的Blob中的内容。一旦完成result属性中将包含一个字符串以表示所读取的文件内容 onload 文件读取成功完成时触发