南梦倾寒 2019-11-30 09:38 采纳率: 0%
浏览 1025

electron,分支页面无法实现require功能

此问题悬赏金(10元),帮助我解决问题即可获得,联系方式:1109039558(QQ)

我写了一个electron文件,文件中包含index.html(主)和test.html
单独运行test页面,功能没有任何问题
但是从index页面进入test页面,test页面无法正常运行.
原因可能如下,我在test导入bcli.js库,这个js是我自己写的,里面有一些函数,如check123 opening123等等

<script>
        require("./js/electron/bcli.js");
</script>

我上网上查了好多资料,都没有任何用,包括下面这个

webPreferences: { 
      nodeIntegration: true
    }

我现在很无奈,求各位大佬帮帮我,错误问题如下,其中check123,我定义在了bcli.js中.

Uncaught ReferenceError: check123 is not defined

刚才一位朋友提出的建议,我试了,代码如下:上面两行代码没有注释时,结果可以运行,可是我将上面两行代码改成下面两行,结果运行不了.

// const $ = require('jquery');       // jQuery
// const shell = require('shelljs');      // cli executor
import {$} from require('jquery');
import {shell} from require('shelljs');

可能是我理解有问题,上面的问题,我想在描述一下,我普通的function可以运行,可是当我在支页面运行我写的代码,就运行不了,我把我写的代码附上去:


// libraries
const os = require('os');           // operating system
const path = require('path');         // path joiner
const $ = require('jquery');       // jQuery
const shell = require('shelljs');      // cli executor

// global varialbes, DO NOT CHANGE
let executing = false;
let imgNotFound = false;
let imgID = "#shrimp";


var fs=require("fs")

const {dialog}=require('electron').remote

 opendialog=function(){
    if (executing) return;  
    let imgPathes = [];
    dialog.showOpenDialog({
      title:"选择文件:",
          filters: [
            { name: 'Images', extensions: ['jpg', 'png', 'bmp'] },
          ]
      },result => {
        if (executing) return;   // do nothing if we are already executing the files
        // otherwise try to get pdf pathes user has dropped to us
        let imgPathes = [];
        imgPathes.push(result);
        fs.writeFile("/home/tju/桌面/abcd28/path1.txt",result,(err,data)=>{
            if(err) throw err;                           
        })
        let len = imgPathes.length;
        var i=0;
        if (len >= 1) {    // if we got 1 or more pdf pathes, start executing
            console.log(imgPathes[i]);
            execute(imgPathes[i]);
            i=i+1;
        } else {
            console.error("Need exact 1 image");
        }
    })
    }

function rmImg(imgPath) {


    let str1=`cp -r `+ imgPath + ` `+` /home/tju/桌面/xuejie/train/SRGAN_CL/input/new.jpg`;
    fs.writeFile("./path.txt",str1,(err,data)=>{

        if(err) throw err;                           
    })

     let str=str1 +`  && cd /home/tju/桌面/xuejie/train/SRGAN_CL && python test2.py --path1=/home/tju/桌面/xuejie/train/SRGAN_CL/input/new.jpg`;
    fs.writeFile("/home/tju/桌面/cmd.txt",str,(err,data)=>{

        if(err) throw err;                           
    })

    let cmd = str;
    shell.exec(cmd, {silent:false}, (code, stdout, stderr)=>{
        if(code === 0){
            console.log({code: code, stdout: stdout, stderr: stderr});
        }else{
            console.error({code: code, stdout: stdout, stderr: stderr});
        }
    });

};

    $('#refresh').click(()=>{
        //  $('#input1').load(location.href+'#input1');
        setTimeout(() => {
        javascript:location.reload();   
          }, 10000);       
    });

async function execute(imgPath) {
    executing = true;   // tell ui we are doing executing!
    try {
        let result = await rmImg(imgPath);
        console.log(result);
    } catch (err) {    // will catch >>first<< error in the try block
        console.error(error);
    }
    executing = false; // tell ui the shrink is done
}

 check123=function(){
    var textarea=this.document.querySelector('#textarea');
    fs.readFile('/home/tju/桌面/abcd28/path1.txt','utf-8',function(err,data){
        if(err){
            console.error(err);
        }
        else{
            textarea.innerHTML= data;
        }
    });
    var textarea1=this.document.querySelector('#textarea1');
    fs.readFile('/home/tju/桌面/xuejie/train/SRGAN_CL/sta_2019_16_1080/shuchu.csv','utf-8',function(err,data){
        if(err){
            console.error(err);
        }
        else{
            textarea1.innerHTML= data;
        }
    });
 }

console.log("script loaded");


  • 写回答

1条回答 默认 最新

  • facing-screen 2019-11-30 10:50
    关注

    页面是渲染进程,建议使用import,如有帮助记得采纳

    /---/
    接上次回复:
    你这个问题被你改的,我的耐心真是越来约好了(居然看完了)。。。
    记得你之前是问标题的问题即:无法require。。。

    /---/
    至于你现在import出现问题,这个 check123 is not defined,
    推断是文件没有引入成功,import之前应该对被引入的文件做export,
    这个是js基本语法,建议看看用法先
    还有:nodeIntegration: true也是可以解决问题,你所谓的支页面。。。
    应该是new BrowserWindow(config)出来的页面吧?
    只要在config里正确配置了nodeIntegration: true 也是可以用require的;
    额,你不会是直接window.open(url)的新窗口吧那它自然是没有node的api里,所以叫你用import。

    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)