3

I'm using the electron-vue boilerplate, and I want to make my mainWindow a fullScreen after clicking a button.

Electron Window API: electron.atom.io/docs/api/browser-window

HTML:

<button @click="setFullScreen">Switch to Full Screen</button>

Vue:

export default {
  name: 'mainComponent',
  methods: {
    setFullScreen: function() {
      mainWindow.setFullScreen(true);
    }
  }

This is not working. How can I use the Electron API in electron-vue?

and the index.js:

'use strict'

import { app, BrowserWindow } from 'electron'
let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:${require('../../../config').port}`
  : `file://${__dirname}/index.html`
    
function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 728,
    width: 1024,
    fullscreen: false,
  })
    
  mainWindow.loadURL(winURL)
 
  mainWindow.on('closed', () => {
    mainWindow = null
  })
    
  // eslint-disable-next-line no-console
  console.log('mainWindow opened')
}
    
app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

you will find it as it is in electron-Vue

the picture shows How the Structure of the folder enter image description here

2
  • where are you defining mainWindow? Commented Apr 19, 2017 at 13:55
  • in index.js, check i have updated the problem! Commented Apr 19, 2017 at 14:06

1 Answer 1

6

mainWindow is not available in your Vue code because it is defined in your main process.

In your single file component, however, you can import remote from electron, where you can get access to the current window. So your code would look something like this.

const {remote} = require("electron")

export default {
  name: 'mainComponent',
  methods: {
    setFullScreen: function() {
      remote.getCurrentWindow().setFullScreen(true);
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What is the alternative for this, as remote has been deprecated? I seen I should do main.js I should add global.api.myFunc. but main.ts is not the same as main.js for vue.
@WORMSS electronjs.org/docs/api/ipc-renderer is probably the way to go. I am going to test this myself as I have the same requirements. I am using vue-cli-plugin-electron-builder though.
@Benargee I ended up doing (simplied) // Vue side window.require('electron').ipcRenderer.invoke('loadFile', filePath); // background.js before createWindow() import { ipcMain } from 'electron'; ipcMain.handle('loadFile', (_, filePath) => require('fs').promises.readFile(filePath, 'utf-8'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.