将数据传递给指令?

pan*_*hro 4 vue.js vuejs2 vue-directives

文档中,它指出您可以将各种参数传递给指令。

所以我想传入一个值:

v-my-directive="test"
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

Property or method "test" is not defined on the instance but referenced during render
Run Code Online (Sandbox Code Playgroud)

如何将字符串传递给指令?

acd*_*ior 6

该值是一个常规 JavaScript 表达式。这样,如果您想传递一个字符串,例如'test',请使用:

v-my-directive="'test'"
Run Code Online (Sandbox Code Playgroud)

演示:

v-my-directive="'test'"
Run Code Online (Sandbox Code Playgroud)
Vue.directive('my-directive', function (el, binding) {
  console.log('directive expression:', binding.value)  // => "test"
})

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  }
})
Run Code Online (Sandbox Code Playgroud)