You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue3 Composition API 如何在template中调用函数获取返回值

问题原因

你的Composition API代码中getScope函数声明语法不合法:在setup函数的作用域内定义普通函数必须添加function关键字,你当前的写法是对象方法的简写语法,仅能在对象字面量中使用(比如你之前写的Options API的methods对象里),无法在函数作用域内生效。

修复代码

标准setup选项写法

setup() {
  // 补充function关键字声明函数
  function 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,
  };
}

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 23:45:01