Vue.js Vuex如何正确使用store.watch()方法?

Vue.js Vuex如何正确使用store.watch()方法?,vue.js,vuex,Vue.js,Vuex,我阅读了store.watch(),但找不到有效的示例 Vuex手表 监视(fn:函数,回调:函数,选项?:对象):函数 反应性地监视fn的返回值,并在值更改时调用回调。fn接收存储的状态作为第一个参数,getters作为第二个参数。接受一个可选选项对象,该对象采用与Vue的vm.$watch方法相同的选项。 要停止观看,请调用返回的unwatch函数 我有一个Vuex操作,用于用户使用Firebase登录 signUserIn ({commit}, payload) { commit(ty

我阅读了store.watch(),但找不到有效的示例

Vuex手表 监视(fn:函数,回调:函数,选项?:对象):函数 反应性地监视fn的返回值,并在值更改时调用回调。fn接收存储的状态作为第一个参数,getters作为第二个参数。接受一个可选选项对象,该对象采用与Vue的vm.$watch方法相同的选项。 要停止观看,请调用返回的unwatch函数

我有一个Vuex操作,用于用户使用Firebase登录

signUserIn ({commit}, payload) {
  commit(types.SET_LOADING, true)
  commit(types.CLEAR_ERROR)
  firebase.auth().signInWithEmailAndPassword(payload.email, payload.password)
  .then((user) => {
    commit(types.SET_LOADING, false)
    const newUser = { id: user.uid, name: user.displayName, email: user.email, photoUrl: user.photoURL }
    commit(types.SET_USER, newUser)
  })
  .catch((error) => {
    commit(types.SET_LOADING, false)
    commit(types.SET_ERROR, error)
  })
},
我正在从我的Signin vue组件发送此操作

在我的业务案例中,登录失败,因为用户尚未注册,所以会在存储中引发并提交一个erro 但在我的组件中,我不会在分派后立即捕获错误 因此,我将存储中的加载值设置为true/false 我需要看着它改变。。。所以我试着去商店看看

onSignIn方法(验证后)

    this.$store.dispatch('signUserIn', { email: this.email, password: this.password })
      console.log('loading...: ', this.$store.getters.loading)
      this.$store.watch(this.$store.getters.loading, () => {
        console.log('loaded')
        console.log('onSIgnin error', this.error.code)
      })
但是我得到了Vuex错误

 Error: [vuex] store.watch only accepts a function 
我正在传递一个函数,不是吗


感谢您的反馈

watch的第一个参数是一个函数,它接收state作为第一个参数,接收getter作为第二个参数。所以像这样访问getter,返回值就是被监视的值

      this.$store.watch((state, getters) => getters.loading, () => {
        console.log('loaded')
        console.log('onSIgnin error', this.error.code)
      })