2

How to prepare a property for template? This code throws an error:

<script setup>
  ...
  const props = defineProps<{ datails: TData }>();
  const value = props.datails.value; // i dont want to use long paths in the template
</script>

<template>
  <div>
    <a>{{ value }}</a>
...

The error: error Getting a value from the 'props' in root scope of will cause the value to lose reactivity vue/no-setup-props-destructure

So how to short paths for data binding in the template please?

1

1 Answer 1

1

This is how you quickly convert props to refs:

<script setup>
  import { toRefs } from 'vue';
  const props = defineProps({ details: String });
  
  const { details } = toRefs(props);
</script>

<template>
  <div>
    <a>{{ details }}</a>
  </div>
</template>

And the error you are getting could be prevented by just by writing ref(props.datails.value) so the object won't loose reactivity.

Watch out for the usage of defineProps

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.