【问题标题】:Composition API - Vue 3 - How to call a function inside the template to get a value returned组合 API - Vue 3 - 如何在模板中调用函数以获取返回值
【发布时间】:2021-10-11 03:25:33
【问题描述】:

在我的模板中,我使用了以下代码:

 <template> 
    {{ getScope(scope.row, itemIn.field) }}    
 </template>

在我使用的 Option API 中:

methods: {
    getScope(data, key) {
        const str_spl = key.split(".")
        if(str_spl.length == 2) {
            return data[str_spl[0]][str_spl[1]]
        } else {
            return data[key]
        }
    },
}

现在我想转向组合 API 方法。我创建了以下代码,但无法像使用 Options API 那样将其返回。如何解决?

setup() {

 getScope(data, key) {
     const str_spl = key.split(".")
     if(str_spl.length == 2) {
         return data[str_spl[0]][str_spl[1]]
     } else {
         return data[key]
     }
 }

 return {
   getScope,
 };
}

【问题讨论】:

    标签: vue.js vuejs3 vue-composition-api


    【解决方案1】:

    在组合 API 中,您需要将方法声明为函数:

    const getScope = (data, key) => {
      const str_spl = key.split(".")
      if(str_spl.length == 2) {
         return data[str_spl[0]][str_spl[1]]
      } else {
         return data[key]
      }
    }
    

    function getScope(data, key) { ...
    

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2020-06-01
      • 2021-02-13
      • 2023-02-22
      • 2021-01-14
      • 2020-05-14
      相关资源
      最近更新 更多