electron

本文介绍了在Electron应用中如何引入jq库,实现页面跳转,使用node模块,发送Ajax请求读取本地json数据,读取文件以及调用Electron主进程方法。特别强调了注意事项,如禁用某些安全设置,避免使用特定模块,并提醒目录命名避免中文。
摘要由CSDN通过智能技术生成

使用jq(版本不同导入方式也不同)

 <script src="js/jquery-1.11.3.min.js"></script>
    <script>
        if (typeof module === 'object') {
            window.jQuery = window.$ = module.exports;
        };
    </script>

页面跳转

  window.location.href = 'home.html'

使用node模块

在main.js中配置
   // 隐藏菜单栏 
  Menu.setApplicationMenu(null)

  mainWindow = new BrowserWindow({
    // 如果不默认全屏就开启这个
    width: 1024,
    height: 682,
    webPreferences: {
      //注意大小写
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,   // 使用remote模块
      webSecurity: false,
      preload: path.join(__dirname, 'preload.js')
    }
  })
  // 默认打开工具栏
  mainWindow.webContents.openDevTools(); 

发送Ajax请求本地json数据

1.先在dist目录下新建data目录,下面新建json数据
2.请求

    $(".btn_json").click(function(){
        $.ajax({
            type:"GET",
            url:"data/json/index.json",
            success:(res)=>{
                console.log(res)
            }
        })
    })

读取文件(node模块之读取文件)

1.在dist下新建data>video
2.获取

  var path = require("path");
    var fs = require("fs");
    console.log(__dirname)  // ...Main-win32-x64\resources\app\dist    
    fs.readdir(__dirname+"/data/video", function(err, files){
        if(err){
          return  console.log(err)
        }
        console.log(files) 
    });

调用electron主进程方法

1.直接在dist下js文件中导入

const { app } = require('electron').remote

2.使用
// 退出

    $("#exit").click(() => {
        app.exit()
    })

注意事项

1.不能使用 electron-store
2. 创建目录不能有中文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值