繁体   English   中英

Angular 原理图条件属性/提示

[英]Angular schematics conditional property/prompt

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

名称@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>

这是我的 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"
  ]
}

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

我正在努力解决同样的问题。 我发现 - 如果您需要条件提示,那么您不能依赖声明性 schema.json 文件(它不支持条件)。 相反,您应该使用@angular/cli/utilities/promptaskConfirmation函数。 所以你的例子可能是这样的:

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 */);
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM