javascript - Graphql的返回查询为空时如何在Nuxtjs中进行重定向
问题描述
我对 Nuxtjs 和 javascript 世界很陌生。我正在使用 Graphql 从后端(Strapi)获取所需数据的 Nuxtjs 项目。目标是在 Graphql 返回空查询结果时重定向到主页(index.vue)。但是,当我使用 router.push('/') 作为下面的代码时,它不起作用。
请注意,我正在尝试在结果块和挂载块上重定向。他们两个都没有工作。
请帮我指出我在这里做错了什么。提前感谢您的好意
<script>
import getProfiletQuery from '~/apollo/queries/profiles/profile'
export default {
data () {
return {
profiles: {
}
}
},
apollo: {
profiles: {
prefetch: true,
query: getProfiletQuery,
variables () {
return { user: this.$route.params.user }
},
result ({ data }) {
if (data.profiles.length === 0) {
console.log('it enters here!!!')
this.$router.push('/') //this is not working
}
}
}
},
mounted () {
console.log('this.profiles.length: ', this.profiles.length)
if (this.profiles.length === 0) {
this.$router.push('/')
}
}
}
</script>
解决方案
推荐阅读
- ios - 使用 AVPlayer 加载和播放视频很慢
- vb.net - 使用与图形绘图不兼容的扩展窗口样式控制闪烁修复?
- reactjs - GraphQL 中使用 Apollo 的动态过滤器运算符(在 React 应用程序中)
- shell - Sqlplus 脚本部分显示“Badly placed ()'s”。错误
- mysql - XAMPP - MySQL 意外关闭,即使在更改端口后也是如此
- php - Paypal Payflow 托管结帐页面
- r - 在 R 中循环遍历 glm
- linux - Linux:如何等待另一个进程阻塞 POSIX 信号量?
- jsrender - JsViews:使用深拷贝对象重新初始化模板:~root 问题
- c - 尝试写入结构数组的元素时出现分段错误