我试着在TypeScript中使用火花,但是当我写这个
Spark.get("/facture", (req, res) => {
chalk.red('Hello test');
chalk.green('Hello word');
})
它返回未定义的我,但当我只写一行时,它就工作了。
Spark.get("/facture", (req, res) =>
chalk.green('Hello word');
)
我认为问题来自于语法。有人能帮我吗?
发布于 2017-06-02 09:57:09
使用箭头函数时,如果它们是一个行,则可以省略{},表达式返回的值将是函数的返回值。
实质上:
Spark.get("/facture", (req, res) =>
chalk.green('Hello word');
)
转到:
Spark.get("/facture", function (req, res) {
return chalk.green('Hello word');
});
但是,当您有多个语句并为箭头函数创建一个主体时,您必须像在正常函数中一样手动返回值。
你可以很容易地看到它时,转移。
Spark.get("/facture", (req, res) => {
chalk.red('Hello test');
chalk.green('Hello word');
})
转到:
Spark.get("/facture", function (req, res) {
chalk.red('Hello test');
chalk.green('Hello word');
});
如果您想返回某项内容,则必须编写返回语句:
Spark.get("/facture", (req, res) => {
chalk.red('Hello test');
return chalk.green('Hello word');
})
所以在javascript中,结果是这样的:
Spark.get("/facture", function (req, res) {
chalk.red('Hello test');
return chalk.green('Hello word');
});
您可以在操场 chalk.green('Hello word'); ) Spark.get("/facture", (req, res) => { chalk.red('Hello test'); chalk.green('Hello word'); }) Spark.get("/facture", (req, res) => { chalk.red('Hello test'); return chalk.green('Hello word'); })">这里中看到示例,并在MDN页面上为它们学习更多关于箭头函数的这里。
https://stackoverflow.com/questions/44325701
复制相似问题