代码之家  ›  专栏  ›  技术社区  ›  Sarmad Ali

vuejs中的计算属性到底是什么?

  •  0
  • Sarmad Ali  · 技术社区  · 6 年前

    下面有几个与计算属性相关的问题

    1. “vuejs窗体计算属性”

    2. “VueJs中的计算属性”

    3. “VueJS中的计算属性”

    4. “在Vuejs中的数据中使用计算属性”

    他们在询问具体的错误或逻辑。有很多网站都在解释与vuejs相关的概念。我在vuejs官方网站上看到了计算属性。当我们做复杂的计算或想避免写更多的逻辑在我们的 html 然后我们使用计算属性。

    2 回复  |  直到 6 年前
        1
  •  3
  •   tao    6 年前

    太长,读不下去了 :计算属性是Vue中的getter/setter。


    computed: {
      someComputed() {
        return `${this.foo} ${this.bar}`;
      }
    }
    

    等同于

    computed: {
      someComputed: {
        get: function() {
          return `${this.foo} ${this.bar}`;
        }
      }
    }
    

    也可以有一个设置器:

    computed: {
      someComputed: {
        get: function() {
          return `${this.foo} ${this.bar}`;
        }
        set: function(fooBar) {
          const fooBarArr = fooBar.split(' ');
          this.foo = fooBarArr[0];
          this.bar = fooBarArr[1];
        }
      }
    }
    

    • 吸气剂 :查找该属性时函数运行;用法:
    this.someComputed // returns the computed current value, running the getter.
    
    • 塞特
    this.someComputed = value; // sets the computed current value, running the setter.
    

    getters setters 在Javascript中。

    Vue computed properties .

        2
  •  1
  •   Daniel Kemeny    6 年前

    你可以使用计算属性,例如当你有一些逻辑什么会炸毁你的模板。

    为此,您可以使用计算道具,这些道具通常做一些简单的事情,如:

      computed: {
        // a computed getter
        reversedMessage: function () {
          // `this` points to the vm instance
          return this.message.split('').reverse().join('')
        }
      }
    

    或者另一个很好的例子,如果你有一些货币,你想格式化它与千分隔符和欧元签署在最后。

    像这样:

    <div>{{reversedMesage}}</div>
    

    每次,当你计算的属性中使用的任何变量发生变化时,vue vill都会处理它,并重新计算你的计算属性。

    假设您有以下条件:

      computed: {
        prettyAmount: function () {
          return this.amount + ' ' + this.currency.toUpperCase()
        }
      }
    
    <div>{{prettyAmount}}</div>