{"id":99733,"date":"2024-04-12T11:46:45","date_gmt":"2024-04-12T03:46:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99733.html"},"modified":"2024-04-12T11:46:47","modified_gmt":"2024-04-12T03:46:47","slug":"%e4%bd%bf%e7%94%a8graphql-api%e5%9c%a8vue-js%e4%b8%ad%e6%9f%a5%e8%af%a2%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99733.html","title":{"rendered":"\u4f7f\u7528GraphQL API\u5728Vue.js\u4e2d\u67e5\u8be2\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02154928\/a74a37c5-b478-4c67-a843-c7912caf08b7.webp\" alt=\"\u4f7f\u7528GraphQL API\u5728Vue.js\u4e2d\u67e5\u8be2\u6570\u636e\" \/><\/p>\n<p><p>\u5728\u4f7f\u7528GraphQL API\u5728Vue.js\u4e2d\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u9996\u5148\u8981\u4e86\u89e3GraphQL\u662f\u5982\u4f55\u4f5c\u4e3a\u4e00\u79cd\u6570\u636e\u67e5\u8be2\u548c\u64cd\u4f5c\u8bed\u8a00\u6765\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u3001\u5f3a\u5927\u7684\u6570\u636e\u83b7\u53d6\u65b9\u5f0f\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u8bbe\u7f6e\u5ba2\u6237\u7aef\u3001\u5b9a\u4e49\u67e5\u8be2\u3001\u6267\u884c\u67e5\u8be2\u4ee5\u53ca\u5904\u7406\u54cd\u5e94\u3002<\/strong> \u66f4\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528GraphQL\u5ba2\u6237\u7aef\u5e93\u5982Apollo Client\u6216Vue Apollo\uff0c\u53ef\u4ee5\u5728Vue.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u67e5\u8be2\u6240\u9700\u8981\u7684\u7cbe\u786e\u6570\u636e\uff0c\u65e0\u8bba\u662f\u5355\u4e2a\u5bf9\u8c61\u8fd8\u662f\u591a\u4e2a\u8d44\u6e90\u7684\u96c6\u5408\uff0cGraphQL\u90fd\u80fd\u591f\u5e2e\u52a9\u524d\u7aef\u5de5\u7a0b\u5e08\u4ec5\u901a\u8fc7\u4e00\u4e2aAPI\u8bf7\u6c42\u83b7\u53d6\u5230\u6240\u6709\u6240\u9700\u4fe1\u606f\uff0c\u4e00\u65b9\u9762\u51cf\u5c11\u4e86\u6570\u636e\u4f20\u8f93\u91cf\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u7b80\u5316\u4e86\u524d\u7aef\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e2d\uff0c<strong>\u8bbe\u7f6e\u5ba2\u6237\u7aef<\/strong>\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u9700\u8981\u5728Vue.js\u9879\u76ee\u4e2d\u5b89\u88c5\u5e76\u914d\u7f6eGraphQL\u5ba2\u6237\u7aef\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9009\u62e9\u4f7f\u7528Apollo Client\uff0c\u901a\u5e38\u4f1a\u5f00\u59cb\u4e8e\u5b89\u88c5\u76f8\u5e94\u7684npm\u5305\u3002\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aApollo\u5ba2\u6237\u7aef\u5b9e\u4f8b\uff0c\u914d\u7f6e\u597dGraphQL\u670d\u52a1\u5668\u7684URI\u4ee5\u53ca\u5176\u4ed6\u53ef\u80fd\u7684\u8ba4\u8bc1\u4fe1\u606f\u3002\u8fd9\u4e2a\u5ba2\u6237\u7aef\u5b9e\u4f8b\u5c06\u88ab\u7528\u4e8e\u5728Vue\u7ec4\u4ef6\u4e2d\u6267\u884cGraphQL\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eGraphQL\u5ba2\u6237\u7aef<\/h3>\n<\/p>\n<p><p><strong>\u642d\u5efaGraphQL\u5ba2\u6237\u7aef\u73af\u5883<\/strong> \u662f\u6574\u4e2a\u6570\u636e\u67e5\u8be2\u8fc7\u7a0b\u7684\u57fa\u7840\u3002\u901a\u5e38\uff0c\u8fd9\u4e00\u6b65\u9aa4\u6d89\u53ca\u5230\u51e0\u4e2a\u5c0f\u4efb\u52a1\uff1a\u9996\u5148\u9700\u8981\u6839\u636e\u6240\u9009\u7684\u5ba2\u6237\u7aef\u5e93\uff08\u4f8b\u5982Apollo Client\u6216Vue Apollo\uff09\u5b89\u88c5\u76f8\u5e94\u7684npm\u5305\u3002\u5728\u5b89\u88c5\u5b8c\u6210\u4e4b\u540e\uff0c\u901a\u5e38\u9700\u8981\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u5b9e\u4f8b\uff0c\u5e76\u4e14\u914d\u7f6e\u5b83\u8fde\u63a5\u5230GraphQL\u670d\u52a1\u5668\u7684URI\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u5ba2\u6237\u7aef\u5e93<\/h4>\n<\/p>\n<p><p>\u987b\u77e5\u9053\u5982\u4f55\u5b89\u88c5GraphQL\u5ba2\u6237\u7aef\u5e93\u3002\u5bf9\u4e8eVue.js\uff0c\u901a\u5e38\u4f1a\u9009\u62e9Apollo Client\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528npm\u6216yarn\u6765\u5b89\u88c5Apollo\u5ba2\u6237\u7aef\u548c\u4e0eVue.js\u96c6\u6210\u6240\u9700\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code>npm install @apollo\/client graphql<\/p>\n<p>\/\/ or<\/p>\n<p>yarn add @apollo\/client graphql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u548c\u914d\u7f6eApollo\u5ba2\u6237\u7aef<\/h4>\n<\/p>\n<p><p>\u521b\u5efaApollo\u5ba2\u6237\u7aef\u5b9e\u4f8b\u662f\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\uff0c\u9700\u8981\u6307\u5b9aGraphQL\u670d\u52a1\u7684URI\uff0c\u5e76\u53ef\u80fd\u8fd8\u9700\u8bbe\u7f6e\u5176\u4ed6\u914d\u7f6e\uff0c\u5982\u8ba4\u8bc1\u4fe1\u606f\u3001\u7f13\u5b58\u7b56\u7565\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">import { ApolloClient, InMemoryCache, createHttpLink } from &#039;@apollo\/client&#039;;<\/p>\n<p>const httpLink = createHttpLink({<\/p>\n<p>  uri: &#039;YOUR_GRAPHQL_API_ENDPOINT&#039;,<\/p>\n<p>});<\/p>\n<p>const client = new ApolloClient({<\/p>\n<p>  link: httpLink,<\/p>\n<p>  cache: new InMemoryCache(),<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49GraphQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u5728Vue.js\u4e2d\u67e5\u8be2\u6570\u636e\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49GraphQL\u67e5\u8be2\u3002\u8fd9\u4e9b\u67e5\u8be2\u662fGraphQL\u7279\u5b9a\u7684\u67e5\u8be2\u8bed\u8a00\uff08GraphQL query language\uff09\u6240\u5199\uff0c\u5b83\u4eec\u6620\u5c04\u5230Vue.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u67e5\u8be2\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u6784\u5efa\u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u4f60\u9700\u8981\u6e05\u695a\u81ea\u5df1\u60f3\u8981\u83b7\u53d6\u54ea\u4e9b\u6570\u636e\uff0c\u5e76\u4e14\u5229\u7528GraphQL\u7684\u7c7b\u578b\u7cfb\u7edf\u6765\u786e\u4fdd\u53ea\u67e5\u8be2\u4f60\u9700\u8981\u7684\u6570\u636e\u3002\u8fd9\u65b9\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\u67e5\u8be2\u53ef\u80fd\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-graphql\">query GetBooks {<\/p>\n<p>  books {<\/p>\n<p>    id<\/p>\n<p>    title<\/p>\n<p>    author {<\/p>\n<p>      name<\/p>\n<p>      id<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u67e5\u8be2\u94a9\u5b50<\/h4>\n<\/p>\n<p><p>\u5728GraphQL\u5ba2\u6237\u7aef\u5e93\u4e2d\uff0c\u5b58\u5728\u7740\u4e13\u4e3aVue.js\u8bbe\u8ba1\u7684\u94a9\u5b50\uff08hooks\uff09\u6216\u9009\u9879\uff08options\uff09\uff0c\u5b83\u4eec\u53ef\u4ee5\u8ba9\u4f60\u66f4\u5bb9\u6613\u5730\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u5b9a\u4e49\u597d\u7684\u67e5\u8be2\u3002\u6bd4\u5982\uff0c\u5728Apollo Client\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u7528\u5230<code>useQuery<\/code>\u94a9\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">import { useQuery, gql } from &#039;@apollo\/client&#039;;<\/p>\n<p>const GET_BOOKS = gql`<\/p>\n<p>  query GetBooks {<\/p>\n<p>    books {<\/p>\n<p>      id<\/p>\n<p>      title<\/p>\n<p>      author {<\/p>\n<p>        name<\/p>\n<p>        id<\/p>\n<p>      }<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>`;<\/p>\n<p>export default {<\/p>\n<p>  setup() {<\/p>\n<p>    const { loading, error, data } = useQuery(GET_BOOKS);<\/p>\n<p>    \/\/ ...\u5176\u4ed6\u903b\u8f91<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884cGraphQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u597d\u67e5\u8be2\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5728Vue\u7ec4\u4ef6\u6216\u89c6\u56fe\u4e2d\u6267\u884c\u8fd9\u4e9b\u67e5\u8be2\uff0c\u5e76\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u5728\u7ec4\u4ef6\u4e2d\u67e5\u8be2\u6570\u636e<\/h4>\n<\/p>\n<p><p>GraphQL\u5ba2\u6237\u7aef\u7684\u6267\u884c\u67e5\u8be2\u901a\u5e38\u6d89\u53ca\u5230\u5728Vue.js\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u4e2d\u8c03\u7528\u67e5\u8be2\u51fd\u6570\u3002\u4f60\u9700\u8981\u5904\u7406\u6570\u636e\u52a0\u8f7d\u65f6\u7684\u72b6\u6001\u663e\u793a\uff0c\u5982\u52a0\u8f7d\u6307\u793a\u5668\uff0c\u4ee5\u53ca\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">export default {<\/p>\n<p>  name: &#039;Books&#039;,<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      loading: true,<\/p>\n<p>      error: null,<\/p>\n<p>      books: null,<\/p>\n<p>    };<\/p>\n<p>  },<\/p>\n<p>  async created() {<\/p>\n<p>    try {<\/p>\n<p>      const { data } = aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t client.query({<\/p>\n<p>        query: GET_BOOKS,<\/p>\n<p>      });<\/p>\n<p>      this.books = data.books;<\/p>\n<p>    } catch (error) {<\/p>\n<p>      this.error = error;<\/p>\n<p>    } finally {<\/p>\n<p>      this.loading = false;<\/p>\n<p>    }<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u7f13\u5b58\u4f18\u5316\u6027\u80fd<\/h4>\n<\/p>\n<p><p>GraphQL\u5ba2\u6237\u7aef\u901a\u5e38\u5305\u542b\u6570\u636e\u7f13\u5b58\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6539\u8fdb\u6027\u80fd\u3002Apollo Client\u5c31\u63d0\u4f9b\u4e86<code>InMemoryCache<\/code>\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u672c\u5730\u7f13\u5b58\u67e5\u8be2\u7ed3\u679c\uff0c\u51cf\u5c11\u91cd\u590d\u7684\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u54cd\u5e94\u548c\u6570\u636e\u7ed1\u5b9a<\/h3>\n<\/p>\n<p><p>\u5f53\u67e5\u8be2\u6267\u884c\u5b8c\u6bd5\uff0c\u4f60\u4f1a\u6536\u5230GraphQL\u670d\u52a1\u5668\u7684\u54cd\u5e94\u6570\u636e\u3002\u6b64\u65f6\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230Vue\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u540e\uff0c\u6b63\u786e\u5904\u7406\u5b83\u4eec\u5bf9\u4e8e\u5c55\u793a\u6570\u636e\u81f3\u5173\u91cd\u8981\u3002\u4f60\u9700\u8981\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7684\u60c5\u51b5\uff0c\u5982\u6570\u636e\u52a0\u8f7d\u4e2d\u3001\u67e5\u8be2\u9519\u8bef\u4ee5\u53ca\u6570\u636e\u6210\u529f\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">export default {<\/p>\n<p>  name: &#039;BooksList&#039;,<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      loading: true,<\/p>\n<p>      error: null,<\/p>\n<p>      books: [],<\/p>\n<p>    };<\/p>\n<p>  },<\/p>\n<p>  created() {<\/p>\n<p>    client.query({ query: GET_BOOKS })<\/p>\n<p>      .then(result =&gt; {<\/p>\n<p>        this.books = result.data.books;<\/p>\n<p>        this.loading = false;<\/p>\n<p>      })<\/p>\n<p>      .catch(error =&gt; {<\/p>\n<p>        this.error = error;<\/p>\n<p>        this.loading = false;<\/p>\n<p>      });<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u636e\u7ed1\u5b9a\u81f3\u89c6\u56fe<\/h4>\n<\/p>\n<p><p>Vue.js\u975e\u5e38\u64c5\u957f\u5c06\u6570\u636e\u7ed1\u5b9a\u81f3\u89c6\u56fe\u5c42\u3002\u4e0d\u7ba1\u662f\u901a\u8fc7\u6a21\u677f\u8bed\u6cd5\u8fd8\u662f\u4f7f\u7528\u6e32\u67d3\u51fd\u6570\uff0c\u4e00\u65e6\u4f60\u7684\u6570\u636e\u51c6\u5907\u59a5\u5f53\uff0c\u4f60\u5c31\u53ef\u4ee5\u5c06\u5176\u5c55\u793a\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">&lt;template&gt;<\/p>\n<p>  &lt;div&gt;<\/p>\n<p>    &lt;div v-if=&quot;loading&quot;&gt;Loading...&lt;\/div&gt;<\/p>\n<p>    &lt;div v-if=&quot;error&quot;&gt;An error occurred!&lt;\/div&gt;<\/p>\n<p>    &lt;ul v-if=&quot;books&quot;&gt;<\/p>\n<p>      &lt;li v-for=&quot;book in books&quot; :key=&quot;book.id&quot;&gt;{{ book.title }}&lt;\/li&gt;<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5728Vue.js\u5e94\u7528\u4e2d\u4f7f\u7528GraphQL\u67e5\u8be2\u6570\u636e\u4e0d\u4ec5\u53ef\u4ee5\u51cf\u5c11\u524d\u540e\u7aef\u4e4b\u95f4\u7684\u6c9f\u901a\u590d\u6742\u5ea6\uff0c\u540c\u65f6\u4e5f\u8ba9\u6570\u636e\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u7684\u76f4\u89c2\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Vue.js\u4e2d\u4f7f\u7528GraphQL API\u67e5\u8be2\u6570\u636e\uff1f<\/strong><\/p>\n<p>GraphQL\u662f\u4e00\u79cd\u7528\u4e8eAPI\u67e5\u8be2\u548c\u6570\u636e\u64cd\u4f5c\u7684\u67e5\u8be2\u8bed\u8a00\u3002\u8981\u5728Vue.js\u4e2d\u4f7f\u7528GraphQL API\u67e5\u8be2\u6570\u636e\uff0c\u60a8\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5728Vue.js\u9879\u76ee\u4e2d\u5b89\u88c5graphql\u548cvue-apollo\u63d2\u4ef6\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aGraphQL\u5ba2\u6237\u7aef\uff0c\u901a\u5e38\u4f7f\u7528Apollo Client\uff0c\u5e76\u5c06\u5176\u914d\u7f6e\u4e3a\u8fde\u63a5\u5230\u60a8\u7684GraphQL API\u3002<\/li>\n<li>\u5728Vue\u7ec4\u4ef6\u4e2d\uff0c\u4f7f\u7528Apollo Client\u6765\u5b9a\u4e49\u67e5\u8be2\u5e76\u5c06\u5176\u4e0eVue\u6a21\u677f\u7ed1\u5b9a\u3002<\/li>\n<li>\u4f7f\u7528Vue\u6a21\u677f\u548c\u6570\u636e\u7ed1\u5b9a\u6765\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Vue.js\u4e2d\u4f7f\u7528GraphQL API\u67e5\u8be2\u6570\u636e\u4e86\u3002<\/p>\n<p><strong>2. Vue.js\u4e2d\u5982\u4f55\u5411GraphQL API\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\uff1f<\/strong><\/p>\n<p>\u8981\u5411GraphQL API\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Apollo Client\u5e93\u6765\u521b\u5efa\u4e00\u4e2aGraphQL\u5ba2\u6237\u7aef\uff0c\u5e76\u4f7f\u7528\u8be5\u5ba2\u6237\u7aef\u7684<code>query<\/code>\u51fd\u6570\u6765\u53d1\u9001\u67e5\u8be2\u3002\u5728Vue.js\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\uff1a<\/p>\n<ul>\n<li>\u5b89\u88c5apollo-client\u548cgraphql\u4f9d\u8d56\u9879\u3002<\/li>\n<li>\u914d\u7f6eApollo Client\u4ee5\u8fde\u63a5\u5230\u60a8\u7684GraphQL API\u3002<\/li>\n<li>\u5728Vue\u7ec4\u4ef6\u4e2d\u4f7f\u7528<code>apollo<\/code>\u6a21\u5757\uff0c\u5e76\u901a\u8fc7<code>this.$apollo<\/code>\u8bbf\u95eeGraphQL\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u4f7f\u7528<code>this.$apollo.query<\/code>\u51fd\u6570\u53d1\u9001\u60a8\u7684GraphQL\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Vue.js\u4e2d\u5411GraphQL API\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\u4e86\u3002<\/p>\n<p><strong>3. \u5728Vue.js\u4e2d\u5982\u4f55\u5904\u7406GraphQL API\u7684\u67e5\u8be2\u54cd\u5e94\uff1f<\/strong><\/p>\n<p>\u5f53\u60a8\u5411GraphQL API\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\u540e\uff0c\u60a8\u5c06\u83b7\u5f97\u4e00\u4e2a\u67e5\u8be2\u54cd\u5e94\u3002\u5728Vue.js\u4e2d\u5904\u7406GraphQL API\u7684\u67e5\u8be2\u54cd\u5e94\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528Apollo Client\u7684<code>query<\/code>\u51fd\u6570\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\uff0c\u5e76\u5728\u54cd\u5e94\u5b8c\u6210\u540e\u63a5\u6536\u54cd\u5e94\u6570\u636e\u3002<\/li>\n<li>\u901a\u8fc7\u5728Vue\u7ec4\u4ef6\u7684\u6a21\u677f\u4e2d\u4f7f\u7528<code>v-if<\/code>\u548c<code>v-for<\/code>\u7b49Vue\u6307\u4ee4\uff0c\u5c06\u54cd\u5e94\u6570\u636e\u5c55\u793a\u5230\u9875\u9762\u4e0a\u3002<\/li>\n<li>\u5728\u54cd\u5e94\u6570\u636e\u6539\u53d8\u65f6\uff0cVue\u4f1a\u81ea\u52a8\u66f4\u65b0\u9875\u9762\u4e0a\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u67e5\u8be2\u54cd\u5e94\u5305\u542b\u9519\u8bef\u4fe1\u606f\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u663e\u793a\u4e3a\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528Apollo Client\u7684\u5176\u4ed6\u529f\u80fd\uff0c\u4f8b\u5982\u7f13\u5b58\u6570\u636e\u548c\u5b9e\u65f6\u67e5\u8be2\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Vue.js\u4e2d\u5904\u7406GraphQL API\u7684\u67e5\u8be2\u54cd\u5e94\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u4f7f\u7528GraphQL API\u5728Vue.js\u4e2d\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u9996\u5148\u8981\u4e86\u89e3GraphQL\u662f\u5982\u4f55\u4f5c\u4e3a\u4e00\u79cd\u6570\u636e\u67e5\u8be2\u548c\u64cd\u4f5c\u8bed [&hellip;]","protected":false},"author":3,"featured_media":99738,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99733"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=99733"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/99738"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=99733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=99733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=99733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}