What is the best practice for the following scenario in terms of clean logic and maintenance?
My use case is that there is a query object whose parameters are a bunch of text
and the results of the former query.
I initially thought to structure the code as follow (see pseudocode):
data() {
return {
queries : {
q1 : { data: `${this.text} + ${this.formerResults}` }
// q2, q3...
},
formerResults : ''
}
},
methods : {
makeQuery : function() {
let vm = this;
axios({ /* param of the query, data : this.queries.q1 */ })
.then((response) => { vm.formerResults += /* add the results */ })
}
The problem with this is that formerResults
does get appended, but results as undefined
when injected in the next query q1
. I can't tell why, but solved the issue by structuring the query q1
as a computed property.
Can you tell why logic fails when using a property, and works with a computed property?
computed: {
q1() {
return {
// same as above
}
}
// q2, q3 ...
}
and call it in my methods:
axios({ /* param of the query, data : this.q1 */ });
I can see that both parameters text
and formerResults
are passed.
However, I'd like to group queries (q1, q2, q3..) under a same object queries
, like in data()
: it is for code clarity, for all those queries will be against the same url, and I might have others.
But trying :
computed: {
computedQueries : {
q1() {
return {
// same as above
}
},
// q2, q3 ...
}
}
and call it like that in my methods:
axios({ /* param of the query, data : this.computedQueries.q1 */ })
will result in vue error:
Error in v-on handler: "TypeError: Cannot read property 'q1' of undefined"
Can you clarify how can I group (or nest) computed properties in one object ?
Do I really need to use a computed property in my example, to avoid formerResults
be undefined in a next query? Can you suggest how to keep code clean ?