【问题标题】:Angular 5 with multiple modules and Sharepoint SPFX具有多个模块和 Sharepoint SPFX 的 Angular 5
【发布时间】:2019-09-22 05:13:37
【问题描述】:

我正在使用 Angular 5 在 SPFX 解决方案中运行应用程序,但我的应用程序不会触发一些基本的角度事件,例如 onInit 甚至 @Input 集都不起作用。

唯一正常工作的模块和组件是启动 app.component 和 app.module 中的模块和组件。对于所有其他模块,仅触发构造函数,但不会触发所有其他 Angular 生命周期事件。

这是我的 app.webpart.ts:

export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloAngularWebPartProps> {
  constructor() {
    super();
  }
  ngElement: HTMLElement;

  public render(): void {
    window['webPartContext'] = this.context;

    this.domElement.innerHTML = `<app-root [isOnline]='false'></app-root>`;

    platformBrowserDynamic().bootstrapModuleFactory(
      AppModuleNgFactory, { ngZone: 'noop' })
      .catch(
        err => console.log('error', err)
      );
  }

还有我的 app.module

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    CoreModule,
    TrackersModule,
    LocalStorageModule.withConfig({
      prefix: 'my-app',
      storageType: 'localStorage',
    }),
    RouterModule.forRoot(ROUTES, { useHash: true, preloadingStrategy: PreloadAllModules })
  ],
  providers: [
    BackendRequestClass,
    {
      provide: APP_INITIALIZER, useFactory: (config: BackendRequestClass) => () => config.load(),
      deps: [BackendRequestClass], multi: true
    },
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor() { }
}  

我猜这个问题与如何从 webpart 启动应用程序有关,所以我尝试使用 boostrapModule 而不是 bootstrapModuleFactory 启动应用程序。但我什至不确定这是从 app.module 加载所有模块的正确方法。

platformBrowserDynamic().bootstrapModule(AppModule, {
    ngZone: 'noop'
  }).catch(err => console.log(err));

但是当我运行它时,我得到了这个错误:

承诺拒绝:找不到“AppModule”的 NgModule 元数据。 ;区域:;任务:Promise.then;值:错误:找不到“AppModule”的 NgModule 元数据。

【问题讨论】:

    标签: angular typescript sharepoint spfx


    【解决方案1】:

    为了确保它不是 SharePoint 的本机 JS 干扰您的 Angular 应用程序,请在内容编辑器中运行您的 Angular 应用程序。您可以将 Angular 构建设置为具有保存构建文件的资产文件夹的部署 URL。然后将内容编辑器指向 index.html 文件位置。

    如果应用程序在从内容编辑器中运行时显示类似问题,最好检查存在的 polyfill 以确保没有与 SharePoint 原生 JS 混淆。我知道 core-js/es6/string polyfills 在 SharePoint 中表现不佳。

    第三个建议是在使用 Angular 时使用内容编辑器而不是 SPFx。 SPFx 框架还没有。

    请参阅 SPJeffs 博客文章了解所有细节。 https://www.spjeff.com/2017/05/31/angular-cli-todo-list-in-sharepoint-content-editor/

    【讨论】:

    • 感谢您的回复,错误来自 SPFX 未正确集成 Angular 解决方案。我们使用了 Angular Elements 方法,但是一旦我们尝试向 Angular Elements 添加多个组件,我们就会面临一些其他问题,因为它似乎不是为这样使用而设计的......
    • 我对 Angular Elements 没有特别的经验,但如果您能够找出解决问题的方法,我很想听听您是如何解决的。祝你好运。
    • 感谢 Andrew 的文章,我找到了一种使用 Angular Elements 使其工作的方法:andrewconnell.com/blog/… 仍然面临一些问题,但我设法将一个测试组件从我的主应用程序部署到 SPFX,如果您有兴趣我正在将我的 WIP 放入一个 git repo。
    • 我很感兴趣。请尽可能链接它。
    • github.com/clementlepere/SPFX-AngularElements 我仍在努力显示一些数据,目前代码非常难看,因为它是 POC 必须从 cmd 使用命令行而不是使用 npm run 的工作(我不知道为什么),如果您在运行 gulp serve 命令时遇到一些解析问题,则必须更改 ngElementWorld.js 中的编码文件。
    猜你喜欢
    • 2018-09-14
    • 2018-05-12
    • 2019-01-09
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多