【发布时间】:2021-10-07 20:14:13
【问题描述】:
我正在更新 vue2 到 vue3 但遇到这个问题。
我有一个服务叫TService
// T.ts
class T {
public obj = { value: false };
constructor() {
setInterval(() => {
this.obj.value = !this.obj.value;
}, 1000);
}
}
const t = new T();
export { t as TService };
服务很简单,每1秒更新一次obj值。
现在进入有趣的部分
在 vue2 上,我可以这样做:
<template>
<div> {{ test }} </div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";
import { TService } from './T;
@Component
export default class HelloWorld extends Vue {
public obj = TService.obj;
get test() {
return this.obj.value;
}
}
</script>
test 值每 1 秒在屏幕上更新一次,并按预期工作。
但是,当我使用以下代码更改为 vue3 时,它不再工作了
<template>
<div>{{ test }}</div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { TService } from './T';
@Options({})
export default class HelloWorld extends Vue {
public obj = TService.obj;
get test() {
return this.obj.value;
}
}
</script>
不知道发生了什么,如果有人能修复我的代码,不胜感激。
我正在使用最新的vue 3.1.5 和vue-class-component 8.0.0-rc.1
【问题讨论】: