在Electron应用程序中导航Angular路线时黑屏

在Electron应用程序中导航Angular路线时黑屏

本文介绍了在Electron应用程序中导航Angular路线时黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently writing a desktop hybrid app with Electron with AngularJS integration for routing etc, please see following angular config:

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中.

The app loads up fine, and the initial 'dashboard.html' is injected into ng-view perfectly fine.

当我单击一个标签以加载到另一个视图中时,例如在sites.html中,就会出现问题.我得到了全屏白屏,没有任何错误输出到控制台,也没有来自node.js本身的任何错误.

The problem comes when I click on an tag to load in another view, such as sites.html, for example. I get a full white screen with no errors output into the console, nor any errors coming from node.js itself.

我想知道这是一个已知问题,还是我在配置中做错了什么.

I'm wondering whether this is a known problem, or whether I've done something wrong in my config.

推荐答案

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

I've managed to overcome this issue, finally.

最后,这肯定是由于AngularJS对于只有在由网络服务器提供服务时才能正常工作而有些有趣"引起的.

In the end it certainly seemed to be caused by AngularJS being a bit 'funny' about working correctly only when served by a webserver.

考虑到这一点,我在我的电子应用程序的创建窗口"阶段在特定端口上实现了一个快递服务器.然后,我将窗口指向localhost URL,从技术上讲,它现在是运行在电子应用程序中并运行AngularJS的快速应用程序.

With that in mind, I've implemented an express server on a specific port during the 'create window' phase of my electron app. I then point the window at the localhost URL, which is now technically an express app running inside an electron app, running AngularJS.

在使设置符合要求的过程中,我一阵困惑,但现在看来,它也运行得非常完美而且非常迅速.

It confused me for a while getting the setup in line, but now it seems to be working perfectly and very speedy, too.

这是代码!

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:

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

这篇关于在Electron应用程序中导航Angular路线时黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-25 23:53