javascript - 如何在 vue 中将这些数据从 console.log(element.faculty_name) 显示到模板?
问题描述
我有显示MySQL主表数据的功能。我想通过与下面的教师表的 id 进行比较来显示该专业的类型。我已将其显示在 console.log 上,如何将其显示在模板上?
模板标签
<td>
{{ filterFaculty }}
</td>
脚本标签
data() {
return {
majors:[],
faculties:[],
form: new Form({
major_id:'',
major_code:'',
major_name:'',
major_faculty:'',
major_status: '',
}),
};
},
computed: {
filterFaculty() {
for(let i in this.majors) {
this.faculties.forEach((element) => {
if(element.faculty_code==this.majors[i].major_faculty) {
console.log(element.faculty_name);
}else {
return '-';
}
});
}
}
},
mounted() {
this.fetchFaculties();
this.fetchMajors();
},
methods: {
fetchFaculties(page_url) {
let vm = this;
page_url = '../../api/admin/edu-faculty/faculty/faculty';
fetch(page_url)
.then(res => res.json())
.then(res => {
this.faculties = res.data;
})
.catch(err => console.log(err));
},
fetchMajors(page_url) {
let vm = this;
page_url = '../../api/admin/edu-major/major/'+this.currentEntries+'?page='+this.pagination.current_page;
fetch(page_url)
.then(res => res.json())
.then(res => {
this.majors = res.data;
this.pagination = res.meta;
})
.catch(err => console.log(err));
},
}
解决方案
有不同的方法可以实现这一点,我认为最简单的方法是执行以下操作:
在你的数据中定义一些东西——比如inputText
然后将你设置element.faculty_name
为这个——如下所示:
data() {
return {
inputText: "",
}
}
computed: {
filterFaculty() {
for(let i in this.majors) {
this.faculties.forEach((element) => {
if(element.faculty_code==this.majors[i].major_faculty) {
this.inputText = element.faculty_name; //Changed here
}else {
return '-';
}
});
}
}
},
而不是像这样在您的模板中引用它:
<td>
{{ inputText }}
</td>
这应该可以解决您的问题!
推荐阅读
- vue.js - 用于在 Vue 脚本键之间强制执行空行的 Eslint 规则
- java - 如何在 Java netbeans 中解决这种性质的启动画面/表单问题
- java - Kafka Streams, Process API 和 Schedule using Key
- c# - 为什么 StringSplitOptions 在 C# 中用 Flags 属性标记?
- python - 当['href']元素是超链接时如何提取href
- javascript - 跨越所有集合的文本搜索
- cypress - Transition 和 Cypress 的意外行为
- android - 如何在 MVVM 架构中使用来自 onActivityResult 的数据?
- html-email - html-emails - 如果我添加图像,电子邮件发送将不起作用
- sql - 从 2 个 case 语句中相互扣除值