Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

location.reload() and location.replace(url) not work in Electron with Angular 6

I use Electron to rebuild a ready web app. the electron main.js is simple:

    // 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()
  }
})

when i use 'electron .' run the app, it works good except the line with location. The app code below is the login submit form action. After login there is one line 'location.reload()' not work:

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;
  });

I try lots of ways to make the web go to the target url but failed. location.reload() location.replace router.navigate all not work. When I use 'ng serve' and open site with chrome, 'location.reload()' has no problem. So what should I do ? Thank you.

EDIT: after location.reload() executed , the page go to blank. and when console.log(location), the href is chrome-error://chromewebdata/

console.log(location)  
Location {replace: ƒ, assign: ƒ, href: "chrome-error://chromewebdata/", ancestorOrigins: DOMStringList, origin: "null", …}
like image 696
Hermit Huang Avatar asked Dec 15 '25 05:12

Hermit Huang


1 Answers

It stopped working for me when I introduced a Listener to my browserView.webContents.on('will-navigate', ...); and I was stopping some events, page reloading was blocked by that one.

like image 128
Mateo Tibaquira Avatar answered Dec 16 '25 22:12

Mateo Tibaquira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!