简体   繁体   English

[Vue警告]:在参数列表后无法生成渲染函数:SyntaxError:缺少)

[英][Vue warn]: Failed to generate render function: SyntaxError: missing ) after argument list

I came across a very strange behavior where Vue is complaining about a missing ) but in reality there is no missing ). 我遇到了一个非常奇怪的行为,其中Vue抱怨缺少,但实际上没有缺失。 What makes it even stranger is that if I do not use the filterOptions object but make a simple property then it works. 更令我感到奇怪的是,如果我不使用filterOptions对象,而是创建一个简单的属性,那么它将起作用。 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 [Vue警告]:在参数列表后无法生成渲染函数:SyntaxError:缺少)

<input
    v-model="date_format(filterOptions.date_start)"
/>

But if I change it to this (without filterOptions object) then it works 但是,如果我将其更改为此(没有filterOptions对象),那么它将起作用

<input
    v-model="date_format(startdate)"
/>

Here's my date_format function and data. 这是我的date_format函数和数据。

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: '',
        },
    }
},

To use a property that is derived from another property as a v-model, you should use computed properties instead of methods. 要将从另一个属性派生的属性用作v模型,应使用计算属性而不是方法。 Computed properties have two explicit methods, get and set. 计算属性具有两个显式方法:get和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. 在getter中,您可以获取YYYY-MM-DD格式的开始日期,并将其转换为DD.MM.YYYY并返回,在setter中,您可以获取DD.MM.YYYY并将其转换为YYYY-MM-DD并进行设置它到开始日期。

<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")
          : ""
      }
    }
  }
});

我同意上面的答案,并且有一种非常愚蠢的方法直接将filterOptions对象传递给方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM