[Question] How to open new window using angular router?
@maximegris thanks for such a great project. i am wondering How to open new window using angular router? can you point me to right direction?
Hi @okonon. You can open a new window to a specific route as such:
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
// Create a browser window
var win = new BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: false,
frame: true,
transparent: false
});
// Load the page + route
win.loadURL('file://' + __dirname + '/index.html#/settings');
I'm using settings as an example route. For this to work:
- index.html should have
<base href=""> - router module should have useHash: true
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'settings',
component: SettingsComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
@maximegris thanks a lot for your help. I was close! did not have a hash in load url line :)
@maximegris
I have both <base href=""> in index.html and router module has useHash: true.
But when I load new window using win.loadURL('file://' + __dirname + '/index.html#/settings'); it opens just index.html HomeComponent.
Could you please help what should I do ASAP?
Thanks
@maximegris
@daxsom Following what you showed, I was left with doubts about one thing.
As you are opening a new route in a new window will not angular create a new instance of the entire application? In other words, your modules and things that need to be loaded at the start of the application would be loaded again when you open this Login window, right?
@daxsom Yes. I am going to open new route(setting route) in the new window when I click a button on home route. But when I try it, it opens new window with the same home route and does not open setting route.
Please help me @maximegris
@tech-eng Can please provide a minimal repo where we can reproduce your issue?
@seopei There is no something special in my project for now. I've just added two components like you did:
const routes: Routes = [
{
path: 'home',
component: HomeComponent
},
{
path: 'setting',
component: SettingsComponent
}
];
And in my home.components.ts, I called this when a button is clicked:
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
// Create a browser window
var win = new BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: false,
frame: true,
transparent: false
});
// Load the page + route
win.loadURL('file://' + __dirname + '/index.html#/setting');
But it always opens home component page instead of settings one.
I already have <base href=""> in index.html and imports: [RouterModule.forRoot(routes, {useHash: true})], as well.
Can you please post the content of home.component.html too? I think you have to add a router-outlet to your template of the HomeComponent
<div class="left-side">
<div class="text-center btn0">
<div class="glyphicon-plus" (click)="goSetting();"></div>
</div>
<div class="text-center btn1">
<div class="glyphicon glyphicon-cog" (click)="goSetting();"></div>
</div>
</div>
<div class="right-side">
<div #container></div>
</div>
This is the content of the home.component.html
This is the home.component.ts file:
import { Component, OnInit, ElementRef, ViewChild, Renderer } from '@angular/core';
import { LocaldbService } from '../../providers/localdb.service';
import { Router } from '@angular/router';
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
const path = require('path');
const url = require('url');
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
public htmlToAdd: string;
public second: boolean = false;
public first: boolean = false;
public isFirstShown: boolean = true;
@ViewChild('container') container: ElementRef;
constructor(private elementRef: ElementRef,
public renderer: Renderer,
public router: Router,
public localDb: LocaldbService) {
this.init();
}
ngOnInit() {}
ngAfterViewInit() {}
init() {}
goSetting() {
var win = new BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: false,
frame: true,
transparent: false
});
win.loadURL(url.format({
pathname: path.join(__dirname, './index.html'),
protocol: 'file:',
slashes: true,
hash: 'setting'
}));
// // Load the page + route
// win.loadURL('file://' + __dirname + '/index.html#/setting');
// this.router.navigateByUrl('setting');
}
}
add this <router-outlet></router-outlet> to your home.component.html and it should work.
<router-outlet></router-outlet>
<div class="left-side">
<div class="text-center btn0">
<div class="glyphicon-plus" (click)="goSetting();"></div>
</div>
<div class="text-center btn1">
<div class="glyphicon glyphicon-cog" (click)="goSetting();"></div>
</div>
</div>
<div class="right-side">
<div #container></div>
</div>
This is new home.component.html file. But it does not work still..
Should I create this as an issue?
@seopei Do you think there is nothing to do with the setting component?
Please help me. This is really urgent issue for now. Thank you!
this does work for me:
my app-routing.module
import { HomeComponent } from './components/home/home.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SettingsComponent } from './components/settings/settings.component';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'settings',
component: SettingsComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
export class AppRoutingModule {
}
my app.modle
@NgModule({
declarations: [
AppComponent,
HomeComponent,
WebviewDirective,
SettingsComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
AppRoutingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (HttpLoaderFactory),
deps: [HttpClient]
}
})
],
providers: [ElectronService],
bootstrap: [AppComponent]
})
export class AppModule {
}
my home.component:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor() {
}
ngOnInit() {
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
// Create a browser window
const win = new BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: false,
frame: true,
transparent: false
});
// Load the page + route
win.loadURL('file://' + __dirname + '/index.html#/settings');
}
}
I see that you call that url load function inside ngOnInit . Can you try to call a function to load new window with that url router in home.component.html file? It still does not go to setting page and it opens home page.
