为什么字符串使用${变量}无法正确读取

2018-04-25 14:25:22 +08:00
 spaceprogram

使用 WebStorm 工具写 Vue,一个项目代码 authorName(val) { const _this = this; if (this.authorName) { this.$http({ url: https://xxxx.org/api/v1/user/${val}, method: 'get' }) .then((res) => { console.log(res); _this.userInfo = res.data.data; _this.loading = false; }) .catch((res) => { console.log('SideSec.vue :', res); }); } } url: https://xxxx.org/api/v1/user/${val},能正确将 val 拼接起来。 另一个项目 created() { console.log(this.$route.path); const _url = 'https://xxxx.org/api/v1${this.$route.path}; this.$http({ url: _url, method: "get" }) 则控制台报错 https://xxxx.org/api/v1$%7Bthis.$route.path%7D 404 (Not Found),字符串没有正确拼接。请问大佬们这是什么原因

2717 次点击
所在节点    JavaScript
2 条回复
spaceprogram
2018-04-25 15:05:06 +08:00
卧槽,搞定了,原来是要用`而不是'
jessie0717
2018-04-26 09:50:48 +08:00
就你这个排版,我回这一句话已经相当过分了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/449732

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX