角度示意图条件属性/提示

raf*_*cik 8 javascript typescript angular angular-schematics

我正在努力创建自己的原理图。该原理图将负责使用一些代码创建一个组件(容器)。该组件的模板将包含一些其他组件。该组件之一是可选的横幅组件。该横幅将显示将被翻译成其他语言的文本,因此如果该横幅将包含在组件中,我还应该要求用户提供(默认)翻译文本。以下是该模板的示例:

name@dasherize .component.html.template:

<% if (includeBanner) { %>
<app-banner [title]="'<%= translationModuleKey %>.banner.title' | translate"
                           [description]="'<%= translationModuleKey %>.banner.description' | translate">
</app-banner>
<% } %>
<app-other-component>

</app-other-component>
Run Code Online (Sandbox Code Playgroud)

这是我的 schema.json:

{
  "$schema": "http://json-schema.org/schema",
  "id": "MySchematics",
  "title": "My schematics",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "The name of the container",
      "x-prompt": "Container name"
    },
    "includeBanner": {
      "type": "boolean",
      "description": "Include banner",
      "default": "true",
      "x-prompt": "Include banner"
    },
    "bannerTitle": {
      "type": "string",
      "description": "Banner title",
      "x-prompt": "Banner title"
    },
    "bannerDescription": {
      "type": "string",
      "description": "Banner description",
      "x-prompt": "Banner description"
    },
    "translationModuleKey": {
      "type": "string",
      "description": "Root key for translations"
    }
  },
  "required": [
    "name", "includeBanner", "bannerTitle", "bannerDescription"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当用户将 includeBanner 设置为 true 时,应需要字段 BannerTitle 和 BannerDescription,并且如果未提供这些属性,则应显示提示,但如果 includeBanner 为 false,则不应需要 BannerTitle 和 BannerDescription,并且不应有如果未提供这些属性,则不会显示填充这些属性的提示。知道如何实现这一目标吗?

Kir*_*rik 4

我也遇到了同样的问题。我发现 - 如果您需要条件提示,那么您不能依赖声明性 schema.json 文件(它不支持条件)。相反,您应该使用askConfirmation中的函数@angular/cli/utilities/prompt。所以你的例子可能看起来像这样:

import { askConfirmation } from '@angular/cli/utilities/prompt';

export function yourSchematicsFn(options: Schema): Rule {
    return async (tree: Tree, context: SchematicContext) => {
         const includeBanner = await askConfirmation('Include Banner?', true);
         if(includeBanner) {
              // ask for bannerTitle and bannerDescription
         }
         else {
              // do something else
         }
         return chain(/* chain your rules here */);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 Angular CLI 原理图源代码中发现了这一点ng-addaskConfirmation