美文网首页
webPreferences

webPreferences

作者: ww4u | 来源:发表于2019-08-07 21:13 被阅读0次
    • 实验electron例程
    • 在页面上调用 osenv 居然不成功
    • 而在启动时(main.js)中是可以成功调用的
    • 百思不解,甚至怀疑版本问题
    • 无意中,翻看之前的electron 入门demo发现其在创建BrowserWindow时有参数
    
    function createWindow(){
        win = new BrowserWindow(
            {
                width: 800,
                height: 600,
                webPreferences:{
                    nodeIntegration: true 
                }
            }
        )
    
        win.loadFile("index.html")
    
        win.webContents.openDevTools()
    
        win.on( 'closed', () => {
            win = null 
        })
    }
    
    
    • 怀疑,难道要传入参数,声明中明确是可选参数的
    image.png
    • 一个参数,一个参数地试,终于发现是 nodeIntegration在搞鬼。说明很明确,在页面中调用node。例子中在页面中调用osenv得到homepath
    const osenv = require('osenv');
    function getUsersHomeFolder(){
      return osenv.home();
    }
    
    image.png

    相关文章

      网友评论

          本文标题:webPreferences

          本文链接:https://www.haomeiwen.com/subject/antzdctx.html