简体   繁体   English

如何在React中加载外部js文件?

[英]How to load external js file in React?

I have a data.js file with the following structure: 我有一个data.js文件,具有以下结构:

var DATA= {
  "groups": [{
    "id": "1",
    "name": "group 1",
    "subgroups": [{}, {}, ...]
  }, ...],
  ...
}

Is it possible to load this file in my React app and make the DATA var available in any component (I will probably want to initiate Redux state with it), if I can't use export in this file and can't turn it into regular JSON file? 是否可以在我的React应用程序中加载此文件并在任何组件中使DATA var可用(我可能希望用它启动Redux状态),如果我不能在此文件中使用export并且无法将其转换为常规的JSON文件? Basically I can't edit this file. 基本上我无法编辑此文件。 How can it be achieved? 如何实现?

You can definitely use export in .js file. 你绝对可以在.js文件中使用export。 export default or module.exports should work. export defaultmodule.exports应该有效。 If you aren't using a language transformer then use the later approach. 如果您没有使用语言转换器,请使用后面的方法。

var DATA = {
  "groups": [{
    "id": "1",
    "name": "group 1",
    "subgroups": [{}, {}, ...]
  }, ...],
  ...
}

module.exports = DATA;

Otherwise convert the file into json format and directly use it. 否则将文件转换为json格式并直接使用它。

{
  "groups": [{
    "id": "1",
    "name": "group 1",
    "subgroups": [{}, {}, ...]
  }, ...],
  ...
}

React app file.js: 反应app file.js:

import data from './data.json';

// do something with data.groups

As I understand it, you just have a file with this exact structure and are looking for a way to access it in your app. 据我所知,你只有一个具有这种确切结构的文件,并且正在寻找一种在你的应用程序中访问它的方法。 Since it uses a var declaration, you can just include it in your web app via a <script> tag and DATA variable will be globally available in your code. 由于它使用var声明,您可以通过<script>标记将其包含在Web应用程序中,并且DATA变量将在您的代码中全局可用。 If you use TypeScript or FlowType, you should also provide a declaration file with declare var DATA: Data; 如果使用TypeScript或FlowType,还应提供declare var DATA: Data;的声明文件declare var DATA: Data; where Data would be your custom interface describing the structure of this data object. 其中Data将是描述此数据对象结构的自定义接口。 What I would suggest instead, though, is creating a single module that will consume the variable, provide a declaration in this module only, transform it however I like and export it from there. 不过,我建议的是创建一个单独的模块,它将使用变量,仅在此模块中提供声明,然后转换它,然后从那里导出它。 Just to give you an idea what it would look like: 只是为了让你知道它会是什么样子:

interface Data {
    groups: Group[];
}

interface Group {
    id: string;
    name: string;
    subgroups: {}[];  // you might provide more type information here, but to begin it would do as well
}

declare var DATA: Data;

const APP_DATA: Data = DATA;
export default APP_DATA;

Assuming: 假设:

  • The data.js file is available during build time data.js文件在构建期间可用
  • File has content in pre-defined format 文件具有预定义格式的内容
  • You are using webpack to bundle your application(Optional) 您正在使用webpack捆绑您的应用程序(可选)

You can use webpack raw-loader (Other bundlers probably have alternatives) to load the content of the file and then parse the content to get the required data. 您可以使用webpack raw-loader (其他捆绑包可能有替代方法)来加载文件的内容,然后解析内容以获取所需的数据。

Example 👇 例子👇

编辑1o92xyn41q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM