【问题标题】:Scope of locally defined variables in vuevue中局部定义变量的作用域
【发布时间】:2021-06-22 12:22:15
【问题描述】:

在此示例中:

<template>
  <div>
    <p 
      v-for="prop in receivedPropsLocal"
      :key="prop.id"
    >
        {{prop}}
    </p>
  </div>
</template>

<script>

export default {
  name: "PropsReceiver",
  props: {
    receivedProps: {        
      required: true,
      type: Array,      
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      receivedPropsLocal: Array,
    };
  },
  methods: {
  },
  watch: {
    receivedProps: {
      deep: true,
      handler(val) {
        let tmp = Object.entries(Object.assign({}, val));
        this.receivedPropsLocal = tmp;
      },
    },
  },
  computed: {
    getReceivedPropsLocal: {
      get() {
        if (!this.receivedPropsLocal) {
          let tmp = Object.entries(Object.assign({}, this.receivedProps));
          this.receivedPropsLocal = tmp;
          return this.receivedPropsLocal;
        }
        return this.receivedPropsLocal;
      },
      set(value) {
        this.receivedPropsLocal = value;
      },
    },
  },
};
</script>

tmp 的范围是什么?它的处理方式是否与data() 中的其他条目类似?或者没关系。

【问题讨论】:

标签: javascript vue.js vuejs2 vue-component


【解决方案1】:

我相信 tmp 只能从处理函数内部访问,因为您使用了 let 来声明它。

您应该直接在data 对象中声明它以在组件的任何位置使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多