angular - 如何在内容脚本中引导 Angular 应用程序
问题描述
我正在尝试在内容脚本中引导 Angular 应用程序,这样只要 div 可见,就应该引导 Angular 应用程序。
我找到了一种手动引导 Angular 应用程序的方法,代码如下。
问题是我将通过manifest
文件在内容脚本中加载一次角度包。但是,特定的 div 可以一次又一次地显示和销毁。如何与这个事实调和?
应用模块
@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));
解决方案
推荐阅读
- npm - VS Code 终端无法使用来自 nvm 的 npm 版本
- python - 为什么不应该在 python 中动态生成变量名?
- acumatica - 更改默认字段的标签名称
- python - 为什么 tkinter 不更新完成百分比?
- gem5 - 什么是 gem5 注释 mops 魔术说明以及如何使用它们?
- c# - 如何在 AWS 中为 cron 作业部署和运行窗口服务?
- tensorflow - Tensorflow:Model_Average_optimizer 在为关键字参数“dtype”获取多个值时使用问题
- javascript - 为什么 JavaScript 中 parseInt("10e+1") 和 parseFloat("10e+1") 的输出不同?
- yaml - Hugo 降价元数据
- r - R Studio Github md 文件没有图片