【发布时间】:2019-11-10 13:46:37
【问题描述】:
我正在使用 Vue.js、Vuex 和 Firebase 构建一个待办事项列表应用程序。该应用程序似乎可以正常工作,因为 Store 文件成功地管理了输入的 todo 项的检索和呈现(往返于 firestore)。但是,我仍然有一个关于在 Vuex 中设置参数的问题。突变中的 REMOVE_TODO 函数(参见 store.js)似乎需要两个参数,即使“id”是函数实际代码中引用的唯一参数。换句话说,如果我取出初始参数(在本例中为“state”),那么控制台将返回一个错误,内容为:“Function CollectionReference.doc() 要求其第一个参数的类型为非空字符串,但是它是:一个自定义对象对象”。我的问题是:如果“id”参数是函数中唯一实际使用的参数,为什么这个 REMOVE_TODO 函数需要两个参数才能正常运行?为什么需要另一个论点?下面是我的代码。谢谢!
app.vue
<template>
<div id="app" class="container">
<input class="form-control" :value="newTodo" @change="getTodo" placeholder="I need to...">
<button class="btn btn-primary" @click="addTodo">Add New Post</button>
<ul class="list-group">
<li class="list-group-item" v-for="todo in this.$store.getters.getTodos" :key="todo.id">
{{todo.title}}
<div class="btn-group">
<button type="button" @click="remove(todo.id)" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-remove-circle"></span> Remove
</button>
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
beforeCreate: function() {
this.$store.dispatch('setTodo')
},
methods: {
getTodo(event) {
this.$store.dispatch('getTodo', event.target.value)
},
addTodo() {
this.$store.dispatch('addTodo')
this.$store.dispatch('clearTodo')
},
remove(id){
this.$store.dispatch('removeTodo', id)
}
},
computed: {
newTodo() {
return this.$store.getters.newTodo
},
todos(){
return this.$store.getters.todos
}
}
}
</script>
<style>
body {
font-family: Helvetica, sans-serif;
}
li {
margin: 10px;
}
</style>
store.js
import Vue from 'vue'
import Vuex from 'vuex'
import db from '../firebase'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
todos: [],
newTodo: '',
errors: ''
},
mutations: { //syncronous, committed
GET_TODO: (state, todo) => {
state.newTodo = todo
},
ADD_TODO: state => {
db.collection('items').add({
title: state.newTodo,
created_at: Date.now(),
}).then(function(){
console.log('Document successfully added')
})
.catch((error) => {
this.errors = error
})
},
REMOVE_TODO: (state, id) => {
if (id) {
db.collection("items").doc(id).delete().then(function() {
console.log('Document successfully deleted')
})
.catch((error) => {
this.errors = error
})
} else {
this.errors = 'Invalid ID'
}
},
CLEAR_TODO: state => {
state.newTodo = ''
},
SET_TODO: state => {
let todos = []
db.collection('items').orderBy('created_at').onSnapshot((snapshot) => {
todos = []
snapshot.forEach((doc) => {
todos.push({ id: doc.id, title: doc.data().title })
})
state.todos = todos
})
}
},
actions: { //asyncronous, dispatched
getTodo: (context, todo) => {
context.commit('GET_TODO', todo)
},
addTodo: context => {
context.commit('ADD_TODO')
},
removeTodo: (context, id) => {
context.commit('REMOVE_TODO', id)
},
clearTodo: context => {
context.commit('CLEAR_TODO')
},
setTodo: context => {
context.commit('SET_TODO')
}
},
getters: {
newTodo: state => state.newTodo,
getTodos: state => {
return state.todos
}
}
})
【问题讨论】:
-
嗨 JS_is_awesome18 突变是修改你必须给它的状态以更新你的状态可能是你可以在状态中有一个消息属性并做 state.message = "document deleted" 如果没问题or not state.message = "错误文档未删除".vuex.vuejs.org/guide/mutations.html
-
@Birante,感谢您的反馈,但我不确定这是否完全回答了问题。这是否意味着一个突变的函数,默认情况下,即使在函数的代码中没有使用状态,也需要状态作为参数?
-
是的,你明白我认为这是一种最佳实践的一切。
标签: javascript firebase vue.js google-cloud-firestore vuex