正在生成

实际构建应用程序时,除了使用aem-clientlib-generator自动创建客户端库之外,还使用Webpack进行转换。 因此,构建命令将类似于:

"build": "ng build --build-optimizer=false && clientlib",

构建后,可以将包上传到AEM实例。

AEM 项目原型

任何 AEM 项目都应使用 AEM 项目原型,它支持使用 React 或 Angular 的 SPA 项目并利用 SPA SDK。

应用程序结构

如前所述,包括依赖项和构建应用程序将为您提供一个有效的SPA包,您可以将该包上传到您的AEM实例。

本文档的下一部分将介绍AEM中SPA的结构、驱动应用程序的重要文件以及它们如何协同工作。

以简化的图像组件为例,但应用程序的所有组件都基于相同的概念。

app.module.ts

此处显示的app.module.ts文件是SPA的入口点,该文件经过了简化,以重点关注重要内容。

// app.module.ts
import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { SpaAngularEditableComponentsModule } from '@adobe/aem-angular-editable-components';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  imports: [ BrowserModule.withServerTransition({ appId: 'my-angular-app' }),
    SpaAngularEditableComponentsModule,
    AppRoutingModule,
    BrowserTransferStateModule ],
  providers: ...,
  declarations: [ ... ],
  entryComponents: [ ... ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

app.module.ts文件是应用程序的起点,包含初始项目配置并使用AppComponent引导应用程序。

静态实例化

使用组件模板静态实例化组件时,必须将值从模型传递到组件的属性。 模型中的值作为属性进行传递,以便以后作为组件属性使用。

app.component.ts

在app.module.ts引导AppComponent后,它可以初始化应用程序,此处以简化版本显示,重点介绍重要内容。

// app.component.ts
import { Component } from '@angular/core';
import { ModelManager } from '@adobe/aem-spa-page-model-manager';
import { Constants } from "@adobe/aem-angular-editable-components";

@Component({
  selector: 'app-root',
  template: `
    <router-outlet></router-outlet>
  `
})

export class AppComponent {
  items;
  itemsOrder;
  path;

  constructor() {
    ModelManager.initialize().then(this.updateData.bind(this));
  }

  private updateData(model) {
    this.path = model[Constants.PATH_PROP];
    this.items = model[Constants.ITEMS_PROP];
    this.itemsOrder = model[Constants.ITEMS_ORDER_PROP];
  }
}

main-content.component.ts

通过处理页面,app.component.ts调用此处以简化版本列出的main-content.component.ts。

import { Component } from '@angular/core';
import { ModelManagerService }     from '../model-manager.service';
import { ActivatedRoute } from '@angular/router';
import { Constants } from "@adobe/aem-angular-editable-components";

@Component({
  selector: 'app-main',
  template: `
    <aem-page class="structure-page" [attr.data-cq-page-path]="path" [cqPath]="path" [cqItems]="items" [cqItemsOrder]="itemsOrder" ></aem-page>
  `
})

export class MainContentComponent {
  items;
  itemsOrder;
  path;

  constructor( private route: ActivatedRoute,
    private modelManagerService: ModelManagerService) {
    this.modelManagerService.getData({ path: this.route.snapshot.data.path }).then((data) => {
      this.path = data[Constants.PATH_PROP];
      this.items = data[Constants.ITEMS_PROP];
      this.itemsOrder = data[Constants.ITEMS_ORDER_PROP];
    });
  }
}

MainComponent摄取页面模型的JSON表示形式,并处理内容以包装/装饰页面的每个元素。 有关Page的更多详细信息可在文档SPA Blueprint中找到。

image.component.ts

Page由组件组成。 摄取JSON后,Page可以处理这些组件,如image.component.ts,如下所示。

/// image.component.ts
import { Component, Input } from '@angular/core';

const ImageEditConfig = {

    emptyLabel: 'Image',

    isEmpty: function(cqModel) {
        return !cqModel || !cqModel.src || cqModel.src.trim().length < 1;
    }
};

@Component({
  selector: 'app-image',
  templateUrl: './image.component.html',
})

export class ImageComponent {
  @Input() src: string;
  @Input() alt: string;
  @Input() title: string;
}

MapTo('my-angular-app/components/image')(ImageComponent, ImageEditConfig);

AEM中SPA的核心想法是,将SPA组件映射到AEM组件,并在修改内容时更新组件(反之亦然)。 有关此通信模型的摘要,请参阅文档SPA编辑器概述。

MapTo('my-angular-app/components/image')(Image, ImageEditConfig);

MapTo方法将该SPA组件映射到AEM组件。 它支持使用单个字符串或字符串数组。

ImageEditConfig是一个配置对象,它通过为编辑器提供生成占位符所需的元数据来帮助启用组件的创作功能

如果没有内容,则会提供标签作为占位符来表示空内容。

动态传递的属性

来自模型的数据将作为组件的属性动态传递。

image.component.html

最后,可以在image.component.html中呈现图像。

// image.component.html
<img [src]="src" [alt]="alt" [title]="title"/>

在SPA组件之间共享信息

单页应用程序中的组件经常需要共享信息。 有几种推荐的方法可以做到这一点,按复杂性递增的顺序如下所示。

  • 选项1:​将逻辑集中并广播到必需的组件,例如,通过使用util类作为纯面向对象解决方案。
  • 选项2:​使用状态库(如NgRx)共享组件状态。
  • 选项3:​通过自定义和扩展容器组件来利用对象层次结构。

后续步骤

已自动翻译
使用语言选择器查看此页面的英文版本。
翻译有帮助吗?

Ask AI

Not sure where to start?
Ask me anything about Adobe products.

Type your question or pick a suggestion below.

Use of this AI chatbot is subject to Adobe's Privacy Policy. Don't share sensitive data. AI responses are not your Content, may be inaccurate, and any offers provided are non-binding. Generative AI Terms.