gpt4 book ai didi

javascript - 在AngularJS/Electron应用程序中设置$ location

转载 作者:行者123 更新时间:2023-12-03 12:39:56 24 4
gpt4 key购买 nike

是否可以在Electon应用程序中的AngularJS中更改$location的URL,但无需隐式加载该URL?问题在于,Electron正在本地加载index.html(和其他资源),这是有意的。但是,当然,它还会将$location设置为本地文件系统。
背景:我之所以需要$location指向服务器的原因是,存在一些现有的旧版代码(我不得更改),并且该代码使用例如$location.search。因此,在Electron应用程序启动后,我需要正确设置位置,以便此旧代码可以正常工作。
更新2020年7月17日
这是请求的示例代码:
我正在尝试使用window.location = "https://example.com?param1=test"设置位置,以便AngularJS函数$location.search()返回param1=test。如上所述,问题是,当设置window.location时,Electron从该服务器加载index.html并替换BrowserWindow的内容。但是我想加载那些资源(index.html,*。js,*。css在本地),我也尝试过:

window.location.href = ...
window.location.assign (...)
window.location.replace (...)
但所有这些都在重新加载页面。

最佳答案

我认为您将要为will-navigate添加一个事件监听器。可以在here中找到文档。重要的一点:

[The will-event event will be] emitted when a user or the page wants to start navigation. It can happen when the window.location object is changed or a user clicks a link in the page.


我以为您的 main.js 文件看起来像这样,简直是九牛一毛,但我希望您能明白。
const {
app
} = require("electron");

let window;
function createWindow(){
window = new BrowserWindow(){...};
// etc....
}

app.on("ready", createWindow);

// For all BrowserWindows you make, the inner bindings will be applied to each;
// more information for "web-contents-created" is here: https://www.electronjs.org/docs/api/app#event-web-contents-created
app.on("web-contents-created", (event, contents) => {
contents.on("will-redirect", (event, navigationUrl) => {

// prevent the window from changing path via "window.location = '....'"
event.preventDefault();
return;
});
});
仅供引用:此事件监听器是 mainly used for security reasons,但是我不明白为什么不能在您的情况下使用它。

关于javascript - 在AngularJS/Electron应用程序中设置$ location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62933983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com