在你的store的modules的js文件中
// 重点
import Vue from 'vue'
const state = {
friendsMap: {}
}
// getters
const getters = {
friendsMap: state => state.friendsMap
}
// actions
// let db = getters.db
const actions = {
SetFriendsMap ({ commit, rootGetters }, info) {
let db = rootGetters.db
db.transaction(function (tx) {
tx.executeSql(
'SELECT wechatId,friendId,avatar,friendNick FROM FRIENDS WHERE wechatId = ? and friendId = ?',
[info.wechatId, info.friendId],
function (tx, results) {
commit('SET_FRIENDS_MAP', results.rows[0])
},
function (tx, error) {
console.log(error)
}
)
})
}
}
// mutations
const mutations = {
SET_FRIENDS_MAP: (state, friend) => {
// state.friendsMap[friend.friendId + friend.wechatId] = friend
// 重点
Vue.set(state.friendsMap, friend.friendId + friend.wechatId, friend)
}
}