【问题标题】:How to declare computed values in SFC/Composition in Vue?如何在 Vue 的 SFC/Composition 中声明计算值?
【发布时间】:2022-01-24 05:35:13
【问题描述】:

我正在尝试在 VueJs 中使用 computed 字段。

以前可以通过以下方式实现:

{
  props: {
    left: 5,
    right: 100,
  },
  computed: {
    width: () {return this.right.value - this.left.value};
  }
}

但是使用新的 SFC/Composition 语法我该怎么做呢?

<script setup>
defineProps({left: 5, right: 100});

defineComputed(  //??
)
</script>

我已经阅读了这个链接,但它没有解释它: https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    在新的设置语法中,您使用 computed 创建计算属性:

    import { computed } from 'vue'
    
    const props = defineProps({left: 5, right: 100});
    const width = computed(() => props.right - props.left);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 2022-08-23
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多