I'm able to change an array with the code below.
new Vue({
el: '#app',
data: {
students: [
{name: "derek"},
{name: "frank"}
],
},
methods: {
changeStudents: function() {
this.students = [
{name: "aa"},
{name: "bb"}
];
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<button @click="changeStudents">Click Me</button>
<p v-for="student in students">{{student.name}}</p>
</div>
But when I try to change the array while inside a Google Firebase method it doesn't work. For some reason it doesn't know this.students
is available. this.students
should be accessible from any method, at least I thought. Could this be a pointer issue or something like that?
new Vue({
el: '#app',
data: {
students: [
{name: "derek"},
{name: "frank"}
],
},
methods: {
changeStudents: function() {
var newStudents = [];
// Get the students from the database
studentsCol.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
var student = doc.data();
newStudents.push(student);
// line below doesn't work because it doesn't know this.students
// this.students.push(student);
}
});
});
// doesn't work either
this.students = newStudents;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<button @click="changeStudents">Click Me</button>
<p v-for="student in students">{{student.name}}</p>
</div>