如何為模板準備屬性?此代碼引發錯誤:
<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>
...
錯誤:
error Getting a value from the 'props' in root scope of will cause the value to lose reactivity vue/no-setup-props-destructure
那么如何在模板中縮短資料系結的路徑呢?
uj5u.com熱心網友回復:
這是您快速將 props 轉換為 refs 的方法:
<script setup>
import { toRefs } from 'vue';
const props = defineProps({ details: String });
const { details } = toRefs(props);
</script>
<template>
<div>
<a>{{ details }}</a>
</div>
</template>
你得到的錯誤可以通過撰寫來防止,ref(props.datails.value)這樣物件就不會失去反應性。
注意defineProps的使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467034.html
