vue中局部定义变量的作用域

Scope of locally defined variables in vue

在此示例中:

<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() 中的其他条目类似?或者没关系。

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

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