0


Vue--为什么data是个函数--实例讲解

原文网址:Vue--为什么data是个函数--实例讲解_IT利刃出鞘的博客-CSDN博客

简介

说明

  1. 本文用示例介绍Vue中的data设计为函数的原因。
  2. 分别用Vue和原生JavaScript进行展示。

结论

  1. 对象是一个引用数据类型,如果data是一个对象会造成所有组件共用一个data。若data是一个函数,每次函数都会返回一个新的对象,这样每个组件都会维护一份独立的对象(data)。
  2. 根实例对象data可以是对象也可以是函数(根实例是单例),不会产生数据污染情况。

官网

组件基础 — Vue.js

问题引出

  1. 大家都知道,Vue的组件一般是下边这样写的。
  2. 可以看到,data是个函数,那么为什么不写成对象呢?就像这样:data: { msg: 'Hello'}
  1. <template>
  2. <div class="hello">
  3. hello world
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'Demo',
  9. data () {
  10. return {
  11. msg: 'Hello'
  12. }
  13. }
  14. }
  15. </script>
  16. <style scoped>
  17. </style>

实例:使用Vue创建计数器

例1:data为函数

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>this is title</title>
  6. </head>
  7. <body>
  8. <div id="components-demo">
  9. <button-counter></button-counter>
  10. <button-counter></button-counter>
  11. <button-counter></button-counter>
  12. </div>
  13. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  14. <script>Vue.config.productionTip = false</script>
  15. <script>
  16. // 定义一个名为 button-counter 的新组件
  17. Vue.component('button-counter', {
  18. data: function () {
  19. return {
  20. count: 0
  21. }
  22. },
  23. template: '<button v-on:click="count++">你点击了 {{ count }} 次</button>'
  24. })
  25. new Vue({
  26. el: '#components-demo'
  27. })
  28. </script>
  29. </body>
  30. </html>

结果:每个计数器单独计数

例2:data为对象

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>this is title</title>
  6. </head>
  7. <body>
  8. <div id="components-demo">
  9. <button-counter></button-counter>
  10. <button-counter></button-counter>
  11. <button-counter></button-counter>
  12. </div>
  13. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  14. <script>Vue.config.productionTip = false</script>
  15. <script>
  16. // 定义一个名为 button-counter 的新组件
  17. Vue.component('button-counter', {
  18. data: {
  19. count: 0
  20. },
  21. template: '<button v-on:click="count++">你点击了 {{ count }} 次</button>'
  22. })
  23. new Vue({
  24. el: '#components-demo'
  25. })
  26. </script>
  27. </body>
  28. </html>

结果:直接报错

是因为新版的Vue2直接进行了检测,如果不是函数,就会报错。

如果是旧版的Vue2,那么结果是:点击了一个按钮,所有按钮都会增加次数(它们共享data里的count这个属性)。

实例:原生JS

例1:data为函数

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>This is title</title>
  6. </head>
  7. <body>
  8. <div class="container">
  9. 这是个Demo
  10. </div>
  11. <script>
  12. function MyComponent() {
  13. this.data = this.data();
  14. }
  15. MyComponent.prototype.data = function() {
  16. return {
  17. age: 12
  18. }
  19. };
  20. let user1 = new MyComponent();
  21. let user2 = new MyComponent();
  22. console.log(user1.data === user2.data) // false
  23. user1.data = {age: 13};
  24. console.log('user1的age:' + user1.data.age); //user1的age:13
  25. console.log('user2的age:' + user2.data.age); //user2的age:12
  26. </script>
  27. </body>
  28. </html>

结果:输出的结果不同

例2:data为对象

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>This is title</title>
  6. </head>
  7. <body>
  8. <div class="container">
  9. 这是个Demo
  10. </div>
  11. <script>
  12. function MyComponent() {
  13. }
  14. MyComponent.prototype.data = {
  15. age: 12
  16. }
  17. let user1 = new MyComponent();
  18. let user2 = new MyComponent();
  19. console.log(user1.data === user2.data) // false
  20. user1.data = {age: 13};
  21. console.log('user1的age:' + user1.data.age); //user1的age:13
  22. console.log('user2的age:' + user2.data.age); //user2的age:12
  23. </script>
  24. </body>
  25. </html>

结果:输出的结果相同

其他网址

Vue 组件 data 为什么必须是函数(分析源码,找到答案)_Jioho的博客-CSDN博客


本文转载自: https://blog.csdn.net/feiying0canglang/article/details/122334962
版权归原作者 IT利刃出鞘 所有, 如有侵权,请联系我们删除。

“Vue--为什么data是个函数--实例讲解”的评论:

还没有评论