Javascript全局变量未按预期工作

时间:2020-08-10 13:59:06

标签: express electron

我试图在express中使用我的“ window”变量,但我不能。谁能帮忙

const { app, BrowserWindow } = require("electron")
const expapp = require("express")()

this.window

app.on("ready", () => {
    this.window = new BrowserWindow({webPreferences:{ nodeIntegration: true }})

    this.window.loadURL("https://www.google.com.tr")
    expapp.get("/", (req,res) => {
        res.render("index.ejs")
    })
    /* Sayfayı geri al */
    expapp.get("/back", (req,res) => {
        this.window.selectPreviousTab()
        res.send("ok")
    })
})

expapp.listen(1661, () => { console.log("1661 portu üzerinden dinleniyor") })

当我用快递“ / back”去这条路线时,出现此错误

TypeError: this.window.selectPreviousTab is not a function
    at C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\app.js:15:21
    at Layer.handle [as handle_request] (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\Users\GreXLin85\Desktop\Projelerim\PC\xx\node_modules\express\lib\router\layer.js:95:5)

1 个答案:

答案 0 :(得分:0)

您应该阅读有关电子中的mainrenderer过程的信息: https://www.electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes

main进程中,您没有全局window变量:main进程是node进程,而不是浏览器。

并且BrowserWindow类没有.selectPreviousTab()方法-它不是NSWindow,并且您不是在Swift中编写本机代码。您应该使用electron / js文档而不是native/Swift开发electron应用的文档。

electron是用于使用节点和铬在JS中编写桌面应用程序的框架/平台,它不是本机框架。< / p>