首页 > 解决方案 > How to get data into my Nuxt app for all pages/components

问题描述

I have a file called config-ingest.js in my plugins directory with the following:

import gql from 'graphql-tag'

export default ({ app }, inject) => {

  const client = app.apolloProvider.defaultClient;

  const apps = gql`
    query {
      apps: allApps {
        key
        tree
      }
    }
  `;

  client.query({ query: apps }).then(response => {
    inject('configStructure', response.data.apps);
  });

}

Although the data is successfully retrieved, it doesn't seem to be available in all my pages and components.

If I want to get data from another server that should be available to all my pages, how would I do this? I want to stick this data in it's own file so I can get the data from any component or page when I please.

标签: apiasynchronousvue.jsapollonuxt.js

解决方案


您应该为此使用 Vuex 商店。 https://nuxtjs.org/guide/vuex-store/


推荐阅读