【问题标题】:Javascript ES6 How to convert a const function to a functionJavascript ES6 如何将 const 函数转换为函数
【发布时间】:2018-12-04 06:52:26
【问题描述】:

尝试使用 vuexfire 进行 Firebase 绑定,文档状态为插入以下绑定操作

const setTodosRef = firebaseAction(({ bindFirebaseRef, unbindFirebaseRef }, { ref }) => {
  // bunding will automatically unbind any previously bound ref so you
  // don't need to unbind before binding over an existing bound key
  bindFirebaseRef('todos', ref)
  // it is possible to unbind a bound key at any time
  unbindFirebaseRef('todos')
})

在我的商店 root.js 中,所有的动作都是用以下模式编写的

/**
 * Import Dependency
 */
import * as types from './mutation_types'
import i18n from '@/locales'
import * as firebase from 'firebase'
import { firebaseMutations, firebaseAction } from 'vuexfire'

setTodosRef ( ) {
  bindFirebaseRef('todos', ref)
  unbindFirebaseRef('todos')
}

如何将参数传递给函数? 为了打电话

this.$store.dispatch('setTodosRef', db.ref('todos'))

setTodosRef (firebaseAction(({ bindFirebaseRef, unbindFirebaseRef }, { ref }))  { ... }  

没用……

Syntax Error: Unexpected token, expected "," (119:29)

感谢反馈

更新

我使用

删除了语法错误
  setTodosRef: firebaseAction(({ bindFirebaseRef, unbindFirebaseRef }, ref) => {
    bindFirebaseRef('todos', ref)
    unbindFirebaseRef('todos')
  })

但我不确定这是否正确...?

【问题讨论】:

    标签: firebase vue.js vuexfire


    【解决方案1】:

    您的商店操作将采用两个参数。第一个是 vuex 传递的上下文对象,通常被取消引用。我没有你所有的代码,所以我不能为你写一个确切的方法,但它需要是这样的(假设是 es6):

    setTodosRef({ commit }, todos) {
      commit(types.SET_TODOS, { todos })
    } 
    

    然后你会有一个处理提交的突变。它还从 vuex 接收一个注入参数作为第一个参数,然后在第二个参数中提供数据,例如:

    [types.SET_TODOS](state, { todos }) {
      state.todos = todos
    }
    

    https://vuex.vuejs.org/guide/actions.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-06
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多