LoginSignup
21
16

More than 5 years have passed since last update.

Nuxt×vuexfireでちょっとハマった

Last updated at Posted at 2018-08-06

やりたかったこと

  • Nuxtで
  • storeをモジュールモードで
  • namespaceを利用して
  • vuexfireを使う

ハマったところ

store/index.jsにvuexfire関連の設定をしているサンプルはたくさんあったが、namespaceを利用してindex.js以外のファイルを使おうとした場合に、

[vuex] unknown mutation type: VUEXFIRE_ARRAY_INITIALIZE
[vuex] unknown mutation type: vuexfire/ARRAY_ADD

が出てしまった。

その時の設定例は以下の通り。(index.jsならうまく行くが、それ以外だと↑のエラーで怒られる)

store/index.js/other.js
import firebase from '@/plugins/firebase'
import { firebaseMutations, firebaseAction } from 'vuexfire'

const db = firebase.database()
const itemsRef = db.ref('items')

export const state = () => ({
  items: []
})

export const mutations = {
  ...firebaseMutations
}

export const getters = {
  items: state => {
    return state.items
  }
}

export const actions = {
  init: firebaseAction(({ bindFirebaseRef }) => {
    bindFirebaseRef('items', itemsRef)
  })
}

解決策

ちゃんとREAMDEに記載されていた。。。

スクリーンショット 2018-08-06 14.51.52.png

mutationsはrootに作れよ、と。

ということでmutaionsのみindex.jsへ。

store/index.js
import { firebaseMutations } from 'vuexfire'

export const mutations = {
  ...firebaseMutations
}
store/other.js
import firebase from '@/plugins/firebase'
import {firebaseAction } from 'vuexfire'

const db = firebase.database()
const itemsRef = db.ref('items')

export const state = () => ({
  items: []
})

export const getters = {
  items: state => {
    return state.items
  }
}

export const actions = {
  init: firebaseAction(({ bindFirebaseRef }) => {
    bindFirebaseRef('items', itemsRef)
  })
}

これでエラーが解消され、やりたかったことがやれるようになった。

サンプル

こちら

教訓

ドキュメントを読め。
読んだつもりじゃなくて読め。

21
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
16