在 Electron 应用程序中导航 Angular 路由时出现空白屏幕 [英] Blank screen when navigating Angular routes within Electron app

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

问题描述

我目前正在编写一个带有 AngularJS 集成的 Electron 桌面混合应用程序,用于路由等,请参阅以下 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:

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 路由时出现空白屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆