Как загрузить угловое приложение в content-script - PullRequest
1 голос
/ 25 июня 2019

Я пытаюсь загрузить приложение Angular в контент-скрипте так, чтобы всякий раз, когда div был видим, приложение Angular должно быть затем загружено.

Я нашел способ вручную загрузить приложение Angular, код ниже.

Проблема в том, что я загружаю угловой пакет один раз в скрипт контента через файл manifest.Тем не менее, конкретный div может отображаться и уничтожаться снова и снова.Как смириться с этим фактом?

AppModule

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  entryComponents: [AppComponent]
  // bootstrap: [AppComponent]
})
export class AppModule { 

  ngDoBootstrap(app: ApplicationRef) { 
    // obtain reference to the DOM element that shows status
    // and change the status to `Loaded`
    const dynamicComponentElement = document.querySelector('#sidebar');
    dynamicComponentElement.textContent = 'Loaded';
    // create DOM element for the component being bootstrapped
    // and add it to the DOM
    const componentElement = document.createElement('app-root');
    document.body.appendChild(componentElement);
    // bootstrap the application with the component
    app.bootstrap(AppComponent);
  }

}

В файле main.ts

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
  // Ensure Angular destroys itself on hot reloads.
  if (window['ngRef']) {
    window['ngRef'].destroy();
  }
  window['ngRef'] = ref;
  // Otherise, log the boot error
}).catch(err => console.error(err));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...