299

这是否可以在 Vue.Js 的计算属性中传递参数。我可以看到当 getter/setter 使用计算时,他们可以接受一个参数并将其分配给一个变量。就像文档中的这里:

// ...
computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}
// ...

这也可能吗:

// ...
computed: {
  fullName: function (salut) {
      return salut + ' ' + this.firstName + ' ' + this.lastName    
  }
}
// ...

其中计算属性接受一个参数并返回所需的输出。但是,当我尝试这个时,我收到了这个错误:

vue.common.js:2250 Uncaught TypeError: fullName is not a function(...)

我应该在这种情况下使用方法吗?

4

10 回答 10

408

很可能您想使用一种方法

<span>{{ fullName('Hi') }}</span>

methods: {
  fullName(salut) {
      return `${salut} ${this.firstName} ${this.lastName}`
  }
}

更长的解释

从技术上讲,您可以使用带有如下参数的计算属性:

computed: {
   fullName() {
      return salut => `${salut} ${this.firstName} ${this.lastName}`
   }
}

(感谢Unirgy您的基本代码。)

计算属性和方法之间的区别在于计算属性被缓存并且仅在它们的依赖关系发生更改时才更改。每次调用时都会评估一个方法。

如果您需要参数,在这种情况下,使用计算属性函数而不是方法通常没有任何好处。尽管它允许您将参数化的 getter 函数绑定到 Vue 实例,但您会丢失缓存,因此实​​际上并没有任何收益,实际上,您可能会破坏反应性 (AFAIU)。您可以在 Vue 文档https://vuejs.org/v2/guide/computed.html#Computed-Caching-vs-Methods中阅读更多相关信息

唯一有用的情况是当您必须使用 getter 并需要对其进行参数化时。例如,这种情况发生在Vuex中。在 Vuex 中,这是从存储中同步获取参数化结果的唯一方法(操作是异步的)。因此,官方 Vuex 文档为它的 getters 列出了这种方法 https://vuex.vuejs.org/guide/getters.html#method-style-access

于 2016-11-11T00:47:22.660 回答
43

您可以使用方法,但我更喜欢使用计算属性而不是方法,如果它们没有改变数据或没有外部影响。

您可以通过这种方式将参数传递给计算属性(未记录,但维护人员建议,不记得在哪里):

computed: {
   fullName: function () {
      var vm = this;
      return function (salut) {
          return salut + ' ' + vm.firstName + ' ' + vm.lastName;  
      };
   }
}

编辑:请不要使用此解决方案,它只会使代码复杂化而没有任何好处。

于 2017-01-05T00:11:24.103 回答
12

好吧,从技术上讲,我们可以将参数传递给计算函数,就像我们可以将参数传递给 vuex 中的 getter 函数一样。这样的函数是返回函数的函数。

例如,在商店的吸气剂中:

{
  itemById: function(state) {
    return (id) => state.itemPool[id];
  }
}

这个 getter 可以映射到组件的计算函数:

computed: {
  ...mapGetters([
    'ids',
    'itemById'
  ])
}

我们可以在模板中使用这个计算函数,如下所示:

<div v-for="id in ids" :key="id">{{itemById(id).description}}</div>

我们可以应用相同的方法来创建一个带参数的计算方法。

computed: {
  ...mapGetters([
    'ids',
    'itemById'
  ]),
  descriptionById: function() {
    return (id) => this.itemById(id).description;
  }
}

并在我们的模板中使用它:

<div v-for="id in ids" :key="id">{{descriptionById(id)}}</div>

话虽如此,我并不是说这是使用 Vue 做事的正确方式。

但是,我可以观察到,当具有指定 ID 的项目在商店中发生变化时,视图确实会使用该项目的新属性自动刷新其内容(绑定似乎工作得很好)。

于 2018-01-26T14:02:22.917 回答
6

[Vue2]过滤器是 Vue 组件提供的一项功能,可让您将格式和转换应用于模板动态数据的任何部分。

它们不会更改组件的数据或任何东西,但只会影响输出。

假设您正在打印一个名称:

new Vue({
  el: '#container',
  data() {
    return {
      name: 'Maria',
      lastname: 'Silva'
    }
  },
  filters: {
    prepend: (name, lastname, prefix) => {
      return `${prefix} ${name} ${lastname}`
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="container">
  <p>{{ name, lastname | prepend('Hello') }}!</p>
</div>

请注意应用过滤器的语法,即 | 过滤器名称。如果您熟悉 Unix,那就是 Unix 管道运算符,它用于将操作的输出作为输入传递给下一个操作。

组件的过滤器属性是一个对象。单个过滤器是一个接受一个值并返回另一个值的函数。

返回的值是实际打印在 Vue.js 模板中的值。

于 2019-03-17T16:36:58.063 回答
5

你可以传递参数,但要么它不是 vue.js 的方式,要么你做的方式是错误的。

但是在某些情况下您需要这样做。我将向您展示一个使用 getter 和 setter 将值传递给计算属性的简单示例。

<template>
    <div>
        Your name is {{get_name}} <!-- John Doe at the beginning -->
        <button @click="name = 'Roland'">Change it</button>
    </div>
</template>

还有剧本

export default {
    data: () => ({
        name: 'John Doe'
    }),
    computed:{
        get_name: {
            get () {
                return this.name
            },
            set (new_name) {
                this.name = new_name
            }
        },
    }    
}

单击按钮时,我们将名称“Roland”传递给计算属性,并且set()我们将名称从“John Doe”更改为“Roland”。

下面是当计算与 getter 和 setter 一起使用时的常见用例。假设您有以下 vuex 商店:

export default new Vuex.Store({
  state: {
    name: 'John Doe'
  },
  getters: {
    get_name: state => state.name
  },
  mutations: {
    set_name: (state, payload) => state.name = payload
  },
})

在您的组件中,您想添加v-model到输入但使用 vuex 存储。

<template>
    <div>
        <input type="text" v-model="get_name">
        {{get_name}}
    </div>
</template>
<script>
export default {
    computed:{
        get_name: {
            get () {
                return this.$store.getters.get_name
            },
            set (new_name) {
                this.$store.commit('set_name', new_name)
            }
        },
    }    
}
</script>
于 2018-06-24T06:09:52.270 回答
5

您还可以通过返回函数将参数传递给 getter。当您要查询存储中的数组时,这特别有用:

getters: {
  // ...
  getTodoById: (state) => (id) => {
    return state.todos.find(todo => todo.id === id)
  }
}
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }

请注意,通过方法访问的 getter 将在您每次调用它们时运行,并且结果不会被缓存。

这称为方法样式访问,它记录在 Vue.js 文档中。

于 2018-12-05T16:03:14.340 回答
5
computed: {
  fullName: (app)=> (salut)=> {
      return salut + ' ' + this.firstName + ' ' + this.lastName    
  }
}

当你想使用

<p>{{fullName('your salut')}}</p>
于 2019-02-27T11:54:30.953 回答
2

我想首先重申之前的警告,即使用带参数的计算(已缓存)只会使计算不被缓存,实际上只是使其成为一种方法。

然而,话虽如此,这里是我能想到的所有变体,它们可能有边缘情况可供使用。如果您将其剪切并粘贴到演示应用程序中,则应该清楚发生了什么:

<template>
  <div>

    <div style="background: violet;"> someData, regularComputed: {{ someData }}, {{ regularComputed }} </div>
    <div style="background: cornflowerblue;"> someComputedWithParameterOneLine: {{ someComputedWithParameterOneLine('hello') }} </div>
    <div style="background: lightgreen;"> someComputedWithParameterMultiLine: {{ someComputedWithParameterMultiLine('Yo') }} </div>
    <div style="background: yellow"> someComputedUsingGetterSetterWithParameterMultiLine: {{ someComputedUsingGetterSetterWithParameterMultiLine('Tadah!') }} </div>

    <div>
      <div style="background: orangered;"> inputData: {{ inputData }} </div>
      <input v-model="inputData" />
      <button @click="someComputedUsingGetterSetterWithParameterMultiLine = inputData">
        Update 'someComputedUsingGetterSetterWithParameterMultiLine' with 'inputData'.
      </button>
    </div>

    <div style="background: red"> newConcatenatedString: {{ newConcatenatedString }} </div>

  </div>
</template>

<script>

  export default {

    data() {
      return {
        someData: 'yo',
        inputData: '',
        newConcatenatedString: ''
      }
    },

    computed: {

      regularComputed(){
        return 'dude.'
      },

      someComputedWithParameterOneLine(){
        return (theParam) => `The following is the Parameter from *One* Line Arrow Function >>> ${theParam}`
      },

      someComputedWithParameterMultiLine(){
        return (theParam) => {
          return `The following is the Parameter from *Multi* Line Arrow Function >>> ${theParam}`
        }
      },

      // NOTICE that Computed with GETTER/SETTER is now an Object, that has 2 methods, get() and set(), so after the name of the computed we use : instead of ()
      // thus we do: "someComputedUsingGetterSetterWithParameterMultiLine: {...}" NOT "someComputedUsingGetterSetterWithParameterMultiLine(){...}"
      someComputedUsingGetterSetterWithParameterMultiLine: {
        get () {
          return (theParam) => {
            return `As part of the computed GETTER/SETTER, the following is inside get() which receives a Parameter (using a multi-line Arrow Function) >>> ${theParam}`
          }
        },
        set(newSetValue) {
          console.log('Accessing get() from within the set()', this.someComputedUsingGetterSetterWithParameterMultiLine('hello from inside the Setter, using the Getter.'))
          console.log('Accessing newSetValue in set() >>>>', JSON.stringify(newSetValue))
          this.newConcatenatedString = `**(1)${this.someComputedUsingGetterSetterWithParameterMultiLine('hello from inside the Setter, using the Getter.')}**  This is a concatenation of get() value that had a Parameter, with newSetValue **(2)${newSetValue}** that came into the set().`
        }
      },

    },

  }

</script>
于 2021-07-06T23:32:39.407 回答
1

Computed 可以被认为是一个函数。因此,对于一个示例,validation您可以清楚地执行以下操作:

    methods: {
        validation(attr){
            switch(attr) {
                case 'email':
                    const re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
                    return re.test(this.form.email);
                case 'password':
                    return this.form.password.length > 4
            }
        },
        ...
    }

您将使用如下:

  <b-form-input
            id="email"
            v-model="form.email"
            type="email"
            :state="validation('email')"
            required
            placeholder="Enter email"
    ></b-form-input>

请记住,您仍然会错过特定于computed.

于 2019-09-06T08:22:44.680 回答
0

是的,有使用参数的方法。就像上面提到的答案一样,在您的示例中,最好使用方法,因为执行非常轻松。

仅供参考,在方法复杂、成本高的情况下,可以这样缓存结果:

data() {
    return {
        fullNameCache:{}
    };
}

methods: {
    fullName(salut) {
        if (!this.fullNameCache[salut]) {
            this.fullNameCache[salut] = salut + ' ' + this.firstName + ' ' + this.lastName;
        }
        return this.fullNameCache[salut];
    }
}

注意:使用它时,如果处理数千个,请注意内存

于 2017-08-16T04:53:35.393 回答