uni-app 使用computed 终端报错

uni-app 使用computed 终端报错

开发环境 版本号 项目创建方式
Windows win10 CLI
# bug描述:

使用了computed,运行项目终端就会有报错,用的是默认模板(ts)

1. vue create -p dcloudio/uni-preset-vue my-project
2. 默认index文件使用computed  

- dev:mp-weixin  
- 切换其他node版本也会报错
2 回复

运行效果什么的没问题,只是终端会报错,看着不太舒服

更多关于uni-app 使用computed 终端报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个常见的uni-app与TypeScript配合使用computed时的类型声明问题。解决方案如下:

  1. 确保在组件中正确声明计算属性类型:
import { defineComponent, computed } from 'vue'

export default defineComponent({
  computed: {
    // 显式声明返回类型
    myComputed(): string {
      return 'computed value'
    }
  }
})
  1. 如果使用组合式API,应该这样写:
import { ref, computed } from 'vue'

const count = ref(0)
const doubleCount = computed(() => count.value * 2)
  1. 检查你的tsconfig.json配置,确保包含:
{
  "compilerOptions": {
    "strict": true,
    "types": ["[@dcloudio](/user/dcloudio)/types"]
  }
}
  1. 确保已安装必要的类型声明:
npm install [@dcloudio](/user/dcloudio)/types -D
回到顶部