angular-electron icon indicating copy to clipboard operation
angular-electron copied to clipboard

[Question] How to open new window using angular router?

Open okonon opened this issue 9 years ago • 44 comments

@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?

okonon avatar Sep 21 '17 16:09 okonon

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]
})

daxsom avatar Sep 22 '17 03:09 daxsom

@maximegris thanks a lot for your help. I was close! did not have a hash in load url line :)

okonon avatar Sep 23 '17 02:09 okonon

@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

tech-eng-dev avatar Feb 19 '18 18:02 tech-eng-dev

@maximegris

tech-eng-dev avatar Feb 19 '18 18:02 tech-eng-dev

@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?

abalad avatar Feb 19 '18 22:02 abalad

@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.

tech-eng-dev avatar Feb 20 '18 05:02 tech-eng-dev

Please help me @maximegris

tech-eng-dev avatar Feb 20 '18 05:02 tech-eng-dev

@tech-eng Can please provide a minimal repo where we can reproduce your issue?

seopei avatar Feb 20 '18 07:02 seopei

@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.

tech-eng-dev avatar Feb 20 '18 10:02 tech-eng-dev

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

seopei avatar Feb 20 '18 10:02 seopei

<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

tech-eng-dev avatar Feb 20 '18 10:02 tech-eng-dev

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

tech-eng-dev avatar Feb 20 '18 10:02 tech-eng-dev

add this <router-outlet></router-outlet> to your home.component.html and it should work.

seopei avatar Feb 20 '18 12:02 seopei

<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..

tech-eng-dev avatar Feb 20 '18 13:02 tech-eng-dev

Should I create this as an issue?

tech-eng-dev avatar Feb 20 '18 13:02 tech-eng-dev

@seopei Do you think there is nothing to do with the setting component?

tech-eng-dev avatar Feb 20 '18 13:02 tech-eng-dev

Please help me. This is really urgent issue for now. Thank you!

tech-eng-dev avatar Feb 20 '18 13:02 tech-eng-dev

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

}

seopei avatar Feb 20 '18 14:02 seopei

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.

tech-eng-dev avatar Feb 20 '18 14:02 tech-eng-dev

screen shot

tech-eng-dev avatar Feb 20 '18 14:02 tech-eng-dev