Electron封装配置

Posted ronle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Electron封装配置相关的知识,希望对你有一定的参考价值。

src/renderer/util/option.js

export const mainOption = {
  width: 1000,
  height: 520,
  // width 和 height 将设置为 web 页面的尺寸
  useContentSize: true,
  // 窗口在屏幕居中
  center: true,
  // 窗口是否可以改变尺寸 开启后maximizable失效
  resizable: false,
  // 窗口是否可以移动
  // movable: true,
  // 窗口是否可以最小化
  minimizable: true,
  // 窗口是否可以最大化
  maximizable: true,
  // 置顶窗口
  alwaysOnTop: false,
  webPreferences: {
    // 是否开启 DevTools,开发模式默认位true
    // devTools:true,
    //  是否集成Node
    // nodeIntegration: false,
    // 禁用同源策略
    webSecurity: false,
    // 是否允许一个使用 https的界面来展示由 http URLs 传过来的资源。默认false
    allowDisplayingInsecureContent: false,
    // 是否允许一个使用 https的界面来渲染由 http URLs 提交的html,css,javascript。默认为 false。
    allowRunningInsecureContent: false,
    nativeWindowOpen: true
  },
  show: false,
  backgroundColor: #fff
};

src/main/index.js

import {app, BrowserWindow} from electron
import {mainOption} from "../renderer/util/option";

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== development) {
  global.__static = require(path).join(__dirname, /static).replace(/\/g, \\)
}

let mainWindow;
const winURL = process.env.NODE_ENV === development
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`;

const createWindow = () => {
  mainWindow = new BrowserWindow(mainOption);
  mainWindow.once(ready-to-show, () => {
    mainWindow.show()
  });
  mainWindow.loadURL(winURL);
  mainWindow.setMenu(null);
  mainWindow.on(closed, () => {
    mainWindow = null
  });
};
// 完成初始化
app.on(ready, createWindow);


// 当所有的窗口都被关闭时触发
app.on(window-all-closed, () => {
  if (process.platform !== darwin) {
    app.quit()
  }
});

app.on(activate, () => {
  if (mainWindow === null) {
    createWindow()
  }
});
/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from ‘electron-updater‘

autoUpdater.on(‘update-downloaded‘, () => {
  autoUpdater.quitAndInstall()
})

app.on(‘ready‘, () => {
  if (process.env.NODE_ENV === ‘production‘) autoUpdater.checkForUpdates()
})
 */

 

以上是关于Electron封装配置的主要内容,如果未能解决你的问题,请参考以下文章

可以将Visual Studio代码配置为启动电子

Electron-vue在发送请求时携带cookie

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

vue2.0 代码功能片段