javascript - 这个使用 Vue2 JS 的基本计算数据值有什么问题?

标签 javascript vue.js vuejs2

我遵循了一个使用计算数据输出名字和姓氏的在线教程。我的代码是一样的,但是渲染出来的结果不一样。目前,它看到 $ 之后的内容只是一个字符串,而不是分配的数据。 https://screenshots.firefox.com/pDElTgV9EB58BjbS/127.0.0.1我做错了什么?

const app = new Vue({
    el: "#app",
    data: {
        bobby: {
            first: "Bobby",
            last: "Boone",
            age: 25
        },
        john: {
            first: "John",
            last: "Boone",
            age: 35,
        }
    },
    computed: {
        bobbyFullName() {
            return '${this.bobby.first} ${this.bobby.last}'
        },
        johnFullName() {
            return '${this.john.first} ${this.john.last}'
        }
    },
    template: `
    <div>
        <h1>Name: {{bobbyFullName}}</h1>
        <p>Age {{bobby.age}}</p>

        <h1>Name: {{johnFullName}}</h1>
        <p>Age {{john.age}}</p>

    </div>
    `
}) 

最佳答案

JS template literal使用反引号而不是单引号。

computed: {
    bobbyFullName() {
        return `${this.bobby.first} ${this.bobby.last}`;
    },
    johnFullName() {
        return `${this.john.first} ${this.john.last}`;
    }
}

关于javascript - 这个使用 Vue2 JS 的基本计算数据值有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167383/

相关文章:

javascript - 使用 Ctrl 按钮按下 keydown

javascript - Props 未分配给 Vue 中的 data() 属性

javascript - 仅当 json 响应为 true 时才启用下一个按钮(转到另一个选项卡)?

javascript - 有没有办法捕获vuejs中多个子组件发出的事件?

javascript - Vue : axios executing synchronously instead of asynchronously on multiple components

javascript - 如何在具有类的另一个特定列表项之前添加新列表项?

javascript - 使用 PHP + JavaScript 循环

javascript - 在多个 Highcharts 树形图中选择点

vue.js - 使用 Vuejs 绑定(bind)颜色

javascript - Vue 对象属性不随 v-bind :class 改变