石家庄站列车时刻表,衡水网站设计公司哪家好,WordPress搭建點播流媒體,江阴网站开发招聘使用electron-vue获取文件夹的路径
记录一次开发过程中遇到的bug#xff0c;我们的项目中需要将vue项目打包为桌面应用软件#xff0c;为此我们引入了electron框架#xff0c;在这个过程中#xff0c;我们需要获取到用户电脑上面文件夹的绝对路径#xff0c;用这篇文章记…使用electron-vue获取文件夹的路径
记录一次开发过程中遇到的bug我们的项目中需要将vue项目打包为桌面应用软件为此我们引入了electron框架在这个过程中我们需要获取到用户电脑上面文件夹的绝对路径用这篇文章记录一下
修改方式
修改vue.config.js 在 vue.config.js 中开启了 Electron 集成这允许你在 Electron 进程中使用 Node.js 功能。
pluginOptions: {electronBuilder: {nodeIntegration: true,contextIsolation: false,}}在background.js添加以下内容 导入依赖
import {ipcMain,ipcRenderer,dialog} from electronapp.on(ready, async () {if (isDevelopment !process.env.IS_TEST) {// Install Vue Devtoolstry {await installExtension(VUEJS_DEVTOOLS)} catch (e) {console.error(Vue Devtools failed to install:, e.toString())}}createWindow()
// 新增在主进程中处理打开文件对话框的请求ipcMain.handle(dialog:openFile,handleFileOpen)
})// 新增处理打开文件对话框的函数
async function handleFileOpen(){const options {title: Select a Folder,properties: [openDirectory]};const {canceled,filePaths}await dialog.showOpenDialog(options)if (canceled){console.log(1)return}else {console.log(2,filePaths)return filePaths[0]}
}在vue页面中编写触发事件 在 Vue 组件中你使用 ipcRenderer 来触发打开文件对话框的操作
templateButton typeinfo stylewidth: 100% clickhandleSaveChart保存/Button
/templatescriptimport {ipcRenderer} from electronexport default {name: DirPage,created() {// const ipc require(electron).ipcRenderer;ipcRenderer.on(save-finished, function (event, filename) {// 当filename等于null的时候表示用户点击了取消按钮// 当用户点击保存按钮的时候filename的值是对应文件的绝对路径console.log(filename)})},methods: {//获取的文件名称handleSaveChart: function () {// 向IPC通道发送信号此时主线程收到信号立即执行相对应的响应函数// const ipcRenderer require(electron).ipcRenderer;const result ipcRenderer.invoke(dialog:openFile);// if (!result.canceled result.filePaths.length 0) {result.then(res{console.log(file,res)})// 在这里可以使用 selectedFolder 的绝对路径进行后续操作// }}}
}
/script总结 background.js 中的 ipcMain.handle这个函数允许你在主进程中注册一个处理函数当从渲染进程发送请求到主进程时会调用这个处理函数并返回结果。在这里我们注册了一个处理函数 handleFileOpen用于打开文件对话框并返回选中的文件夹路径。 Vue 组件中的 ipcRenderer.invoke这个函数用于从渲染进程向主进程发送请求并等待主进程的响应。在这里你向主进程发送了打开文件对话框的请求并使用 invoke 来等待主进程返回选中的文件夹路径。 主线程就是background.js文件 渲染线程就是.vue文件 文章转载自: http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn