Reputation: 3439
I came across a very strange behavior where Vue is complaining about a missing ) but in reality there is no missing ). What makes it even stranger is that if I do not use the filterOptions object but make a simple property then it works. For some reason it can't handle it being a property of an object.
[Vue warn]: Failed to generate render function: SyntaxError: missing ) after argument list
<input
v-model="date_format(filterOptions.date_start)"
/>
But if I change it to this (without filterOptions object) then it works
<input
v-model="date_format(startdate)"
/>
Here's my date_format function and data.
methods:
{
date_format(date)
{
return (date != null && date != '') ?
moment(date, 'YYYY-MM-DD').format("DD.MM.YYYY") : ''
},
},
data()
{
return {
total: 10,
startdate: '',
filterOptions: {
perPage: 10,
orderBy: 'end_date',
orderDirection: 'desc',
date_start: '',
end_date_end: '',
},
}
},
Upvotes: 0
Views: 830
Reputation: 56
I agree with the above answer, and there is a very stupid way to directly pass the filterOptions object into the method.
Upvotes: 0
Reputation: 1470
To use a property that is derived from another property as a v-model, you should use computed properties instead of methods. Computed properties have two explicit methods, get and set.
In the getter you can get the YYYY-MM-DD formatted startdate and convert it into DD.MM.YYYY and return, and in the setter you can take a DD.MM.YYYY and convert it into YYYY-MM-DD and set it into startdate.
<div id="app">
<p>{{ message }}</p>
<input v-model="formatted">
{{ startdate }}
</div>
new Vue({
el: "#app",
data: {
message: "Hello Vue.js!",
total: 10,
startdate: "2017-02-15",
filterOptions: {
perPage: 10,
orderBy: "end_date",
orderDirection: "desc",
date_start: "",
end_date_end: ""
}
},
computed: {
formatted: {
get: function() {
return this.startdate != null && this.startdate != ""
? moment(this.startdate, "YYYY-MM-DD").format("DD.MM.YYYY")
: "";
},
set: function(newValue) {
this.startdate = newValue != null && newValue != ""
? moment(newValue, "DD.MM.YYYY").format("YYYY-MM-DD")
: ""
}
}
}
});
Upvotes: 2