9

所以我想要做的是在进一步提出问题时使用先前的答案。基本上这样我就可以显示将要创建的内容的摘要并要求验证。

this.prompt([   
    {
      type: 'input',
      name: 'name',
      message: 'What is your name?'
      default: 'Jake'
    },
    {
      type: 'confirm',
      name: 'summary',
      message: 'Is this information correct? Your name is:' + answers.name',
    }

有没有简单的方法来实现这一点?或者另一种实现列出所有先前答案的摘要类型的方法?

4

4 回答 4

5

就我而言,丹尼尔的回答不适用于询问者 7。一种解决方法可能是将大提示分成几个,并使用匿名async函数将它们包装起来。这将永远是安全的。

const inquirer = require("inquirer");

(async () => {
  const ans1 = await inquirer.prompt([
    {
      type: "input",
      name: "name",
      message: "What is your name?",
      default: "Jake",
    },
  ]);
  const ans2 = await inquirer.prompt([
    {
      type: "confirm",
      name: "summary",
      message: "Is this information correct? Your name is:" + ans1.name,
    },
  ]);
  return { ...ans1, ...ans2 };
})()
  .then(console.log)
  .catch(console.error);

这将记录:

{ name: 'Foo bar', summary: true }
于 2020-04-27T00:36:53.643 回答
4

嵌套查询器调用:

inquirer
  .prompt({
    type: 'list',
    name: 'chocolate',
    message: "What's your favorite chocolate?",
    choices: ['Mars', 'Oh Henry', 'Hershey']
  })
  .then(() => {
    inquirer.prompt({
      type: 'list',
      name: 'beverage',
      message: 'And your favorite beverage?',
      choices: ['Pepsi', 'Coke', '7up', 'Mountain Dew', 'Red Bull']
    });
  });

或使用该when功能。

{
  type: 'confirm',
  name: 'summary',
  message: 'Is this information correct? Your name is:' + answers.name,
  when: function( answers ) {
    // Only run if user set a name
    return !!answers.name;
  },
}
于 2018-07-29T22:55:44.557 回答
0

我参加聚会有点晚了,但是在寻找解决方案时遇到了这个问题。完整地说,在版本 7 中,可以将一个函数传递给 message 属性,得到的答案如下:

inquirer
  .prompt([
    {
      type: "input",
      name: "name",
      message: "What is your name?",
    },
    {
      type: "list",
      name: "food",
      message: (answers) => `What would you like to eat ${answers.name}?`,
      choices: ["Hotdogs", "Pizza"],
    },
  ])
  .then((answers) =>
    console.log(`Enjoy your ${answers.food}, ${answers.name}!`)
  );
于 2021-01-21T20:14:52.277 回答
0

const run = async () => {
  try {
    const ans1 = await inquirer.prompt([
      {},
    ]);

    const ans2 = await inquirer.prompt([
      {},
    ]);

    return { ...ans1, ...ans2 };

    inquirer.prompt([]);

  } catch (err) {
    if (err) {
      switch (err.status) {
        case 401:
          console.log('401');
          break;
        default:
          console.log(err);
      }
    }
  }
};

run();

于 2020-09-16T18:58:09.517 回答