VUE3+setup报错解决Getting a value from the `props` in root scope of `<script setup>` will cause the valu

<script lang="ts" setup>
import { computed } from 'vue';
type Props = {
  A: string;
  B: string;
};
const props = defineProps<Props>();

const A = computed(() => {
  return props.A;
});
const B = computed(() => {
  return props.B;
});
</script>

在使用props时,直接获取props的值,如const A=props.A报错Getting a value from the `props` in root scope of `<script setup>` will cause the value to lose reactivity.,那么使用计算属性就好了。

本质是Vue采用单向数据流,我们不能直接去更改props的值,需要去使用computed去接收

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和提到,在Vue 3中,如果在`<script setup>`的根作用域中从`props`中获取一个值,这个值将失去响应性。这是因为在`<script setup>`中,Vue 3使用编译时的静态编译来提高性能,而不像Vue 2那样使用运行时的响应式系统。为了解决这个问题,你可以使用`ref`函数将`props`中的值转换为响应式对象,如下所示: ```javascript import { ref } from 'vue'; export default { props: ['myProp'], setup(props) { const myPropValue = ref(props.myProp); // 使用myPropValue进行后续操作 return { myPropValue }; } } ``` 引用提到的报错`TypeError Cannot read properties of undefined (reading 'apply')`与Vue 2升级到Vue 3的路由动态授权和异步加载相关。这个错误通常是由于Vue Router的版本不兼容或配置不正确引起的。你可以尝试升级Vue Router的版本,或者检查动态授权和异步加载的相关代码是否正确配置和使用。如果问题仍然存在,你可以提供更多的代码和错误信息以便进行更详细的分析和解决123 #### 引用[.reference_title] - *1* *3* [【Vue3】报错处理合集](https://blog.csdn.net/Qxn530/article/details/126856780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值