jsk*_*a13 5 javascript computed-properties vuejs3 vue-composition-api
我正在尝试使用 Vuejs 3 中的计算函数,该函数通过组合在 .js 文件中外部化。
\n这是我的 .vue 文件,它非常简单:一个递增以触发计算函数的计数变量。
\n<template>\n <div>\n {{ computedCount }}\n </div>\n</template>\n\n<script setup>\nimport { ref } from \'vue\'\nimport useComputedValue from \'./js/useComputed\' // Import computed function\n\nconst count = ref(0) // Instanciate the count variable\ncount.value += 1 // Trigger the compute\nconst computedCount = useComputedValue(count)\nconsole.debug(computedCount)\n</script>\n\nRun Code Online (Sandbox Code Playgroud)\n这是我的 useCompulated.js 文件:
\nimport { computed } from \'vue\'\n\nexport default function useComputedValue(count) {\n const computedValue = computed(() => count.value * 5)\n return {\n computedValue,\n }\n}\n\nRun Code Online (Sandbox Code Playgroud)\n该函数只是将参数中给出的值乘以 5。\n问题是 console.log(compulatedCount) 给出了
\n{computedValue: ComputedRefImpl}\ncomputedValue: ComputedRefImpl {dep: Set(1), _dirty: false, __v_isRef: true, effect: ReactiveEffect, _setter: \xc6\x92, \xe2\x80\xa6}\n[[Prototype]]: Object\n\nRun Code Online (Sandbox Code Playgroud)\n并且在模板中显示 { "compulatedValue": 5 }\n因此,该函数返回的不是参数乘以 5 的值,而是一个包装对象 refImpl。
\n该示例改编自文档: Composition doc
\n如果我直接在 .vue 文件中的标记中声明计算函数,而不从另一个文件导入它,它会按预期工作:该函数返回计数值乘以 5。
\n显然,这是我不太明白的事情......但是什么?
\n我正在使用 3.2 和标签,因此不再需要脚本标签中 setup() 的返回:3.2
\n提前谢谢您。
\n| 归档时间: |
|
| 查看次数: |
12801 次 |
| 最近记录: |