简体   繁体   English

如何将值作为参数从节点 js 文件发送到电子 main.js 文件的 loadURL()?

[英]how to send value as a parameter to loadURL() of electron main.js file from node js file?

const electron = require("electron");
const { app, BrowserWindow } = electron;
const path = require("path");
​
function createWindow() {
  // Create the browser window.
​
  //window 1
  let win1 = new BrowserWindow({
    width: 1000,
    height: 600,
  });
​
  win1.loadURL("https://twitter.com/");
  win1.on("closed", () => {
    win1 = null;
  });
}
app.on("ready", createWindow);

i need to get url from frontend and access in node.js and pass to main.js loadURL() as a parameter.我需要从前端获取 url 并在 node.js 中访问并传递给 main.js loadURL() 作为参数。

What do you mean by this get url from frontend and access in node.js.从前端获取 url 并在 node.js 中访问是什么意思。 If my recognition is correct, I think you are going to read url from the renderer and send this to Electron main process to create BrowserWindow based on that url.如果我的识别是正确的,我认为您将从渲染器读取 url 并将其发送到 Electron 主进程以基于该 url 创建 BrowserWindow。 So communication between renderer and main.所以渲染器和主之间的通信。 Use this ipcRenderer and ipcMain .使用这个ipcRendereripcMain You can send the url using ipcRenderer like this您可以像这样使用ipcRenderer发送 url

...
    const { ipcRenderer } = require('electron');

    function sendUrlToMain(url) {
      ipcRenderer.sendSync('sendUrlToMain', url);
      return res;
    }
...

Then add this to main.js然后将其添加到 main.js

ipcMain.on('sendUrlToMain',(event, arg) => {
let win1 = new BrowserWindow({
    width: 1000,
    height: 600,
  });
​
  win1.loadURL(arg);
  win1.on("closed", () => {
    win1 = null;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从 web 到电子调用 main.js 文件中的函数? - Is it possible to call a function in main.js file from web to electron? 无法在 electron.js 应用程序的 main.js 文件中加载 node.js 模块 - Can't load node.js module in main.js file of electron.js application 如何发送和接收 ajax 请求而不是从 main.js 文件中抓取代码? - How do I send and receive the ajax request instead of grabbing the code from the main.js file? 如何在electronic中的main.js中从另一个脚本调用函数 - How to call a function in another script from main.js in electron 如何从 vue 中的 main.js 文件重新渲染组件? - How to re render component from main.js file in vue? 如何从 vue 应用程序中的 main.js 文件访问存储 getter 更新值? - How to access store getter updated value from main.js file in vue app? 如何从带有子主题js文件的父主题main.js文件中删除javascript函数 - How to remove a javascript function from a parent theme main.js file with a child theme js file 如何将列表从 other.js 文件导入到我的 main.js 文件? - How to import a list from other .js file to my main.js file? 如何访问从config.js文件到main.js文件的数组? - how to access an array from a config.js file into a main.js file? 电子main.js NODE_ENV不可用 - Electron main.js NODE_ENV is not available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM