在Electron app中导航Angular路线时出现空白屏幕

时间:2016-08-15 20:00:55

标签: angularjs node.js electron

我目前正在编写一个带有Electron的桌面混合应用程序,并将AngularJS集成用于路由等,请参阅以下角配置:

app.config(function($routeProvider, $locationProvider) {

$routeProvider

    .when('/', {
        templateUrl: 'partials/dashboard.html',
        controller: 'dashboardController'
    })

    .when('/sites', {
        templateUrl: 'partials/sites.html',
        controller: 'sitesController'
    })

    .when('/sites/:site', {
        templateUrl: 'partials/site.html',
        controller: 'siteController'
    })

    .when('/sites/:site/content', {
        templateUrl: 'partials/site_content.html',
        controller: 'contentController'
    })

    .when('/sites/:site/content/create', {
        templateUrl: 'partials/site_content_create.html',
        controller: 'createController'
    })

    .when('/sites/:site/content/:contentId/edit', {
        templateUrl: 'partials/site_content_edit.html',
        controller: 'editController'
    })

    .when('/user', {
        templateUrl: 'patials/user.html',
        controller: 'userController'
    })

    .when('/user/edit', {
        templateUrl: 'partials/user_edit.html',
        controller: 'userEditController'
    })

    .when('/login', {
        templateUrl: 'partials/login.html',
        controller: 'loginController'
    })

    .when('/register', {
        templateUrl: 'partials/register.html',
        controller: 'registerController'
    });

$routeProvider.otherwise({
    redirectTo: '/'
});

});

应用程序加载正常,初始的'dashboard.html'完全注入ng-view。

当我点击标签加载到另一个视图(例如sites.html)时出现问题。我得到一个完整的白屏,没有错误输出到控制台,也没有任何错误来自node.js本身。

我想知道这是否是一个已知问题,或者我的配置是否出错了。

2 个答案:

答案 0 :(得分:3)

我终于设法克服了这个问题。

最后,它似乎肯定是由AngularJS引起的一个有趣的'关于仅在由网络服务器提供服务时正常工作。

考虑到这一点,我在“创建窗口”期间在特定端口上实施了快速服务器。我的电子应用程序的阶段。然后我将窗口指向localhost URL,现在技术上是一个在电子应用程序内运行的快速应用程序,运行AngularJS。

让我困惑了一段时间,但是现在它似乎工作得非常好而且速度非常快。

编辑:这是代码!

main.js:

const electron = require('electron');
const server = require("./server");
const sqlite3 = require('sqlite3');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

// 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: 1000, height: 700});

  // and load the index.html of the app.
  //mainWindow.loadURL(`file://${__dirname}/index.html`);
  mainWindow.loadURL(`http://localhost:3333`);

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

process.on('uncaughtException', function (err) {
  console.log(err);
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

server.js:

var path = require('path');
var express = require('express');
var app = express();

app.use(express.static(__dirname));

app.get('/', function (req, res) {
    res.sendfile(__dirname + 'index.html');
});

app.listen(3333);

答案 1 :(得分:3)

最近进入这个并认为接受的答案过于复杂。只需修改

即可
href="#/about" to href="#!/about"

并且所有工作都按预期进行。

这是由于AngularJS而非Electron的变化。另一种解决方案,如果你不想包括#!是通过添加此

来修改app.config中的hashPrfix
$locationProvider.hashPrefix('');

以下是Angular Docs的链接供参考。

希望这可以让别人头疼。