TypeScript 中的控制台输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33858763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Console input in TypeScript
提问by WaterlessStraw
How do I take in a console input from a user in TypeScript?
如何在 TypeScript 中接收来自用户的控制台输入?
For example, in Python I would use:
例如,在 Python 中我会使用:
userInput = input("Enter name: ")
What is the equivalent in TypeScript?
TypeScript 中的等价物是什么?
回答by Elie G.
You can use the readline
node module. See readlinein node documentation.
您可以使用readline
节点模块。请参阅节点文档中的readline。
To import readline in TypeScript use the asterisk(*
) character.
For example:
要在 TypeScript 中导入 readline,请使用星号 ( *
) 字符。例如:
import * as readline from 'readline';
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Is this example useful? [y/n] ', (answer) => {
switch(answer.toLowerCase()) {
case 'y':
console.log('Super!');
break;
case 'n':
console.log('Sorry! :(');
break;
default:
console.log('Invalid answer!');
}
rl.close();
});
回答by Marius Schulz
TypeScript only adds optional static typing and transpilation capabilities to JavaScript. It's a purely compile-time artifact; at runtime, there is no TypeScript, which is why this question is about JavaScript, not TypeScript.
TypeScript 仅向 JavaScript 添加了可选的静态类型和转换功能。它是一个纯粹的编译时工件;在运行时,没有 TypeScript,这就是为什么这个问题是关于 JavaScript,而不是 TypeScript。
If you're talking about accepting input from the console, you're probably talking about a node.jsapplication. In Reading value from console, interactively, the solution is to use stdin:
如果您在谈论从控制台接受输入,那么您可能在谈论一个node.js应用程序。在从控制台以交互方式读取值中,解决方案是使用标准输入:
var stdin = process.openStdin();
stdin.addListener("data", function(d) {
// note: d is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
// with toString() and then substring()
console.log("you entered: [" + d.toString().trim() + "]");
});
回答by Fenton
In the browser, you would use a prompt:
在浏览器中,您将使用提示:
var userInput = prompt('Please enter your name.');
On Node you can use Readline:
在节点上,您可以使用Readline:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of Node.js? ", function(answer) {
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
回答by Jeyanth
It actually depends on which HTML element you use as an input element. Normally, you can read the input by using prompt()
with the help of window
object. On clicking OK, it returns the value what user enters, returns null
on clicking Cancel.
它实际上取决于您使用哪个 HTML 元素作为输入元素。通常,您可以在objectprompt()
的帮助下 使用 using 读取输入window
。单击确定时,它返回用户输入的值,null
单击取消时返回。
class Greeter {
greet() {
alert("Hello "+this.getName())
}
getName() {
return prompt("Hello !! Can I know your name..??" );;
}
}
let greeter = new Greeter();
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
(greeter.greet());
}
document.body.appendChild(button);