nklayman / vue-cli-plugin-electron-builder

Easily Build Your Vue.js App For Desktop With Electron
https://nklayman.github.io/vue-cli-plugin-electron-builder/
MIT License
4.12k stars 278 forks source link

Blank page in child window #1944

Closed ada-hou closed 1 year ago

ada-hou commented 1 year ago

this is my package.json

{
  "name": "electron-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service electron:build",
    "electron:serve": "vue-cli-service electron:serve",
    "postinstall": "electron-builder install-app-deps",
    "postuninstall": "electron-builder install-app-deps"
  },
  "dependencies": {
    "core-js": "^3.8.3",
    "vue": "^3.2.13",
    "router": "^1.3.7",
    "store2": "^2.14.2",
    "ant-design-vue": "^3.2.15",
    "vue-router": "^4.1.6",
    "basic-ftp": "^5.0.2",
    "electron-localstorage": "^1.0.5",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "electron": "^23.0.0",
    "electron-devtools-installer": "^3.1.0",
    "vue-cli-plugin-electron-builder": "3.0.0-alpha.4"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead",
    "not ie 11"
  ]
}

this is my background.js

'use strict'

import path from 'path'
import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
import '../controller/watchFile'
import '../controller/store'
import contextMenu from '../controller/contextMenu'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      webSecurity: false,
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
      preload: path.join(__dirname, 'preload.js'),
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    win.webContents.openDevTools()
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
    // win.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`)
  }

  win.webContents.on('context-menu', (e, args) => {
    contextMenu(e)
  })
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      await installExtension(VUEJS3_DEVTOOLS)
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

this is my contextMenu.js

const path = require('path')
const { app, Menu, BrowserWindow } = require('electron')

const contextMenu = (event) => {
    const menu = Menu.buildFromTemplate([
        {
            label: 'settings',
            click: () => {
                const win = new BrowserWindow({
                    width: 1000,
                    height: 600,
                    resizable: false,
                    title: 'settings',
                    webPreferences: {
                        // preload: path.join(__dirname, '../preload/preload.js'),
                    }
                })
                // win.loadURL('http://localhost:5173/#/settings')
                win.loadURL(`file://${path.join(__dirname, '../dist/index.html#/settings')}`)
                // win.loadURL('app://./index.html')
                win.webContents.openDevTools()
            }
        },
        {
            label: '关闭',
            click: () => {
                app.quit()
            }
        }
    ])
    menu.popup()
}
module.exports = contextMenu

When I compile, the main window launched works, but the window that pops up from the right-click menu doesn't work.

the issue lies in win.loadURL()? How can I modify?

MatthijsBurgh commented 1 year ago

Sorry, I don't provide support for the V3 alpha releases.

ada-hou commented 1 year ago

Sorry, I don't provide support for the V3 alpha releases.

3.0.0-alpha.4,Is this version not intended for Vue 3?

MatthijsBurgh commented 1 year ago

It is. But i don't provide support for the v3 alpha releases of this project.