Discuss / JavaScript / 为什么我的console.log('${ctx.request.method} ${ctx.request.url}'); 输出结果没有代入表达式呢?

为什么我的console.log('${ctx.request.method} ${ctx.request.url}'); 输出结果没有代入表达式呢?

Topic source

左手天才T

#1 Created at ... [Delete] [Delete and Lock User]

app.use(async(ctx,next)=>{ console.log('${ctx.request.method} ${ctx.request.url}'); await next(); ctx.response.type='text/html'; ctx.response.body='<h1>Hello,koa2!</h1>'; });

我的这个输出还是: Debugging with inspector protocol because Node.js v8.4.0 was detected. node --inspect=4677 --debug-brk app.js Debugger listening on ws://127.0.0.1:4677/0404ce71-c5e0-4cc8-b155-0be62e0aad9e app started at port 3000... ${ctx.request.method} ${ctx.request.url} ${ctx.request.method} ${ctx.request.url}

表达式直接就显示出来了,感觉就是个字符不是表达式啊

无尘使

#2 Created at ... [Delete] [Delete and Lock User]

你使用的引号错了吧,``是英文状态下的波浪符号而非引号

一正锅

#3 Created at ... [Delete] [Delete and Lock User]

不是用两个单引号''包围,而是用键盘左上角esc下面的那个来包围,是es6的语法,如下: ES6中的字符串模板 两个反单引号 包裹的字符串就是字符串模板。在字符串模板中可以使用${}符号来引用变量 你也可以直接复制老师的代码运行就知道了


  • 1

Reply