index.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <view class="u-no-demo-here">
  7. 源对象1为:"{info: {name: 'mary'}}"
  8. <view class="">
  9. </view>
  10. 源对象2为:"{info: {age: '22'}}"
  11. </view>
  12. <view class="u-demo-result-line">
  13. {{result}}
  14. </view>
  15. </view>
  16. </view>
  17. <view class="u-config-wrap">
  18. <view class="u-config-title u-border-bottom">
  19. 参数配置
  20. </view>
  21. <view class="u-config-item">
  22. <view class="u-item-title">模式</view>
  23. <u-subsection :list="['浅拷贝', '深拷贝']" @change="modeChange"></u-subsection>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. obj1: {
  33. info: {
  34. name: 'mary'
  35. }
  36. },
  37. obj2: {
  38. info: {
  39. age: '22'
  40. }
  41. },
  42. // obj1和obj3一样,原因是Object.assign(this.obj1, this.obj2)会修改obj1的值
  43. obj3: {
  44. info: {
  45. name: 'mary'
  46. }
  47. },
  48. result: ''
  49. }
  50. },
  51. onLoad() {
  52. this.result = Object.assign(this.obj1, this.obj2);
  53. // 重新修改obj1为原来的值
  54. this.obj1 = this.$u.deepClone(this.obj3);
  55. },
  56. methods: {
  57. modeChange(index) {
  58. if(!index) {
  59. this.result = Object.assign(this.obj1, this.obj2);
  60. // 重新修改obj1为原来的值
  61. this.obj1 = this.$u.deepClone(this.obj3);
  62. } else {
  63. this.result = this.$u.deepMerge(this.obj1, this.obj2);
  64. }
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. .u-demo {}
  71. </style>