<template>
<div id="app">
{{ foo.bar }}
<button @click="meaning++">click</button> <!--not reactive-->
<button @click="foo.bar++">click2</button>
</div>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
@Component
export default class App extends Vue {
private meaning = 42;
private foo = {
that:this,
get bar() {
return this.that.meaning;
},
set bar(value) {
this.that.meaning = value;
},
};
created() {
console.log(this); // VueComponent {}
console.log(this.foo.that); // App {}
console.log(this === this.foo.that); // false
}
}
</script>
我想得到哪个是顶级数据字段的foo.bar
支持。meaning
我从上面的 hack 开始将this
(Vue 选项实例)保存在 中that
,但我也知道在运行时,this
在组件方法中成为 VueComponent 实例,因此this.meaning
不是this.foo.that.meaning
一个额外的问题是上面的代码片段会vue-devtools
在 Chrome 内部中断。vue-devtools
将尝试调用Object.keys()
which instance._data
is null
。
foo.bar
得到支持的正确方法是meaning
什么?我可能在那些 getter 和 setter 中有任意复杂的逻辑。