首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在composition api中使用钟表钩子?

在composition api中使用钟表钩子?
EN

Stack Overflow用户
提问于 2021-10-22 18:04:54
回答 1查看 74关注 0票数 0

我在Options API中有以下代码:

代码语言:javascript
复制
  ...
  watch: {
    $things: {
      async handler(val, oldVal) {
        this.someFunction()
        if(this.someVariable) this.getSomethingHere()
      },
      deep: true
    }
  },
  ...
})

如何使用watch hook将此代码重构为composition api?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-22 21:12:52

watch选项的等效组合API是watch()。假设$things是一个global property,使用getCurrentInstance().appContext.config.globalProperties访问$things

代码语言:javascript
复制
import { watch, getCurrentInstance } from 'vue'

export default {
  setup() {
    const someFunction = () => {/*...*/}
    const getSomethingHere = () => {/*...*/}
    let someVariable = true

    const globals = getCurrentInstance().appContext.config.globalProperties
    watch(
      globals.$things,
      async handler(val, oldVal) {
        someFunction()
        if (someVariable) getSomethingHere()
      },
      { deep: true }
    )
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69681331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档