如何在新的 BrowserWindow(电子)中加载 Angular 2+ 路由? [英] How to Load Angular 2+ Routes in a New BrowserWindow (Electron)?

查看:16
本文介绍了如何在新的 BrowserWindow(电子)中加载 Angular 2+ 路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个完成的 Angular 5 应用程序,我想将它转换成一个 Electron 应用程序.除了一件事之外,我已经让应用程序中的所有内容都像在其 Web 应用程序表单中一样工作.我一生都无法弄清楚如何将路由加载到新的 BrowserWindow 中.以下是我正在使用的以及迄今为止我尝试过的:

So I have a finished Angular 5 app that I want to covert into an Electron app. I've gotten everything to work in the app as it did in its Web App form except for one thing. I cannot for the life of me figure out how to load Routes into a new BrowserWindow. Here is what I am working with and what I have tried so far:

我用这个加载 mainWindow:

I load mainWindow with this:

mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'dist/index.html'),
    protocol: 'file:',
    slashes: true
  }));

我通过 mainWindow 中的 routerLink 成功导航到 Contact 并得到以下结果:

I successfully navigate to Contact via routerLink in mainWindow and get this result:

console.log(mainWindow.webContents.getURL()); = file:///C:/Me/Project/dist/contact

现在我希望通过 mainWindow 在 secondWindow 内打开 Contact 页面,而不是导航到 mainWindow 内的联系人:

Now instead of navigating to Contact inside of mainWindow, I want the Contact page opened inside of secondWindow via mainWindow:

index.html:

index.html:

<base href="./">

app.routes.ts:

app.routes.ts:

export const routes: Routes = [{
    path: 'contact', data: { sectionTitle: 'Contact' },
    loadChildren: 'contact/contact.module#ContactModule'
}]

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { useHash: false })
  ],
  exports: [RouterModule],
  declarations: [],
  providers: [],
})
export class AppRoutingModule { }

main.js:

secondWindow = new BrowserWindow({
    parent: mainWindow,
    width: 1024,
    height: 768,
    frame: false,
    minWidth: 1024,
    minHeight: 768,
    show: false
  });

secondWindow.loadURL('file://' + __dirname + 'dist/contact');
secondWindow.show();

这会导致以下错误消息:

This results in this error message:

Not allowed to load local resource: file:///C:/Me/Project/dist/contact

我尝试过的没有运气(也尝试过哈希路由 w/useHash: true):

What I have tried with no luck (tried hash routes as well w/ useHash: true):

secondWindow.loadURL('file://' + __dirname + '/dist/contact');
secondWindow.loadURL('file:' + __dirname + '/dist/contact');
secondWindow.loadURL('file:' + __dirname + 'dist/contact');
secondWindow.loadURL('file:' + __dirname + 'dist/index.html#/contact');

有什么想法吗?这是唯一阻碍此 Electron 应用发布的原因.

Any ideas? It's the only thing holding up the release of this Electron app.

推荐答案

没有看到你的代码和 Angular 设置,很难知道它为什么不起作用.但是,您应该使用 node.js pathurl 模块来构建您的 url.

Without seeing your code and Angular setup it's tricky to know why its not working. You should however be using the node.js path and url modules to build your url.

猜测,我会说你需要加载你的基本 html 文件,而哈希应该是你想要加载的路由:

At a guess, I would say that you need to load your base html file and the hash should be the route you're wanting to load:

const path = require('path');
const url = require('url');

window.loadURL(url.format({
  pathname: path.join(__dirname, './index.html'),
  protocol: 'file:',
  slashes: true,
  hash: '/contact'
}));

这会给出类似的东西:

file:///full-path/to-your/app-root/index.html#/contact"

这意味着您的最后一个示例是最接近的,但您自己手动构建 url 意味着它无效:

That means your last example was the closest but manually building the url yourself means that it was not valid:

secondWindow.loadURL('file:' + __dirname + 'dist/index.html#/contact');

这篇关于如何在新的 BrowserWindow(电子)中加载 Angular 2+ 路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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