uni-app 使用TS + vue-property-decorator 编译H5正常,APP真机调试出现无法读取属性问题
uni-app 使用TS + vue-property-decorator 编译H5正常,APP真机调试出现无法读取属性问题
| 项目创建方式 | CLI |
|---|---|
| CLI版本号 | 4.4.6 |
示例代码:
就是最简单的示例代码
```html
<template>
<view class="content">
<image class="logo" src="../../static/logo.png" />
<view>
<text class="title">Uni-APP{{title}}</text>
</view>
</view>
</template>
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
@Component({})
export default class index extends Vue {
private title: any = "Hello"
onLoad() {
console.log(this.title, '11111');
}
};
</script>
操作步骤:
如上
预期结果:
真机模式下运行正常
实际结果:
真机模式下运行报错
bug描述:
运行为H5时完全正常, 运行为真机调试时报错如下
16:43:32.435 [Vue warn]: Error in data(): "TypeError: _super.apply is not a function. (In '_super.apply(this, arguments)', '_super.apply' is undefined)"
16:43:32.459 (found at pages/index/index.vue:1) __ERROR
16:43:32.481 TypeError: _super.apply is not a function. (In '_super.apply(this, arguments)', '_super.apply' is undefined) __ERROR
16:43:32.506 undefined, 11111 at pages/index/index.vue:11
16:43:32.525 [Vue warn]: Property or method "title" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
更多关于uni-app 使用TS + vue-property-decorator 编译H5正常,APP真机调试出现无法读取属性问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
问题解决了, 记录一下, 因为tsconfig.json 的配置问题。
{
“compilerOptions”: {
“target”: “esnext”, // 我原本设置的是es5 所以浏览器里是正常的, 但是app里就有问题, 设置成esnext 就 两端都正常了
}
}
更多关于uni-app 使用TS + vue-property-decorator 编译H5正常,APP真机调试出现无法读取属性问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这个问题是由于 vue-property-decorator 在 uni-app 的 App 端与 TypeScript 编译兼容性导致的。H5 平台使用 Webpack 编译,而 App 平台使用 V8 引擎,对装饰器的支持存在差异。
解决方案:
-
检查 TypeScript 配置 确保
tsconfig.json中启用装饰器支持:{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } } -
降级 vue-property-decorator 版本 尝试使用较旧版本(如 8.x 或 9.x),新版可能存在兼容问题:
npm install vue-property-decorator@8.9.0 -
改用 Composition API 推荐迁移到
vue-class-component@next+composition-api,或直接使用<script setup lang="ts">:<script setup lang="ts"> import { ref } from 'vue' const title = ref("Hello") </script> -
检查 CLI 插件 确保已安装并正确配置
@dcloudio/types和uni-app类型声明。 -
临时规避方案 若不使用装饰器,可暂时改用 Options API:
export default { data() { return { title: "Hello" } } }

