美文网首页
vue computed属性 基本使用

vue computed属性 基本使用

作者: 咸鱼不咸_123 | 来源:发表于2022-04-03 11:29 被阅读0次

 1.什么是计算属性

计算属性是指通过一系列运算之后,最终得到一个属性值

这个动态计算出来的属性值可以被模板结构methods方法使用。

1 . 所有的计算属性都需要定义在computed节点下

2. 声明的时候,计算属性在定义的时候需要定义成方法格式

3. 调用的时候,以属性的方式进行调用

 1.1 好处

1.实现了代码复用

2.只要计算属性中依赖的数据源变化了,则计算属性会重新求值

代码示例如下:

```vue

<template>

  <div>

    <h3>计算属性computed的使用</h3>

    <p>R: <input type="text" v-model.number="r" /></p>

    <p>G: <input type="text" v-model.number="g" /></p>

    <p>B: <input type="text" v-model.number="b" /></p>

    <button @click="show">按钮</button>

    <div class="box" :style="{ backgroundColor: getRGB }">{{ getRGB }}</div>

  </div>

</template>

<script>

export default {

  data() {

    return {

      r: "",

      g: "",

      b: "",

    };

  },

  computed: {

    /**

    * 1.所有的计算属性都需要定义到computed节点下

    * 2.计算属性在定义的时候需要定义为方法格式

    */

    getRGB() {

      //这个方法返回一个RGB形式的字符串

      return `rgb(${this.r},${this.g},${this.b})`;

    },

  },

  methods: {

    show() {

      console.log(this.getRGB);

    },

  },

};

</script>

<style lang="scss" scoped>

.box {

  width: 300px;

  height: 300px;

  border: 1px solid black;

}

</style>

```

 2.总结

相关文章

  • 记录一些前端遇到的问题

    一、Vue props 使用 二、Vue 计算属性 computed 的使用

  • vue computed属性 基本使用

    1.什么是计算属性 计算属性是指通过一系列运算之后,最终得到一个属性值 这个动态计算出来的属性值可以被模板结构或m...

  • Vue计算属性简析

    Vue Computed Vue开发人员必然使用过计算属性(Computed Properties):你可以像绑定...

  • Vue-基础-03-重点

    Vue-基础-day03-重点 01-基础-实例选项-计算属性-基本使用 场景:b依赖a b就是computed...

  • 2020-07-28

    了解Vue计算属性的实现原理 computed的作用 在vue的开发中,我们不免会使用到计算属性,使用计算属性,v...

  • Vue复习

    Vue的计算属性 计算属性computed

  • vue 同时监听多个值的变化

    使用computed 和watch来同时监听多个属性值。。 参考原文:vue 使用watch同时监听多个属性[ht...

  • watch、methods 和 computed 的区别?

    1、基本说明1.1)computed:计算属性将被混入到 Vue 实例中,所有 getter 和 setter 的...

  • 3.vue计算属性和过滤器

    1.计算属性 Vue中的computed属性称为计算属性.它与methods不同,computed是响应式的,调用...

  • Vue的Methods

    话不多说先上代码 除了在文本插值的方式,我们可以使用Vue中的 computed 属性,可以在 computed ...

网友评论

      本文标题:vue computed属性 基本使用

      本文链接:https://www.haomeiwen.com/subject/nfdfsrtx.html