久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 require(lib) 與 <script>在電子應(yīng)用程

Use of require(lib) versus lt;scriptgt; in Electron apps(使用 require(lib) 與 lt;scriptgt;在電子應(yīng)用程序中)
本文介紹了使用 require(lib) 與 <script>在電子應(yīng)用程序中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我不知道何時使用 require('jslib')<script src=""></script>電子內(nèi)容頁面(例如 index.html).使用jQuery,我發(fā)現(xiàn)需要按如下方式加載:

<script>window.$ = window.jQuery = require('./js/jquery-2.2.4.min.js');</script>

我需要開始使用其他一些庫(例如 Handlebars、ds3.js、Bootstrap 等),我不確定是否應(yīng)該使用 <script> 標(biāo)簽加載這些庫,或者如果我應(yīng)該要求他們.

解決方案

在模塊捆綁器之前,必須通過 <script> 標(biāo)記或通過模塊加載器(例如

那么你應(yīng)該使用哪種方法呢?

Electron 公開了原生 Node.js require 函數(shù).不利用這一點將是一種恥辱:您可以按名稱要求包,并將您的代碼拆分為可重用的模塊,就像在任何 Node.js 應(yīng)用程序中所做的那樣.

I don't have a handle on when to use require('jslib') versus <script src=""></script> in Electron content pages (e.g. index.html). With jQuery, I discovered that it needs to be loaded as follows:

<script>window.$ = window.jQuery = require('./js/jquery-2.2.4.min.js');</script>

I need to start using some other libraries (e.g. Handlebars, ds3.js, Bootstrap, etc.) and I am not sure if I should be loading those with the <script> tag or if I should require them.

解決方案

Before module bundlers, libraries would have to be imported either via a <script> tag or via module loaders such as RequireJS.

Now it's easier to assume a CommonJS environment and get everything through a module bundler which will expose a require function for you in a browser context.

All of this is not necessary in the context of an Electron app:

In normal browsers, web pages usually run in a sandboxed environment and are not allowed access to native resources. Electron users, however, have the power to use Node.js APIs in web pages allowing lower level operating system interactions.

Cf. renderer process

That means that the native Node.js require function (amongst other things) is available in your renderer process!

Here's a simple Electron app to prove it:

My package.json:

{
  "name": "foobar",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron": "^3.0.7"
  },
  "dependencies": {
    "the-answer": "^1.0.0"
  }
}

My main.js: (the main process)

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

let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadFile('index.html');
}

app.on('ready', createWindow);

My index.html: (the renderer process)

<!DOCTYPE html>
<html>
  <body>
    <script>
      const os = require('os'); // Standard Node.js module
      const answer= require('the-answer'); // An NPM package that returns the answer to everything.
    </script>
    <button onclick="alert(os.platform())">YOUR PLATFORM</button>
    <button onclick="alert(answer)">THE ANSWER</button>
  </body>
</html>

So which method should you use?

Electron exposes the native Node.js require function. It would be a shame not to leverage this: you would be able to require packages by their names and split your code into reusable modules as you would do in any Node.js apps.

這篇關(guān)于使用 require(lib) 與 &lt;script&gt;在電子應(yīng)用程序中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 亚洲欧洲精品在线 | 国产在线一区二区 | 国产精品一区二区欧美黑人喷潮水 | 91网在线播放 | 另类 综合 日韩 欧美 亚洲 | 日韩人体视频 | 青草福利| 精品一区二区三区免费毛片 | 国产高清精品在线 | 黑人一级片视频 | 一区中文 | 欧美a级成人淫片免费看 | 国产在线一区二区三区 | 日韩在线不卡 | 亚洲网在线 | 在线观看中文字幕 | 精品欧美色视频网站在线观看 | 一级毛片黄片 | 国产一区二区a | 国产欧美精品一区 | 9999国产精品欧美久久久久久 | 婷婷久久综合 | 亚洲精品视频在线观看视频 | 四虎影院新地址 | 日韩靠逼 | 日韩在线精品 | 国产欧美日韩久久久 | 欧美伊人 | 午夜在线视频 | 国产ts人妖系列高潮 | 性做久久久久久免费观看欧美 | 91原创视频在线观看 | 羞羞的视频免费看 | 日本欧美国产在线 | 91中文视频 | 国产精品成人一区 | 狠狠艹 | 日韩精品在线看 | 日韩欧美一二三区 | 天天操天天插 | 欧美视频免费 |