代码笔:https ://codepen.io/codingkiwi_/pen/XWMBRpW
假设你有一堂课:
class MyClass {
constructor(){
this.entries = ["a"];
//=== example change triggered from INSIDE the class ===
setTimeout(() => {
this.entries.push("c");
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
在组件中,您可以获得该类的一个实例:
const { reactive } = Vue;
const App = {
setup() {
const myobject = reactive(new MyClass());
//=== example change triggered from OUTSIDE the class ===
setTimeout(() => {
myobject.entries.push("b");
}, 500);
return {
myobject
};
}
}
Run Code Online (Sandbox Code Playgroud)
DOM 中的 myobject.entries 数组将显示条目"a"和"b",但不显示"c"