数据代理:通过一个对象代理对另一个对象属性的操作(读或写)
代码如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>数据代理</title> <script type="text/javascript" src="../Js/vue.js"></script> </head> <body> <script type="text/javascript"> let obj = { x: 100 } let obj2 = { y: 200 } // 注意区分大小写 Object.defineProperty(obj2, 'x', { get() { return obj.x }, set(value) { obj.x = value } }) </script> </body> </html>
示例二:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue 数据代理</title> <script type="text/javascript" src="../Js/vue.js"></script> </head> <body> <!-- id 容器 root --> <div id="root"> <h1>姓名: {{name}}</h1> <h1>地址: {{address}}</h1> </div> </body> </html> <script type="text/javascript"> const vm = new Vue({ el: '#root', data: { name: '心怡宝贝', address: '陕西省/西安市' } }) console.log(vm) </script>
标签:10,Vue,obj,--,代理,vm,obj2,let From: https://www.cnblogs.com/YYkun/p/18027465