ES6模块实现,如何加载一个json文件 [英] ES6 modules implementation, how to load a json file

查看:76
本文介绍了ES6模块实现,如何加载一个json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施来自 https://github.com/moroshko/react-autosuggest

重要代码如下:

import React, { Component } from 'react';
import suburbs from 'json!../suburbs.json';

function getSuggestions(input, callback) {
  const suggestions = suburbs
    .filter(suburbObj => suburbMatchRegex.test(suburbObj.suburb))
    .sort((suburbObj1, suburbObj2) =>
      suburbObj1.suburb.toLowerCase().indexOf(lowercasedInput) -
      suburbObj2.suburb.toLowerCase().indexOf(lowercasedInput)
    )
    .slice(0, 7)
    .map(suburbObj => suburbObj.suburb);

  // 'suggestions' will be an array of strings, e.g.:
  //   ['Mentone', 'Mill Park', 'Mordialloc']

  setTimeout(() => callback(null, suggestions), 300);
}

示例中的此复制粘贴代码(有效)在我的项目中存在错误:

This copy-paste code from the example (that works), has an error in my project:

Error: Cannot resolve module 'json' in /home/juanda/redux-pruebas/components

如果我去掉前缀json!:

If I take out the prefix json!:

import suburbs from '../suburbs.json';

这样我在编译时就没有错误(导入完成).但是当我执行它时出现错误:

This way I got not errors at compile time (import is done). However I got errors when I execute it:

Uncaught TypeError: _jsonfilesSuburbsJson2.default.filter is not a function

如果我调试它,我可以看到郊区是一个 objectc,而不是一个数组,因此未定义过滤器函数.

If I debug it I can see suburbs is an objectc, not an array so filter function is not defined.

但是在示例中被注释的建议是一个数组.如果我像这样重写建议,一切正常:

However in the example is commented suggestions is an array. If I rewrite suggestions like this, everything works:

  const suggestions = suburbs
  var suggestions = [ {
    'suburb': 'Abbeyard',
    'postcode': '3737'
  }, {
    'suburb': 'Abbotsford',
    'postcode': '3067'
  }, {
    'suburb': 'Aberfeldie',
    'postcode': '3040'
  } ].filter(suburbObj => suburbMatchRegex.test(suburbObj.suburb))

所以......什么json!导入中的前缀是做什么的?

So... what json! prefix is doing in the import?

为什么我不能把它放在我的代码中?一些 babel 配置?

Why can't I put it in my code? Some babel configuration?

推荐答案

首先需要安装<代码>json-loader:

First of all you need to install json-loader:

npm i json-loader --save-dev

然后,您可以通过两种方式使用它:

Then, there are two ways how you can use it:

  1. 为了避免添加json-loader在每个 import 你可以添加到 webpack.config 这一行:

  1. In order to avoid adding json-loader in each import you can add to webpack.config this line:

loaders: [
  { test: /.json$/, loader: 'json-loader' },
  // other loaders 
]

然后像这样导入json文件

import suburbs from '../suburbs.json';

  • 直接在您的 json-loader 中使用 code>import,如您的示例所示:

    import suburbs from 'json!../suburbs.json';
    

  • 注意:webpack 2.* 而不是关键字 loaders 需要使用 rules.,

    webpack 2.* 默认使用 json-loader

    *.json 文件现在无需 json-loader 即可支持.你仍然可以使用它.这不是一个重大变化.

    *.json files are now supported without the json-loader. You may still use it. It's not a breaking change.

    v2.1.0-beta.28

    这篇关于ES6模块实现,如何加载一个json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆