在 Electron 应用程序中导航 Angular 路线时出现黑屏
Blank screen when navigating Angular routes within Electron app
我目前正在使用 Electron 编写一个桌面混合应用程序,其中 AngularJS 集成了路由等,请参阅以下 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 本身的任何错误。
我想知道这是一个已知问题,还是我的配置有误。
我终于解决了这个问题。
最后这似乎肯定是由于 AngularJS 有点 'funny' 只有在由网络服务器提供服务时才能正常工作。
考虑到这一点,我在电子应用程序的 'create window' 阶段在特定端口上实现了一个快速服务器。然后我将 window 指向本地主机 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);
运行 最近对此进行了研究,认为接受的答案过于复杂。只需修改
href="#/about" to href="#!/about"
并且一切都按预期工作。
这是由于 AngularJS 而不是 Electron 的变化。如果您不想包含 #!就是修改app.config
中的hashPrfix,加上这个
$locationProvider.hashPrefix('');
这里有一个link到Angular Docs供参考。
希望这能让其他人头疼。
我目前正在使用 Electron 编写一个桌面混合应用程序,其中 AngularJS 集成了路由等,请参阅以下 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 本身的任何错误。
我想知道这是一个已知问题,还是我的配置有误。
我终于解决了这个问题。
最后这似乎肯定是由于 AngularJS 有点 'funny' 只有在由网络服务器提供服务时才能正常工作。
考虑到这一点,我在电子应用程序的 'create window' 阶段在特定端口上实现了一个快速服务器。然后我将 window 指向本地主机 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);
运行 最近对此进行了研究,认为接受的答案过于复杂。只需修改
href="#/about" to href="#!/about"
并且一切都按预期工作。
这是由于 AngularJS 而不是 Electron 的变化。如果您不想包含 #!就是修改app.config
中的hashPrfix,加上这个
$locationProvider.hashPrefix('');
这里有一个link到Angular Docs供参考。
希望这能让其他人头疼。