2017-03-17 39 views
1

如何在Vue.js組件中的mounted事件上觸發Apollo GraphQL查詢。如何在Vue組件事件中觸發Apollo查詢?

這裏是我的代碼如下所示:

<template> 
</template> 

<script> 
import gql from 'graphql-tag'; 

const myQuery = gql` 
    query someQuery { 
    books{ 
     id 
    } 
}` 

export default { 
    data:() => ({ 
    books: [] 
    }), 
    apollo: { 
    books: { 
     query: myQuery, 
    }, 
    }, 
    mounted() { 
    // run the appollo query here as well ? 
    } 
}; 
</script> 

查詢運行在組件的第一負載很好,但我怎樣才能得到它與各種事件的運行?

+0

使查詢的方法,並在事件發生時調用它。 – Bert

回答

1
this.$apollo.queries.books.refetch(); 

,如果你有變量,需要更新他們在每次調用,將它們作爲方法重取的參數:

this.$apollo.queries.books.refetch(variables);