如题!
<div v-for="course in courses" class="col-md-3">
<div v-for="teacher in teachers.filter(t=>{return t.id===course.teacherId})" class="profile-activity clearfix">
<div>
<img v-show="!teacher.image" class="pull-left" src="/assets/images/avatars/avatar5.png">
<img v-show="teacher.image" class="pull-left" v-bind:src="teacher.image">
<a class="user" href="#"> {{teacher.name || '暂无名称'}} </a>
<br>
{{teacher.position || '暂无职位'}}
</div>
</div>
</div>
<script>
setup() {
const courses = ref();
let teachers=ref();
const allCourse = () => {
axios.get("/course/all").then((response) => {
const data=response.data;
if (data.success){
courses .value = data.content;
}else {
Toast.warning(data.message);
}
})
};
const allTeacher = () => {
axios.get("/teacher/all").then((response) => {
const data=response.data;
if (data.success){
teachers.value = data.content;
}else {
Toast.warning(data.message);
}
})
};
onMounted(() => {
allCourse();
allTeacher();
});
return{
courses,
teachers
}
</script>
从teachers数组中过滤出符合条件的teacher对象放在页面中。
用 computed 去进行过滤
直接去遍历computed 不要再模板上直接写过滤函数 这种行为不太规范
再有你的teachers ref() 是一个undefined 改成 teachers ref([])
teachers 打印一下 看看 有没有 或者 是不是 数组类型 。
建议 还是在 在methods 或者 生命周期里处理 数据 过滤数据 。