1

In this sample:

<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>

what's the scope of tmp? Is it handled similarly to other entries in data() or not? or it doesn't matter.

2
  • 2
    tmp is a local variable. It is not an entry in data(). It is completely invisible to Vue. Commented Jun 22, 2021 at 12:35
  • That makes intuitive sense; there is a "caveat" though: stackoverflow.com/a/53757193/1079483 Commented Jun 22, 2021 at 12:58

1 Answer 1

1

I believe tmpis only accessible from inside the handler function since you used let to declare it.

You should declare it directly in the data object to use it anywhere in the component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.