【发布时间】:2020-09-18 20:33:36
【问题描述】:
我正在尝试将“上一个”和“下一个”按钮添加到我的 SPA。我在前端使用 Vue js,在后端使用 Laravel。 我现在遇到的问题是,当用户在切换 URL 中的 id 后单击 Prev 或 Next 按钮时,但没有加载新数据。如果您再次单击任一按钮,它们就会开始正常工作。
这是我的 SearchIndex.vue 组件:
nextCandidate: async function() {
try {
const itemId = this.$route.params.itemId;
const response = await employerService.loadCandidateProfileData(itemId);
this.nextRecord = response.data.next_record;
this.$router.push(`/employer/search/filter-by/${this.nextRecord.id}/show`);
this.loadCandidateProfileData();
} catch(error) {
this.$toast.error(error.response.data.message);
}
},
previousCandidate: async function() {
try {
const itemId = this.$route.params.itemId;
const response = await employerService.loadCandidateProfileData(itemId);
this.previousRecord = response.data.previous_record;
this.$router.push(`/employer/search/filter-by/${this.previousRecord.id}/show`);
this.loadCandidateProfileData();
} catch(error) {
this.$toast.error(error.response.data.message);
}
},
loadCandidateProfileData: async function() {
try {
const itemId = this.$route.params.itemId;
const response = await employerService.loadCandidateProfileData(itemId);
this.candidateProfiles = response.data.candidateProfiles;
} catch (error) {
this.$toast.error("Some error occurred, please refresh!");
}
},
这是我在 SearchIndex.vue 组件中的按钮:
<b-row class="mt-3 text-center mb-3">
<b-col>
<button @click="previousCandidate()"
type="button"
class="btn btn-lg btn-dark mb-3"
>
< Prev
</button>
</b-col>
<b-col>
<button @click="nextCandidate()"
type="button"
class="btn btn-lg btn-dark mb-3"
>
Next >
</button>
</b-col>
</b-row>
因此,如果它在 URL 中显示 ID 为 1 的用户并且您单击下一步,在 URL 中它会转到 2,但页面上的配置文件数据不会刷新。然后,如果您再次按下它,它会正常工作,页面上的数据会刷新并且与上一个按钮相同。这只是您第一次按下任一按钮。
【问题讨论】:
-
观察参数:
watch: {'$route.params.itemId': ...}然后重新触发 loadCandidateProfileData 方法,而不是在 previousCandidate 等中调用它 -
另外
nextCandidate和previousCandidate之间的区别是previous_record/previous_record属性,所以你最好通过它,所以调用next 两次/三/四次然后单击返回是有意义的无法正确跟踪 -
在您的第一条评论中:您希望我在哪里重新触发 loadCandidateProfileData 方法?抱歉,我是 Vue js 的新手。第二条评论:通过哪里?你能给我发个 js fiddle 的例子吗?
-
如果你创建了一个 js 小提琴,我很乐意对其进行调整 :) 如手表
watch: {'$route.params.itemId': function (){this.loadCandidateProfileData()}}中所说,那么你不需要 this.loadCandidateProfileData();在其他方法中。 -
你可以看看
response.data,如果它为空,并且结果中不是this.previousRecord.id,添加一个标志来禁用按钮,与下一个相同。
标签: vue.js pagination