小编use*_*238的帖子

Vue 的自定义指令中何时调用 bind 和 unbind 以及它们如何与生命周期函数(挂载/销毁)相关联

说,我创建了一个自定义指令来处理窗口调整大小事件,我写了

Vue.directive('resize', {
  bind(el, binding, vnode) {
    el.resizeEvent = function() {
      vnode.context[binding.expression]()
    }

    // I would like it to be called in mounted
    Vue.nextTick(el.resizeEvent)

    window.addEventListener('resize', el.resizeEvent)
  },
  unbind(el) {
    window.removeEventListener('resize', el.resizeEvent)
  }
})
Run Code Online (Sandbox Code Playgroud)

我添加Vue.nextTick(el.resizeEvent)是因为我希望在安装组件时调用此函数。

到目前为止,该指令按预期工作。不过,我没有约时,一个清晰的思路bindunbind被调用。

该文件声称

bind: called only once, when the directive is first bound to the element. This is where you can do one-time setup work.

unbind: called only once, when the directive is unbound from the element.

我的问题是,当正好是bindunbind …

javascript vue.js vuejs2

5
推荐指数
1
解决办法
813
查看次数

为什么2.valueOf()无效但(2).valueOf()是?

我猜的JavaScript将解析(2).valueOf()new Number(2).valueOf(),但为什么它不是第一个?

根据运算符优先级,分组运算符的优先级应高于成员访问权限https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

那么为什么(2)不首先进行评估2而不是被解析为屈服new Number(2)

javascript

1
推荐指数
1
解决办法
35
查看次数

标签 统计

javascript ×2

vue.js ×1

vuejs2 ×1