javascript - 反引号中返回的 vue.js 计算方法未在模板中解析

标签 javascript vue.js static

我一直在关注这个 vue.js 教程:

https://youtu.be/h6lhOYv-QM4?t=1m56s

但是很早就遇到了一个奇怪的问题。我正在使用带有标准 index.html 文件和 app.js 文件的静态设置。

这是 JS:

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>

        <h3>Name:  {{ bobbyFullName }}</h3>
        <h3>Age:   {{ bobby.age }}</h3>

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

    </div>
    `
});

预期输出:

Name: Bobby Boone
Age: 25
Name: John Boone
Age: 30

实际输出:

Name: $(this.bobby.first) $(this.bobby.last)
Age: 25
Name: $(this.john.first) $(this.john.last)
Age: 30

想法

可能是因为他们在教程中使用的是本地服务器,而我使用的是静态设置?

(或者它是 ES6 的东西?我知道反引号是 ES6 的一部分,但它们似乎没有在教程中为 ES6 放置任何东西。我能看到的唯一区别是本地服务器。)

如果我不使用反引号,它会起作用:

 computed: {
    bobbyFullName() {
        return this.bobby.first + ' ' + this.bobby.last
    },
    johnFullName() {
        return this.john.first + ' ' + this.john.last
    }
  }, 

最佳答案

错误是您使用的是括号“()”而不是大括号“{}

尝试:

bobbyFullName() {
        return this.bobby.first + ' ' +  this.bobby.last
    }

或者:

bobbyFullName() {
        return `${this.bobby.first} ${this.bobby.last}`
    }

关于javascript - 反引号中返回的 vue.js 计算方法未在模板中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026550/

相关文章:

JavaScript Switch 语句——单例中的同义词

vue.js - 在 Vue 中加载静态 HTML 文件

javascript - Css 关键帧动画只适用于一个元素

jquery - 使用 IIS 静态内容服务与从 SQL Server Jquery WCF 获取数据之间的性能差异

c++ - 子类不是父类的静态成员

rust - 在 Rust 中有没有办法将静态变量包含到其他文件中?

javascript - 在 React 中的按钮单击上添加和删除 HTML 元素

javascript - 如何在不使用 __proto__ 的情况下使一个对象继承另一个对象?

javascript - 通过特定属性计算对象数组的总和返回 NaN

javascript - "mount"在 Vue.js 中是什么意思?