jQuery在电子主流程上 [英] jQuery on electron main process

查看:138
本文介绍了jQuery在电子主流程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



看来我发现的每个例子都是针对渲染器进程的。



示例我想创建一个util,主进程将使用这个util,它将使用get从api获取数据。

然后使用<$ c
$ b $ c> $。get 产生一个错误, get不是函数



Thanks。

解决方案

jQuery是浏览器的JS库,例如DOM操作等。主进程,因为主进程在 NodeJS 中运行。

很难在不了解应用程序的情况下提出解决方案。如果您需要主流程中的AJAX请求数据,则可以使用 NodeJS https 包。来自 Twilio博客的示例:

  const https = require('https'); 

https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY',(resp)=> {
let data ='';

//已收到大量数据
resp.on('data',(chunk)=> {
data + = chunk;
} );

//收到整个响应,打印结果
resp.on('end',()=> {
console.log JSON.parse(data).explanation);
});

})。on(error,(err)=> {
console.log(错误:+ err.message);
});

编辑:
As @ Hans-Koch提到,可能不应该在渲染器进程中使用jQuery,因为它的主要目的之一是为DOM操作,AJAX等规范化API,而在Electron中,您只需支持Chromium。如果您想制作AJAX请求,您可以使用 XMLHttpRequest 或者包裹它的一些npm包,例如 xhr


How I can use jQuery on electron main process?

It seems every example I find is for renderer process.

Example I want to create a util that will be used by the main process, that will fetch data from an api using get.

Then using $.get makes an error that get is not a function.

Thanks.

解决方案

jQuery is a JS library for the browser, eg DOM manipulating, etc. You shouldn't use that in the main process, since the main process is running in NodeJS.

It's hard to propose a solution without knowing more about your application. If you need the data from the AJAX request in your main process, you can use NodeJS https package. Example from Twilio blog:

const https = require('https');

https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => {
  let data = '';

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(JSON.parse(data).explanation);
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});

Edit: As @Hans-Koch mentioned, you probably shouldn't use jQuery in the renderer process either since one of it's main purpose is to normalize the API for DOM manipulation, AJAX, etc. and in Electron you only have to support Chromium. If you want to make AJAX request you can use the XMLHttpRequest or some npm package which wraps it, eg xhr.

这篇关于jQuery在电子主流程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆