location.reload()和location.replace(url)在Angular 6的Electron中不起作用

时间:2018-12-20 06:47:40

标签: angular typescript electron

我使用 Electron 重建了一个就绪的Web应用程序。电子 main.js 很简单:

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

当我使用'electron。'运行该应用程序时,除了具有 location 的行之外,它都可以正常运行。  下面的应用代码是登录提交表单操作。  登录后,只有一行'location.reload()'不起作用:

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

我尝试了很多方法来使网络转到目标URL,但失败了。 location.reload()location.replace router.navigate 均不起作用。 当我使用“ ng serve”并使用chrome打开网站时,“ location.reload()”没有问题。所以我该怎么做 ? 谢谢。

编辑: 在location.reload()执行之后,页面变为空白。而当console.log(location)时,href为 chrome-error:// chromewebdata /

console.log(location)  
Location {replace: ƒ, assign: ƒ, href: "chrome-error://chromewebdata/", ancestorOrigins: DOMStringList, origin: "null", …}

2 个答案:

答案 0 :(得分:0)

在Angular中,您应该使用window.location.reload(),因为window是共享对象,并且可以通过localStorage等任何地方进行访问。

window.location.reload()

您也可以console.log(location)console.log(window.location)来了解区别。

答案 1 :(得分:0)

当我向browserView.webContents.on('will-navigate', ...);引入侦听器并且停止某些事件时,页面停止工作,该事件阻止了页面重新加载。