vue.js - 组合 API - Vue 3 - 如何在模板中调用函数以获取返回值
问题描述
在我的模板中,我使用以下代码:
<template>
{{ getScope(scope.row, itemIn.field) }}
</template>
在我使用的选项 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,
};
}
解决方案
在组合 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) { ...
推荐阅读
- javascript - JavaScript 上的冲突
- angular - 找不到管道“异步”-Angular 11
- iis - 当受信任的根证书颁发机构中存在任何非自签名证书时授权失败
- python - Python使音频文件的特定部分静音
- python - Python:获取列表中的值
- unity3d - Unity json 给出空或空结果
- amazon-web-services - 调用 Sagemaker 端点的最佳方式是什么?
- c# - 在 Postman 中使用单个 POST 请求在 dynamics 365 中插入多条记录
- java - 使用 selenium 运行 ax 时出现 StaleElementException
- linux - Docker 问题,入口点 file.sh 无法识别 su-exec 命令