typescript - location.reload() och location.replace(url) fungerar inte i Electron med Angular 6
Jag använder Electron för att bygga om en färdig webapp. Electron main.js är enkel:
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1600,
height: 900
})
// and load the index.html of the app.
mainWindow.loadFile('dist/mysite/index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
när jag använder 'electron .' för att köra appen, fungerar den bra förutom raden med location. Appkoden nedan är åtgärden för inloggningsformuläret. Efter inloggning finns en rad 'location.reload()' som inte fungerar:
this.http.post(this.url + ':8000/' + 'auth/jwt/create/', this.validateForm.value)
.subscribe((res: any) => {
localStorage.setItem('token', res.token);
// This LINE NOT WORK
location.reload();
// //////
// this.router.navigate([this.router.url]);
// this.router.navigate(['./']);
console.log(location);
}, (err: any) => {
if (err.status === 400) {
this.msg = err.error.non_field_errors[0];
console.log(err);
} else {
this.msg = err;
console.log(err);
}
this.isVisible = true;
});
Jag försöker på många sätt att få webben att gå till mål-URL:en men misslyckades. location.reload() location.replace router.navigate fungerar inte alls. När jag använder 'ng serve' och öppnar sidan med Chrome, har 'location.reload()' inget problem. Så vad ska jag göra? Tack.
REDIGERA: efter att location.reload() har körts, blir sidan blank. och när console.log(location), är href chrome-error://chromewebdata/
console.log(location)
Location {replace: ƒ, assign: ƒ, href: "chrome-error://chromewebdata/", ancestorOrigins: DOMStringList, origin: "null", …}
Svar #1
Det slutade fungera för mig när jag introducerade en Listener till min browserView.webContents.on('will-navigate', ...); och jag stoppade vissa händelser, sidladdningen blockerades av den.
Svar #2
I Angular bör du använda window.location.reload(), eftersom window är ett delat objekt och kan nås var som helst, till exempel localStorage.
window.location.reload()
Du kan också console.log(location) och console.log(window.location) för att veta skillnaden.
