Skip to content

函数简写 ​

本节目标:理解“函数简写”的核心思路,并能把它用于实际开发或面试表达。 在很多时候,你可能想在 bind 和 update 时触发相同行为,而不关心其它的钩子。比如这样写:

Vue.directive('color-swatch', function (el, binding) {  el.style.backgroundColor = binding.value})

对象字面量 如果指令需要多个值,可以传入一个 JavaScript 对象字面量。记住,指令函数能够接受所有合法的 JavaScript 表达式。

<div v-demo="{ color: 'white', text: 'hello!' }"></div>
Vue.directive('demo', function (el, binding) {  console.log(binding.value.color) // => "white"  console.log(binding.value.text)  // => "hello!"})

本站总访问 — 次