//TestFun.js
const b = {zhihu: 0}
let a = new Proxy({}, {
set: function (obj, prop, value) {
obj[prop] = value;
if (prop == 'zhihu') {
// console.log("set " + prop + ": " + obj[prop]);
let nnn = new MyClass()
nnn.jjj(value)
b.zhihu = value
console.log('b:' + b.zhihu)
}
return true;
}
});
export class MyClass {
async inc(count) {
for (let i = 0; i < 30; i++) {
for (let j = 0; j < 30; j++) {
count += i + j
}
a.zhihu = i
}
return count;
}
async jjj(value){
// console.log(value)
b.zhihu = Math.random()
console.log(b.zhihu)
// return value
}
}
export{
b
}
//main
import MyWorker from 'comlink-loader!../../TestFun'
import {b} from '../../TestFun.js'
export default {
data() {
return {
counter: 0,
continuedCount: 0,
value: {a:0},
arr: []
}
},
methods: {
async countP1() {
const inst = new MyWorker();
const objj = await new inst.MyClass();
await objj.jjj()
console.log(b.zhihu)
},
async continuedCountP() {
const count = this.continuedCount;
const inst = new MyWorker();
const obj = await new inst.MyClass();
this.continuedCount = await obj.inc(count)
this.value = await obj.jjj()
}
},
watch: {
counter(oldval, newval) {
console.log(oldval + ',' + newval)
},
'value.a':{
handler(val){
console.log("value:" + val)
}
},
arr:{
handler(val){
console.log(val)
},
// deep: true
},
'b.zhihu':{
handler(val){
console.log(val)
},
deep: true
},
},
}
想实现js文件中的某些变量发生变动后,会触发Vue页面中的相应事件。
如有大佬能够解答,非常感激!