【问题标题】:ngOnInit not firing when opening dialog打开对话框时 ngOnInit 未触发
【发布时间】:2019-10-21 13:29:30
【问题描述】:

打开对话框时,ngOnInit 没有被调用。我可以让它触发的唯一方法是调整窗口大小。我尝试使用detectChangeszone.run(),但没有任何改变。

对话框组件的代码如下:

(settings-dialog.component.ts)

import { Component, OnInit } from '@angular/core';
import { ElectronService } from '../../providers/electron.service';

@Component({
  selector: 'app-settings-dialog',
  templateUrl: './settings-dialog.component.html',
  styleUrls: ['./settings-dialog.component.scss']
})
export class SettingsDialogComponent implements OnInit {
  public settings;

  constructor(
    private electronService: ElectronService,
  ) { }

  ngOnInit(): void {
    console.log('Getting settings');
    this.getSettings();
  }

  private getSettings() {
    this.settings = this.electronService.settings.getAll();
    console.log(this.settings);
  }
}
(settings-dialog.module.ts)
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { SettingsDialogComponent } from './settings-dialog.component';

@NgModule({
  imports: [ CommonModule ],
  providers: [ ],
  declarations: [ SettingsDialogComponent ],
  entryComponents: [ SettingsDialogComponent ]
})
export class SettingsDialogModule { }

正在使用 Electron 从菜单栏设置加载组件:

(app.component.ts)

import { Component } from '@angular/core';
import { MatDialog } from '@angular/material';
import { TranslateService } from '@ngx-translate/core';

import { SettingsDialogComponent } from './dialogs/settings-dialog/settings-dialog.component';
import { ElectronService } from './providers/electron.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  constructor(public electronService: ElectronService, public translate: TranslateService, private dialog: MatDialog) {
    this.setupMenu();
  }

  private setupMenu() {
    const menu = this.electronService.remote.Menu.buildFromTemplate([{
      label: 'File',
      submenu: [{
        label: 'Settings',
        click: () => this.openSettingsDialog()
      }]
    }]);

    this.electronService.remote.Menu.setApplicationMenu(menu);
  }

  private openSettingsDialog() {
    this.dialog.open(SettingsDialogComponent);
  }
}

(app.module.ts)
import '../polyfills';
import 'reflect-metadata';

import { HttpClient, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatDialogModule } from '@angular/material';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { WorkspaceModule } from './components/workspace/workspace.module';
import { SettingsDialogModule } from './dialogs/settings-dialog/settings-dialog.module';
import { WebviewDirective } from './directives/webview.directive';
import { ElectronService } from './providers/electron.service';

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
  declarations: [
    AppComponent,
    WebviewDirective
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    AppRoutingModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (HttpLoaderFactory),
        deps: [HttpClient]
      }
    }),
    WorkspaceModule,
    MatDialogModule,
    SettingsDialogModule
  ],
  providers: [ElectronService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我错过了什么?我做错了什么? (我可能不会再尝试了)

谢谢!

【问题讨论】:

  • 您发布的代码应该可以正常工作。你介意创建一个堆栈闪电战以最小化复制吗?
  • @SachinGupta 这是一个电子项目,从“文件”下拉菜单栏中的菜单栏打开对话框。我从未使用过 stackblitz,但我认为它不支持 Electron 应用程序,并且无法重现应用程序的菜单栏。如果需要,这里是 repo URL:gitlab.com/thebird956/open-3d-asset-library

标签: angular angular-material electron


【解决方案1】:

我尝试使用 detectChanges 和 zone.run() 但它没有改变 任何东西

您的方法是正确的,但可能没有正确使用它。当您点击电子应用程序中的菜单时,您会离开 ngZone。所以下面的代码应该可以工作,并且应该触发 ngOnInit,因为我已经检查过:

app.component.ts

import { NgZone } from '@angular/core';
...
export class AppComponent {
  constructor(...private zone: NgZone) {}


  private openSettingsDialog() {
    this.zone.run(() => {
      this.dialog.open(SettingsDialogComponent);
    });
  }

【讨论】:

  • 这个答案拯救了我的一天! :) 干杯。
  • 也适用于 Angular 元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
相关资源
最近更新 更多