You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
864 B
44 lines
864 B
<template>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
var data = {name: 'kindeng'};
|
|
observe(data);
|
|
data.name = 'dmq'; // 哈哈哈,监听到值变化了 kindeng --> dmq
|
|
|
|
function observe(data) {
|
|
if (!data || typeof data !== 'object') {
|
|
return;
|
|
}
|
|
// 取出所有属性遍历
|
|
Object.keys(data).forEach(function(key) {
|
|
defineReactive(data, key, data[key]);
|
|
});
|
|
};
|
|
|
|
function defineReactive(data, key, val) {
|
|
observe(val); // 监听子属性
|
|
Object.defineProperty(data, key, {
|
|
enumerable: true, // 可枚举
|
|
configurable: false, // 不能再define
|
|
get: function() {
|
|
return val;
|
|
},
|
|
set: function(newVal) {
|
|
console.log('哈哈哈,监听到值变化了 ', val, ' --> ', newVal);
|
|
val = newVal;
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
export default {
|
|
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
|
|
</style> |