【问题标题】:Blank screen when navigating Angular routes within Electron app在 Electron 应用程序中导航 Angular 路由时出现空白屏幕
【发布时间】:2016-12-22 01:52:16
【问题描述】:

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

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 本身的任何错误。

我想知道这是否是一个已知问题,或者我是否在配置中做错了什么。

【问题讨论】:

  • 您在调试器的控制台中看到了什么?
  • 我在调试控制台中什么也看不到,没有任何输出,只是一个没有网络信息/标记的白屏。
  • 这仍然是个问题吗?
  • @IndraUprade 当然是!我用这个把头发拉出来,我无法解决这个问题。我开始认为这可能与电子不充当网络服务器有关?

标签: angularjs node.js electron


【解决方案1】:

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

最终,这似乎是由于 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);

【讨论】:

  • 你能分享一个示例项目吗?我有同样的问题。
  • 嗨@Vishal,当然,请看这个要点:gist.github.com/adamthomason/2eaf1bf3a8358e1b7b06debc789c13e9 当然,你必须运行'npm install express --save'才能让express工作,但是代码在那个要点应该工作得很好:)
  • 我已更新我的答案以包含工作代码。希望对您有所帮助!
  • 您的解决方案非常酷且复杂,但看起来只是在路径前添加!(例如#!/sites 而不是#/sites 有效)discuss.atom.io/t/angular-ngroute-not-working/32605/7
  • 这样做的缺点是您必须为 express 服务器管理端口冲突等和 CORS(如果电子应用程序调用外部 HTTP API)。我在页面刷新时遇到了同样的问题(得到一个空白页面),但想继续从文件系统提供 AngularJS 应用程序
【解决方案2】:

最近遇到了这个问题,并认为接受的答案过于复杂。简单修改

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

一切都按预期工作。

这是由于 AngularJS 而不是 Electron 的变化。如果您不希望包含 #!就是修改app.config中的hashPrfix,加入这个

$locationProvider.hashPrefix('');

这是Angular Docs的链接供参考。

希望这可以避免其他人头疼。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多