How to Load Angular 2+ Routes in a New BrowserWindow (Electron)?

 ̄綄美尐妖づ 提交于 2019-12-09 07:26:49

问题


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:

I load mainWindow with this:

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

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

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

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

index.html:

<base href="./">

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

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

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


回答1:


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.

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

Which would give something like:

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

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



来源:https://stackoverflow.com/questions/48308385/how-to-load-angular-2-routes-in-a-new-browserwindow-electron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!