【发布时间】:2021-12-19 15:52:51
【问题描述】:
我正在使用 vuejs 编写应用程序,我想将 prop 传递给子组件,但出现此错误:
从
setup()根范围内的props获取值将导致该值失去响应性
父组件
<template>
<div>
<course-list :courseId = "id" />
</div>
</template>
import {useRoute} from 'vue-router';
import { ref, onMounted, reactive} from 'vue';
export default defineComponent({
components: { NavBar, CourseList, CourseContent },
props:{
courseId: String
},
setup(){
const route = useRoute()
const id = route.params.id
console.log("the course id is", id);
return{
id
}
}
}
子组件
export default defineComponent({
components: { CourseTopic },
props: {
courseId: {
type: String
}
},
setup(props) {
const trainingCourseId = props.courseId;
return { courses, trainingCourseId };
},
});
我该如何解决这个问题?
【问题讨论】:
-
这可能是您的解决方案吗? Why is this Vue prop not reacting to change?。如果没有,请分享上面提到的错误消息。
-
@PeterKrebs 错误消息在问题中,但由于格式不佳而有些隐藏。我已经解决了。
标签: vue.js vuejs3 reactive vue-props