Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 如何让Vue(Vue cli 3)正确处理GraphQL文件?_Webpack_Vue.js_Graphql_Vue Cli - Fatal编程技术网

Webpack 如何让Vue(Vue cli 3)正确处理GraphQL文件?

Webpack 如何让Vue(Vue cli 3)正确处理GraphQL文件?,webpack,vue.js,graphql,vue-cli,Webpack,Vue.js,Graphql,Vue Cli,我有一个新的基于vue cli 3的项目,它在src/文件夹中有.graphql文件,例如: #import "./track-list-fragment.graphql" query ListTracks( $sortBy: String $order: String $limit: Int $nextToken: String ) { listTracks( sortBy: $sortBy order: $order limit: $limit

我有一个新的基于vue cli 3的项目,它在
src/
文件夹中有
.graphql
文件,例如:

#import "./track-list-fragment.graphql"

query ListTracks(
  $sortBy: String
  $order: String
  $limit: Int
  $nextToken: String
) {
  listTracks(
    sortBy: $sortBy
    order: $order
    limit: $limit
    nextToken: $nextToken
  ) {
    items {
      ...TrackListDetails
    }
    nextToken
  }
}
当我运行
纱线服务
时,它抱怨没有GraphQL的加载程序:

Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
> #import "./track-list-fragment.graphql"
|
| query ListTracks(
但是我的
vue.config.js
设置正确(我认为):

如何修复此问题?

此方法有效

const path = require('path');

module.exports = {
  pluginOptions: {
    i18n: {
      locale: 'en',
      fallbackLocale: 'en',
      localeDir: 'locales',
      enableInSFC: false,
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        $element: path.resolve(
          'node_modules/element-ui/packages/theme-chalk/src/main.scss'
        ),
      },
    },
  },
  chainWebpack: config => {
    config.module
      .rule('graphql')
      .test(/\.graphql$/)
      .use('graphql-tag/loader')
      .loader('graphql-tag/loader')
      .end();
  },
};
我很确定这不是你想要的。webpack文档提到这用于从webpack 1迁移到webpack 2。这不是我们在这里要做的

以下是在中配置加载程序的情况:

按照此方法并假设我正确理解,下面是我如何使用原始示例的数据配置Vue 3应用程序:

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            { loader: 'style-loader' },
            {
              loader: 'css-loader',
              options: {
                modules: true
              }
            }
          ]
        }
      ]
    }
  }
}
现在,我们需要配置graphql加载程序而不是css加载程序:

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.graphql$/,
          use: 'graphql-tag/loader'
        }
      ]
    }
  }
}
这是未经测试的,我只是对webpack和Vue文档的理解有所偏差。我没有一个项目来测试这一点,但如果你发布一个链接到你的项目,我会非常乐意测试

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            { loader: 'style-loader' },
            {
              loader: 'css-loader',
              options: {
                modules: true
              }
            }
          ]
        }
      ]
    }
  }
}
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.graphql$/,
          use: 'graphql-tag/loader'
        }
      ]
    }
  }
}