【问题标题】:window is undefined, Document is undefined - Electron窗口未定义,文档未定义 - 电子
【发布时间】:2018-05-30 10:11:00
【问题描述】:

我正在开发一个应用程序并尝试主要使用 jquery,我已经尝试了所有在线示例,但我似乎无法找到我的错误所在。请有人可以查看我的代码并告诉我我做错了什么.. 这是我的代码..

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

//define jquery..
window.$ = require('jquery')(window);

  // Keep a global reference of the window object, if you don't, the window will
  // be closed automatically when the JavaScript object is garbage collected.
  let win

  function createWindow () {
    // Create the browser window.
    win = new BrowserWindow({
      name:"AtikuDesk",
      width: 396, 
      height: 356, 
      frame:false,     
      transparent: true,
      toolbar: false
    });

    // and load the index.html of the app.
    win.loadFile('assets/index.html')

    // Open the DevTools.
    win.webContents.openDevTools()

    // Emitted when the window is closed.
    win.on('closed', () => {
      // Dereference the window object, usually you would store windows
      // in an array if your app supports multi windows, this is the time
      // when you should delete the corresponding element.
      win = null
    })
  }

  // 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', createWindow)

  // 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 (win === null) {
      createWindow()
    }
  })

  menuarr = [
    {
      label:'Home',
      submenu:[
        {label:'About App'},
        {label:'view statistics'},
        {label:'get help'}
      ]
    },
    {
      label:'Exit',
      click(){
         app.quit()
      }

    }

  ];

  var menu = Menu.buildFromTemplate(menuarr);
  Menu.setApplicationMenu(menu);


function clsApp(e){
   app.quit();
}
$('#clsbtn').bind('click', clsApp);

最后一个函数不起作用,它不起作用,它显示一个窗口未定义的错误,当我尝试使用此代码时.. 没有 Jquery,

document.querySelector('#clsbtn').addEventListener('click', clsApp)

它一直告诉我文档未定义。我无法在网上找到合适的解决方案,如果有人帮我解决这个问题,我会很高兴。

我更喜欢从单独的 javascript 文件中处理我的所有事件和操作,而不是在 html 页面上的内联脚本标签中处理。

谢谢。

【问题讨论】:

  • 看起来代码在document 完成加载之前正在运行。你是如何在你的 html 文件中加载脚本的?
  • 很好的观察,我应该如何加载它。因为我在这里使用电子,如果它是纯 javascript,我会知道该怎么做......
  • 再次查看您的代码,您似乎正在尝试在 Main 进程中使用 jQuery,这是错误的方法。您应该在渲染器进程中使用它。 assets/index.html 我认为这是您的渲染器 html 页面。您应该在 that 页面中加载 jQuery。是定义#clsbtn 的那个页面吗?
  • 并查看here 了解如何在电子中加载 jquery

标签: jquery window electron


【解决方案1】:

从主 JS 文件中删除此代码。然后在 index.html 中:

<script>window.$ = window.jQuery = require('jquery');</script>
<script src="index.js"></script>

还有 index.js :

function clsApp(e){
   app.quit();
}
$('#clsbtn').bind('click', clsApp);

【讨论】:

  • 我实际上已经尝试过了,但也没有用。但让我照你说的做……把代码放在 html 文件中。看看..
  • Uncaught TypeError: $ is not a function at index.html:29 - 这就是我得到的错误。
  • 我已经编辑了答案。我猜 JQuery 必须在 HTML 中定义,而不是在 JS 中。并且一定要完成“npm install jquery”。它必须在你的 package.json 中列出
  • @Andaeiii 还要记住,app 实际上只能从主进程访问。您要么必须在 index.js 中执行 const app = require("electron").remote.app,要么从 clsApp 向主进程发送一条消息,告诉 it 运行 app.quit()
猜你喜欢
  • 1970-01-01
  • 2022-09-18
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多