在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 在react項(xiàng)目中,無法讀取SimpleSample.json文件,一直找不到怎

在react項(xiàng)目中,無法讀取SimpleSample.json文件,一直找不到怎么解決?

Parent01.js

import React, { Component } from 'react';
import $ from 'jquery';

import constantData from './data/SimpleSample.json';

class Parent01 extends Component {
    constructor(props) {
        super(props)

    }

    Ad = () => {
        alert('cac');
        /*
        console.log("constantData  taype is ="+typeof(constantData));
        console.log("employees  taype is ="+typeof(constantData.employees));
        console.log("employees  length = "+constantData.employees.length);
        console.log("No.1 givenName ="+constantData.employees[0].giveName);
        console.log("No.1 FamilyName ="+constantData.employees[0].giveName);
        console.log("No.1 Salary"+constantData.employees[0].salary);
        console.log("type of No.1 Salary"+typeof(constantData.employees[0].salary));
        */

        var test;
        if(window.XMLHttpRequest){
            test = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            test = new window.ActiveXObject();
        }else{
            alert("請升級至最新版本的瀏覽器");
        }
        if(test !=null){
            test.open("GET","./data/SimpleSample.json",true);
            test.send(null);
            test.onreadystatechange=function(){
                if(test.readyState==4&&test.status==200){
                    var obj = JSON.parse(test.responseText);
                    alert(obj);
                }
            };

        }
    }

    render() {
        return (
            <div>
                <table>
                    <tbody>
                        <tr data-resourceuuid="201612121641078700726">
                            <td width="30%">2</td>
                            <td width="35%">3</td>
                            <td width="30%"><a href="#" onClick={this.Ad}>點(diǎn)擊</a></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        )
    }
}


export default Parent01;

data文件下的SimpleSample.json文件

{
  "employees": [
    {
      "FamilyName": "張",
      "giveName": "三",
      "salary": 1
    },
    {
      "FamilyName": "李",
      "giveName": "四",
      "salary": 2
    },
    {
      "FamilyName": "王",
      "giveName": "二",
      "salary": 3
    }
  ]
}

圖片描述

回答
編輯回答
野橘

webpack 里給 json 文件配個(gè) loader,或者改SimpleSample.jsonSimpleSample.js, 內(nèi)容為:

export default {
    ... your json
}
2018年6月3日 09:58
編輯回答
孤巷

json擴(kuò)展名有l(wèi)oader嗎?

2018年4月27日 15:54