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

Vue3使用computed函数出现Vetur(1005)分号缺失错误求助

解决Vue3中computed函数报“';' expected.Vetur(1005)”错误的方案

你的代码核心问题是Options API中computed选项的语法写错了:

  • Vue的Options API规范里,computed是一个对象,不是可执行函数,你写成computed(){}这种函数调用的形式,属于语法错误,才触发了Vetur的分号缺失提示。

修正后的代码如下:

<script>
// eslint-disable-next-line no-unused-vars
import marked from "marked";
export default {
  data() {
    return {
      text: "",
    };
  },
  // 把computed(){}改成computed: {}
  computed: {
    markedText() {
      return marked(this.text);
    }
  }
};
</script>

另外补充两种常见写法的区别:

  • 如果你用的是Vue3的<script setup>语法(Composition API),computed才是需要调用的函数,写法如下:
<script setup>
import { computed, ref } from 'vue';
import marked from "marked";

const text = ref('');
// Composition API中computed是函数,接收回调返回计算值
const markedText = computed(() => marked(text.value));
</script>

你之前遇到“相同语法偶尔能运行”大概率是编辑器缓存或临时语法兼容的偶然情况,本质还是不符合Vue的API规范导致的报错。

内容的提问来源于stack exchange,提问作者Kai021195

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:20:27