Giter VIP home page Giter VIP logo

Comments (44)

daxsom avatar daxsom commented on May 10, 2024 24

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

from angular-electron.

polofgrs avatar polofgrs commented on May 10, 2024 6

@JohnOfficial or anyone who might have issues with the # being converted into %23, try the following: (I was using url.format to avoid path conflicts when using ng build):

var urlToLoad = url.format({
        pathname: path.join(__dirname, `/path/to/index.html`),
        protocol: 'file:',
        slashes: true,
      });
win.loadURL(urlToLoad + '#/settings');

from angular-electron.

neeraaj1502 avatar neeraaj1502 commented on May 10, 2024 3

hi, @shalomdotnet can you please upload the full solution of this implementation, I am also struggling to implement it..

from angular-electron.

abalad avatar abalad commented on May 10, 2024 2

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

from angular-electron.

shijinsmite avatar shijinsmite commented on May 10, 2024 2

how do i pass route parameters, for example win.loadURL('file://' + __dirname + '/index.html#/settings/id');

how do i pass the id from angular

from angular-electron.

Supamiu avatar Supamiu commented on May 10, 2024 1

Hi, I implemented @tech-eng 's solution, turns out it doesn't work with aot build inside electron.

More details on https://stackoverflow.com/questions/50418690/deep-linking-on-a-new-window-using-file-with-aot

from angular-electron.

okonon avatar okonon commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

@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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

@maximegris

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Please help me @maximegris

from angular-electron.

seopei avatar seopei commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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

from angular-electron.

seopei avatar seopei commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024
<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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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

from angular-electron.

seopei avatar seopei commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024
<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..

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Should I create this as an issue?

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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

from angular-electron.

seopei avatar seopei commented on May 10, 2024

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

}

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

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.

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

screen shot

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

This is the screenshot to show when I call loadURL function.

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

As you can see, the content of the new window is not setting page and it is just home page

from angular-electron.

shalomdotnet avatar shalomdotnet commented on May 10, 2024

Do you have any error in the console?
(Add win.webContents.openDevTools(); to open it).

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

screen shot1

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

No error. Exactly same console with the home page

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Could you please call loadURL function inside the home.component.ts file to open new window with setting router? And then make dmg file to send me?
I want to confirm if we are the same page right now.
Thank you very much!

from angular-electron.

shalomdotnet avatar shalomdotnet commented on May 10, 2024

Type location.href in the console (of the new window). What is the result?

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

.../angular-electron-master/dist/index.html#/home

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Above is the console result of the new window(The opened new window by win.loadURL('file://' + __dirname + '/index.html#/setting');)

from angular-electron.

shalomdotnet avatar shalomdotnet commented on May 10, 2024

See this fork: https://github.com/shalomdotnet/angular-electron.
It work for me.

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Hi, @shalomdotnet
It works here. I've just found the reason. This is my app.components.ts:

...
export class AppComponent {
  constructor(public electronService: ElectronService,
              private translate: TranslateService,
              public router: Router) {

    translate.setDefaultLang('en');

    if (electronService.isElectron()) {
      console.log('Mode electron');
      // Check if electron is correctly injected (see externals in webpack.config.js)
      console.log('c', electronService.ipcRenderer);
      // Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
      console.log('c', electronService.childProcess);

      if( !window.localStorage.getItem('endpoints') ) {
        this.router.navigate(['setting']);
      } else {
        this.router.navigate(['home']);
      }
    } else {
      console.log('Mode web');
    }
  }
}

When I comment this part:

if( !window.localStorage.getItem('endpoints') ) {
     this.router.navigate(['setting']);
 } else {
     this.router.navigate(['home']);
 }

It works.

But I should check if which page should be opened first time app opens.
Do you have any idea how can I figure it out?

Thank you

from angular-electron.

shalomdotnet avatar shalomdotnet commented on May 10, 2024

I think you need use guard like CanActivate on home-component

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Hi, @shalomdotnet
Thanks. I will check CanActivate

Thank you so much for your great help!!!

from angular-electron.

tech-eng-dev avatar tech-eng-dev commented on May 10, 2024

Hi, @shalomdotnet
I have another question.
How can I get mainwindow instance inside router component pages?

from angular-electron.

cjackson234 avatar cjackson234 commented on May 10, 2024

Can you console log __dirname and let us know what folder it's in?

from angular-electron.

Supamiu avatar Supamiu commented on May 10, 2024

__dirname is equal to root folder of the project, parent folder of dist/

from angular-electron.

seranus avatar seranus commented on May 10, 2024

Hey, I'm getting the same issue, mostly a clean fork. I checked everything the only thing different is when I type location.href I get "data:text/html,chromewebdata" instead of the path

There is an error in new window console, security is disabled Not allowed to load local resource: file:///D:/angular-electron/node_modules/electron/dist/resources/electron.asar/renderer/index.html

from angular-electron.

Otero09 avatar Otero09 commented on May 10, 2024

I have the same problem I am new in the subject I would like to know if there is any solution to this I keep getting this message "data: text / html, chromewebdata"

from angular-electron.

jovansavic avatar jovansavic commented on May 10, 2024

Hey @daxsom, this works great on dev for me! I have an error after building.
"Not allowed to load local resource: ..."
It converts '#' from URL into '%23'.
So instead to load __dirname + '/index.html#/settings', it's trying to load __dirname + '/index.html%23/settings';

Any idea how can I fix that? Thanks!

from angular-electron.

DanielFrugoni-dev avatar DanielFrugoni-dev commented on May 10, 2024

Im trying this idea, but dosent work, only get the home page.. (index.html)

from angular-electron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.