javascript - Angular 示意图条件属性/提示

标签 javascript angular typescript 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>

Here is my 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 文件(它不支持条件)。
相反,您应该使用 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 */);
    }
}
我在 Angular CLI ng-add 中发现了这一点原理图源代码:askConfirmation .

关于javascript - Angular 示意图条件属性/提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56669902/

相关文章:

javascript - 输入字段,强制格式 00 :00. 00

angular - 在 Angular 中构建后无法加载路由模块

angular - 将 angular-universal 部署到 apache 服务器(Angular 9)

javascript - 如何将动态创建的表单字段绑定(bind)到 Angular 2 中的对象?

typescript - 使用 Typescript 初始化 DataLoader 时出现类型错误

reactjs - 在 typescript 中使用 defaultProps 的 forwardRef

javascript - 使用 html 选择选项分配 javascript 变量

Javascript:将对象分配给 const 变量与 let 变量

javascript - jQuery 自动完成 : How to separate the url of the javascript?

typescript - 未定义的数字(从字符串转换)通过空检查