【发布时间】:2017-12-19 09:21:00
【问题描述】:
我正在使用vue-apollo 和graphql-tag 构建一个GraphQL 查询。
如果我硬编码我想要的 ID,它可以工作,但我想将当前路由 ID 作为变量传递给 Vue Apollo。
有效(硬编码 ID):
apollo: {
Property: {
query: PropertyQuery,
loadingKey: 'loading',
variables: {
id: 'my-long-id-example'
}
}
}
但是,我无法做到这一点:
不起作用(尝试访问 this.$route 获取 ID):
apollo: {
Property: {
query: PropertyQuery,
loadingKey: 'loading',
variables: {
id: this.$route.params.id
}
}
}
我得到错误:
未捕获的类型错误:无法读取未定义的属性“参数”
有没有办法做到这一点?
编辑:完整的脚本块,以便更轻松地查看正在发生的事情:
<script>
import gql from 'graphql-tag'
const PropertyQuery = gql`
query Property($id: ID!) {
Property(id: $id) {
id
slug
title
description
price
area
available
image
createdAt
user {
id
firstName
lastName
}
}
}
`
export default {
name: 'Property',
data () {
return {
title: 'Property',
property: {}
}
},
apollo: {
Property: {
query: PropertyQuery,
loadingKey: 'loading',
variables: {
id: this.$route.params.id // Error here!
}
}
}
}
</script>
【问题讨论】:
-
@VamsiKrishna 感谢您的回复。这是一个位于默认导出上的对象。我已经更新了我的问题以在上下文中显示这一点。
标签: javascript vue.js graphql vue-apollo