最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

电子中的http协议不能要求渲染器

IT培训 admin 3浏览 0评论

电子中的http协议不能要求渲染器

任何人都可以解释为什么这段代码不起作用?我试图在文件协议中要求渲染器(没有快速静态服务器),它很好,但当我用http协议这样做时,我得到了Uncaught Error: Cannot find module './renderer'

有没有办法在http中要求自定义模块?

谢谢 ...


结构体

- project
    + node_modules
    - app.js
    - index.html
    - package.json
    - renderer.js

app.js

const { app, BrowserWindow } = require('electron');
const express = require('express');
const path = require('path');
const url = require('url');

const server = express();

server.use(express.static(__dirname));

app.on('ready', () => server.listen(3000, createWindow));

function createWindow() {
  let win = new BrowserWindow({ width: 800, height: 600 });
  win.loadURL(url.format({
    protocol: 'http',
    hostname: 'localhost',
    port: 3000
  }));
  win.webContents.openDevTools();
  win.on('closed', function () {
    win = null;
    app.quit();
  });
}

的index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>

<body>
  <div id="root"></div>
  <script>
    require('./renderer');
  </script>
</body>

</html>

renderer.js

var root = document.getElementById('root');
root.innerHTML = 'Hello, World !';
  • 电子版:1.7.10
  • 操作系统:Windows 10
回答如下:

就像你对任何其他模块一样需要它并指定路径,在你的情况下:

require('./renderer')

电子中的http协议不能要求渲染器

任何人都可以解释为什么这段代码不起作用?我试图在文件协议中要求渲染器(没有快速静态服务器),它很好,但当我用http协议这样做时,我得到了Uncaught Error: Cannot find module './renderer'

有没有办法在http中要求自定义模块?

谢谢 ...


结构体

- project
    + node_modules
    - app.js
    - index.html
    - package.json
    - renderer.js

app.js

const { app, BrowserWindow } = require('electron');
const express = require('express');
const path = require('path');
const url = require('url');

const server = express();

server.use(express.static(__dirname));

app.on('ready', () => server.listen(3000, createWindow));

function createWindow() {
  let win = new BrowserWindow({ width: 800, height: 600 });
  win.loadURL(url.format({
    protocol: 'http',
    hostname: 'localhost',
    port: 3000
  }));
  win.webContents.openDevTools();
  win.on('closed', function () {
    win = null;
    app.quit();
  });
}

的index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>

<body>
  <div id="root"></div>
  <script>
    require('./renderer');
  </script>
</body>

</html>

renderer.js

var root = document.getElementById('root');
root.innerHTML = 'Hello, World !';
  • 电子版:1.7.10
  • 操作系统:Windows 10
回答如下:

就像你对任何其他模块一样需要它并指定路径,在你的情况下:

require('./renderer')
发布评论

评论列表 (0)

  1. 暂无评论