V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
spaceprogram
V2EX  ›  JavaScript

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

  •  
  •   spaceprogram · 2018-04-25 14:25:22 +08:00 · 2671 次点击
    这是一个创建于 2186 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 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),字符串没有正确拼接。请问大佬们这是什么原因

    2 条回复
    spaceprogram
        1
    spaceprogram  
    OP
       2018-04-25 15:05:06 +08:00
    卧槽,搞定了,原来是要用`而不是'
    jessie0717
        2
    jessie0717  
       2018-04-26 09:50:48 +08:00
    就你这个排版,我回这一句话已经相当过分了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5253 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:28 · PVG 16:28 · LAX 01:28 · JFK 04:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.