我正在使用vue-apollo
和graphql-tag
构造一个GraphQL查询.
如果我对想要的ID进行硬编码,它可以工作,但我希望将当前路由ID作为变量传递给Vue Apollo.
Does work(硬编码ID):
apollo: {
Property: {
query: PropertyQuery,
loadingKey: 'loading',
variables: {
id: 'my-long-id-example'
}
}
}
然而,我无法做到这一点:
Doesn't work(try 访问此.ID为$route):
apollo: {
Property: {
query: PropertyQuery,
loadingKey: 'loading',
variables: {
id: this.$route.params.id
}
}
}
我得到了一个错误:
未捕获的TypeError:无法读取未定义的属性"params"
有什么办法吗?
EDIT:完整的脚本块,以便更容易地查看发生了什么:
<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>