简体   繁体   English

如何通过以下方式包含jquery和datatables <script> tag in electron app

[英]How to include jquery and datatables through <script> tag in electron app

I want to include datatables.net inside my electron app. 我想在我的电子应用程序中包含datatables.net。 I did not find a way to include the jquery through a <script> tag. 我没有找到通过<script>标记包含jquery的方法。 When I install it through npm it fails 当我通过npm安装它失败

Uncaught Error: Cannot find module 'datatables.net-dt'

var $       = require( 'jquery' );
var dt      = require( 'datatables.net-dt' )();
//var buttons = require( 'datatables.net-buttons-dt' )();
$(document).ready(function(){alert("ready")});
const remote = require('electron').remote;
const Menu = remote.Menu;
const MenuItem = remote.MenuItem;
const dialog = remote.require('dialog');
const fs = require('fs');
const parser = require('babyparse');

var template = [
{
label: 'File',
submenu: [
  {
    label: 'Open',
    accelerator: 'CmdOrCtrl+O',
    role: 'open',
    click:function(){openFile()}
  }
]
}
]
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

  function openFile () {
   dialog.showOpenDialog({ filters: [
   { name: 'text', extensions: ['txt'] },
   {name:'csv', extensions:['csv']}
  ]}, function (fileNames) {
  if (fileNames === undefined) return;
   var fileName = fileNames[0];
  fs.readFile(fileName, 'utf-8', function (err, data) {
  // console.log(data);
   var elem = document.getElementById("data");
  elem.innerHTML= data;
  parser.parse(data,{complete:function(results){
var json = JSON.stringify(results.data);
var elem = document.getElementById("data");
elem.innerHTML= json;
//console.log(results.data)
 }})
});
  }); 
  }

This is the code inside index.html 这是index.html中的代码
Is this achievable? 这可以实现吗? What happens with javascript libraries which are not provided as modules? 没有作为模块提供的javascript库会怎样? How to use them inside electron? 如何在电子内部使用它们?

As mentioned in the FAQ you've got a couple of options: 常见问题解答中所述,您有两种选择:

  • Switch off Node integration in the Electron BrowserWindow . 在电子BrowserWindow关闭节点集成。
  • Hide the fact that Node integration is switched on by removing require , exports , and module properties from the window object. 隐藏了通过从window对象中删除requireexportsmodule属性来打开Node集成的事实。

When you install jQuery via NPM you can also require it like this in your index.html : 当您通过NPM安装jQuery时,也可以在index.html这样require它:

<head>
  <script>
    window.jQuery = window.$ = require('jquery');
  </script>
</head>

Note that the above snippet assumes the following directory structure: 请注意,以上代码段采用以下目录结构:

node_modules/
  jquery/
index.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM