【问题标题】:Angular Schematics Prompt for array typeAngular Schematics 提示数组类型
【发布时间】:2019-09-08 03:29:52
【问题描述】:

在设置我的原理图时,我发现我可以提示输入以下任何类型 String |布尔值 |数组 |号码 |整数 |空 |对象。

我正在尝试设置一个示意图,提示用户从可用模块列表中选择一个模块。因此,例如,用户会看到如下内容:

What module are you adding this store to?
 > Foo
   Bar
   Baz

虽然有大量字符串和布尔提示的示例,但没有人提供我为数组提示找到的示例。对于我的生活,我无法弄清楚如何在数组中提供选项以提示用户从中进行选择,这根本不会出现在他们的文档中。

{
  "$schema": "http://json-schema.org/schema",
  "id": "SchematicsIDXStoreGen",
  "title": "IDX Store Gen Schema",
  "type": "object",
  "properties": {
    "featureName": {
      "type": "string",
      "description": "The name of the store",
      "x-prompt": "What is the name of the store you'd like to create"
    },
    "module": {
      "type": "array",
      "description": "Select the appropriate module",
      "x-prompt": "What module are you adding this store to?" // I want to provide a list of available modules here.
    }
  },
  "required": ["featureName", "module"]
}

【问题讨论】:

    标签: angular angular-schematics


    【解决方案1】:

    您也可以尝试以下方法,也可以选择适合您的选项。

    "modules": {
          "type": "array",
          "description": "description",
          "uniqueItems": true,
          "items": {
            "type": "string"
          },
          "x-prompt": {
            "message": "Which module would you like to select?",
            "type": "list",
            "multiselect": true,
            "items": [
              "firstOption",
              "secondOption",
              "thirdOption"
            ]
          }
        }

    【讨论】:

      【解决方案2】:
      ng new myProject
      

      将提示您设置样式,这是您可以从中选择的列表。

      如果你查看@angular/cli 项目内部(\packages\schemics\angular\ng-new), 你可以看看他们是怎么做到的:

      "style": {
        "description": "The file extension or preprocessor to use for style files.",
        "type": "string",
        "default": "css",
        "enum": [
          "css",
          "scss",
          "sass",
          "less",
          "styl"
        ],
        "x-prompt": {
          "message": "Which stylesheet format would you like to use?",
          "type": "list",
          "items": [
            { "value": "css",  "label": "CSS" },
            { "value": "scss", "label": "SCSS   [ http://sass-lang.com/documentation/file.SASS_REFERENCE.html#syntax ]" },
            { "value": "sass", "label": "Sass   [ http://sass-lang.com/documentation/file.INDENTED_SYNTAX.html       ]" },
            { "value": "less", "label": "Less   [ http://lesscss.org                                                 ]" },
            { "value": "styl", "label": "Stylus [ http://stylus-lang.com                                             ]" }
          ]
        },
      

      【讨论】:

        【解决方案3】:

        不幸的是,这些答案都没有完全解决这个问题。是的,您可以使用枚举和 x-prompt 属性中的多个项目显示选项列表,如上面的答案所示,但它是 JSON 文件,它必须是硬编码的值列表。

        这意味着您将无法在列表中显示当前存在的模块。至少现在不是 (v9)

        【讨论】:

        • 如果不同意答案的人至少会评论他们建议的答案,而不是仅仅投反对票并离开,那就太好了
        猜你喜欢
        • 2021-11-10
        • 2015-11-10
        • 2019-06-21
        • 2021-06-13
        • 2015-05-23
        • 2021-02-17
        • 2019-09-05
        • 2014-08-08
        • 2017-09-09
        相关资源
        最近更新 更多