Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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
Javascript 获取错误您可能忘记从文件it';在中定义,或者您可能混淆了默认导入和命名导入_Javascript_Reactjs_Typescript_Chart.js - Fatal编程技术网

Javascript 获取错误您可能忘记从文件it';在中定义,或者您可能混淆了默认导入和命名导入

Javascript 获取错误您可能忘记从文件it';在中定义,或者您可能混淆了默认导入和命名导入,javascript,reactjs,typescript,chart.js,Javascript,Reactjs,Typescript,Chart.js,您能帮我解决这个问题吗?我正在尝试在reactjs中使用chartjs来呈现图表。 这是我的密码: 文件chart.component.js import React, { Component } from 'react'; import { Pie } from 'react-chartjs-2'; export default class Chart extends Component { constructor(props) { super(props); th

您能帮我解决这个问题吗?我正在尝试在reactjs中使用chartjs来呈现图表。 这是我的密码:

文件chart.component.js

import React, { Component } from 'react';
import { Pie } from 'react-chartjs-2';

export default class Chart extends Component {
  
  constructor(props) {
    super(props);
    this.state = {
      chartData: props.chartData
    }
    console.log(JSON.stringify(this.state.chartData))
  }

  static defaultProps = {
    displayTitle: true,
    displayLegend: true,
    legendPosition: 'right',
    location: 'City'
  }

  render() {

    return (

      <div className="chart">
        <Pie
          data={this.state.chartData}
          options={{
            title:{
              display:this.props.displayTitle,
              text:'Largest Cities In '+this.props.location,
              fontSize:25
            },
            legend:{
              display:this.props.displayLegend,
              position:this.props.legendPosition
            }
          }}
        />
      </div>
    )
  }
}
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import AuthService from "../services/auth.service";
import Chart from './chart.component';

export default class Home extends Component {

  constructor(props) {
    super(props);
    this.state = {
      chartData: {}
    }
  }

  componentWillMount() {
    // this.getchartData(); // this should be this.getChartData();
    this.getChartData();
    
  }

  getChartData() {
    // Ajax calls here
    this.setState({
      chartData: {
        labels: ['Boston', 'Worcester', 'Springfield', 'Lowell', 'Cambridge', 'New Bedford'],
        datasets: [
          {
            label: 'Population',
            data: [
              617594,
              181045,
              153060,
              106519,
              105162,
              95072
            ],
            backgroundColor: [
              'rgba(255, 99, 132, 0.6)',
              'rgba(54, 162, 235, 0.6)',
              'rgba(255, 206, 86, 0.6)',
              'rgba(75, 192, 192, 0.6)',
              'rgba(153, 102, 255, 0.6)',
              'rgba(255, 159, 64, 0.6)',
              'rgba(255, 99, 132, 0.6)'
            ]
          }
        ]
      }
    });

    
  }

  render() {
    return (
      <div className="container">
        <Chart chartData={this.state.chartData} location="Massachusetts" legendPosition="bottom" />
      </div>
    );
  }
  
}
import React,{Component}来自'React';
从'react-chartjs-2'导入{Pie};
导出默认类图表扩展组件{
建造师(道具){
超级(道具);
此.state={
chartData:props.chartData
}
log(JSON.stringify(this.state.chartData))
}
静态defaultProps={
显示标题:正确,
是的,
legendPosition:'对',
地点:'城市'
}
render(){
返回(
)
}
}
home.component.js

import React, { Component } from 'react';
import { Pie } from 'react-chartjs-2';

export default class Chart extends Component {
  
  constructor(props) {
    super(props);
    this.state = {
      chartData: props.chartData
    }
    console.log(JSON.stringify(this.state.chartData))
  }

  static defaultProps = {
    displayTitle: true,
    displayLegend: true,
    legendPosition: 'right',
    location: 'City'
  }

  render() {

    return (

      <div className="chart">
        <Pie
          data={this.state.chartData}
          options={{
            title:{
              display:this.props.displayTitle,
              text:'Largest Cities In '+this.props.location,
              fontSize:25
            },
            legend:{
              display:this.props.displayLegend,
              position:this.props.legendPosition
            }
          }}
        />
      </div>
    )
  }
}
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import AuthService from "../services/auth.service";
import Chart from './chart.component';

export default class Home extends Component {

  constructor(props) {
    super(props);
    this.state = {
      chartData: {}
    }
  }

  componentWillMount() {
    // this.getchartData(); // this should be this.getChartData();
    this.getChartData();
    
  }

  getChartData() {
    // Ajax calls here
    this.setState({
      chartData: {
        labels: ['Boston', 'Worcester', 'Springfield', 'Lowell', 'Cambridge', 'New Bedford'],
        datasets: [
          {
            label: 'Population',
            data: [
              617594,
              181045,
              153060,
              106519,
              105162,
              95072
            ],
            backgroundColor: [
              'rgba(255, 99, 132, 0.6)',
              'rgba(54, 162, 235, 0.6)',
              'rgba(255, 206, 86, 0.6)',
              'rgba(75, 192, 192, 0.6)',
              'rgba(153, 102, 255, 0.6)',
              'rgba(255, 159, 64, 0.6)',
              'rgba(255, 99, 132, 0.6)'
            ]
          }
        ]
      }
    });

    
  }

  render() {
    return (
      <div className="container">
        <Chart chartData={this.state.chartData} location="Massachusetts" legendPosition="bottom" />
      </div>
    );
  }
  
}
import React,{Component}来自“React”;
从“react router dom”导入{Redirect};
从“./services/auth.service”导入AuthService;

从“/Chart.component”导入图表; 导出默认类Home extends组件{ 建造师(道具){ 超级(道具); 此.state={ 图表数据:{} } } 组件willmount(){ //this.getchartData();//这应该是this.getchartData(); 这是.getChartData(); } getChartData(){ //Ajax在这里调用 这是我的国家({ 图表数据:{ 标签:[“波士顿”、“伍斯特”、“斯普林菲尔德”、“洛威尔”、“剑桥”、“新贝德福德”], 数据集:[ { 标签:“人口”, 数据:[ 617594, 181045, 153060, 106519, 105162, 95072 ], 背景颜色:[ "rgba(255,99,132,0.6)",, "rgba(54162235,0.6)",, "rgba(255,206,86,0.6)",, "rgba(751921920.6)",, “rgba(153102255,0.6)”, "rgba(255,159,64,0.6)",, 'rgba(255,99,132,0.6)' ] } ] } }); } render(){ 返回( ); } }
在App.js中使用了Home组件

import React, { Component } from "react";
import { Switch, Route, Link } from "react-router-dom";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";

import AuthService from "./services/auth.service";

import Login from "./components/login.component";
import Register from "./components/register.component";
import Home from "./components/home.component";

class App extends Component {
  constructor(props) {
    super(props);
    this.logOut = this.logOut.bind(this);

    this.state = {
      currentUser: undefined,
    };
  }

  componentDidMount() {
    const user = AuthService.getCurrentUser();

    if (user) {
      this.setState({
        currentUser: user,
      });
    }
  }

  logOut() {
    AuthService.logout();
  }

  render() {
    const { currentUser} = this.state;

    return (
      <div>
        <nav className="navbar navbar-expand navbar-dark bg-dark">
          <div className="navbar-nav mr-auto">
          </div>
          {currentUser ? (
            <div className="navbar-nav ml-auto">
              <li className="nav-item">
                <Link to={"/profile"} className="nav-link">
                  Welcome, {currentUser.username}
                </Link>
              </li>
              <li className="nav-item">
                <a href="/login" className="nav-link" onClick={this.logOut}>
                  LogOut
                </a>
              </li>
            </div>
          ) : (
              <div className="navbar-nav ml-auto">
                <li className="nav-item">
                  <Link to={"/login"} className="nav-link">
                    Login
                </Link>
                </li>

                <li className="nav-item">
                  <Link to={"/register"} className="nav-link">
                    Sign Up
                </Link>
                </li>
              </div>
            )}
        </nav>

        <div className="container mt-3">
          <Switch>
            <Route exact path={["/", "/login"]} component={Login} />
            <Route exact path="/register" component={Register} />
            <Route exact path="/home" component={Home} />
          </Switch>
        </div>
      </div>
    );
  }
}

export default App;
import React,{Component}来自“React”;
从“react router dom”导入{Switch,Route,Link};
导入“bootstrap/dist/css/bootstrap.min.css”;
导入“/App.css”;
从“/services/auth.service”导入AuthService;
从“/components/Login.component”导入登录名;
从“/components/Register.component”导入寄存器;
从“/components/Home.component”导入主页;
类应用程序扩展组件{
建造师(道具){
超级(道具);
this.logOut=this.logOut.bind(this);
此.state={
当前用户:未定义,
};
}
componentDidMount(){
const user=AuthService.getCurrentUser();
如果(用户){
这是我的国家({
当前用户:用户,
});
}
}
注销(){
AuthService.logout();
}
render(){
const{currentUser}=this.state;
返回(
{当前用户(
  • 欢迎,{currentUser.username}
  • ) : (
  • 登录
  • 注册
  • )} ); } } 导出默认应用程序;
    登录后,它应该重定向到主页

    我的登录码:

    import React, { Component } from "react";
    import Form from "react-validation/build/form";
    import Input from "react-validation/build/input";
    import CheckButton from "react-validation/build/button";
    
    import AuthService from "../services/auth.service";
    
    const required = value => {
      if (!value) {
        return (
          <div className="alert alert-danger" role="alert">
            This field is required!
          </div>
        );
      }
    };
    
    export default class Login extends Component {
      constructor(props) {
        super(props);
        this.handleLogin = this.handleLogin.bind(this);
        this.onChangeUsername = this.onChangeUsername.bind(this);
        this.onChangePassword = this.onChangePassword.bind(this);
    
        this.state = {
          username: "",
          password: "",
          loading: false,
          message: ""
        };
      }
    
      onChangeUsername(e) {
        this.setState({
          username: e.target.value
        });
      }
    
      onChangePassword(e) {
        this.setState({
          password: e.target.value
        });
      }
    
      handleLogin(e) {
        e.preventDefault();
    
        this.setState({
          message: "",
          loading: true
        });
    
        this.form.validateAll();
    
        if (this.checkBtn.context._errors.length === 0) {
          AuthService.login(this.state.username, this.state.password).then(
            () => {
              this.props.history.push("/home");
              window.location.reload();
            },
            error => {
              const resMessage =
                (error.response &&
                  error.response.data &&
                  error.response.data.message) ||
                error.message ||
                error.toString();
    
              this.setState({
                loading: false,
                message: resMessage
              });
            }
          );
        } else {
          this.setState({
            loading: false
          });
        }
      }
    
      render() {
        return (
          <div className="col-md-12">
            <div className="card card-container">
              <img
                src="//ssl.gstatic.com/accounts/ui/avatar_2x.png"
                alt="profile-img"
                className="profile-img-card"
              />
    
              <Form
                onSubmit={this.handleLogin}
                ref={c => {
                  this.form = c;
                }}
              >
                <div className="form-group">
                  <label htmlFor="username">Username</label>
                  <Input
                    type="text"
                    className="form-control"
                    name="username"
                    value={this.state.username}
                    onChange={this.onChangeUsername}
                    validations={[required]}
                  />
                </div>
    
                <div className="form-group">
                  <label htmlFor="password">Password</label>
                  <Input
                    type="password"
                    className="form-control"
                    name="password"
                    value={this.state.password}
                    onChange={this.onChangePassword}
                    validations={[required]}
                  />
                </div>
    
                <div className="form-group">
                  <button
                    className="btn btn-primary btn-block"
                    disabled={this.state.loading}
                  >
                    {this.state.loading && (
                      <span className="spinner-border spinner-border-sm"></span>
                    )}
                    <span>Login</span>
                  </button>
                </div>
    
                {this.state.message && (
                  <div className="form-group">
                    <div className="alert alert-danger" role="alert">
                      {this.state.message}
                    </div>
                  </div>
                )}
                <CheckButton
                  style={{ display: "none" }}
                  ref={c => {
                    this.checkBtn = c;
                  }}
                />
              </Form>
            </div>
          </div>
        );
      }
    }
    
    import React,{Component}来自“React”;
    从“反应验证/构建/表单”导入表单;
    从“反应验证/构建/输入”导入输入;
    从“react validation/build/button”导入CheckButton;
    从“./services/auth.service”导入AuthService;
    const required=值=>{
    如果(!值){
    返回(
    此字段必填!
    );
    }
    };
    导出默认类登录扩展组件{
    建造师(道具){
    超级(道具);
    this.handleLogin=this.handleLogin.bind(this);
    this.onChangeUsername=this.onChangeUsername.bind(this);
    this.onChangePassword=this.onChangePassword.bind(this);
    此.state={
    用户名:“”,
    密码:“”,
    加载:false,
    信息:“
    };
    }
    onChangeUsername(e){
    这是我的国家({
    用户名:e.target.value
    });
    }
    onChangePassword(e){
    这是我的国家({
    密码:e.target.value
    });
    }
    handleLogin(e){
    e、 预防默认值();
    这是我的国家({
    消息:“”,
    加载:正确
    });
    this.form.validateAll();
    if(this.checkBtn.context.\u errors.length==0){
    AuthService.login(this.state.username,this.state.password)。然后(
    () => {
    this.props.history.push(“/home”);
    window.location.reload();
    },
    错误=>{
    常量消息=
    (错误。响应&&
    error.response.data&&
    错误。响应。数据。消息)||
    错误消息||
    错误:toString();
    这是我的国家({
    加载:false,
    信息:resMessage
    });
    }
    );
    }否则{
    这是我的国家({
    加载:错误
    });
    }
    }
    render(){
    返回(
    {
    这个形式=c;
    }}
    >
    用户名
    密码
    {this.state.loading&&(
    )}
    登录
    {this.state.message&&(