简体   繁体   English

如何在独立页面中保持 Reactjs API 调用 function 并从组件调用?

[英]How to keep Reactjs API calling function in independent page and call from component?

This code is working fine now I want to move api calling function in to separate page这段代码现在工作正常我想将调用 function 的 api 移动到单独的页面

const Login = () => {
const handleSubmit = (event) => {
    axios.post('/api/login', { email: email, password: password})
    .then((result) => {
        let resultEmail = result.email;
        console.log( resultEmail )
        dispatch({ type: 'LOGIN', login: { email } }); 
        console.log(result)
    });
  }
}

=========================== ============================

Please check my code that I added below.. Problems请检查我在下面添加的代码..问题

  1. Please check any Importing syntax error请检查任何导入语法错误
  2. How can I call login function from login component?如何从登录组件调用登录 function?

    CONSOLE ERROR - TypeError: _services__WEBPACK_IMPORTED_MODULE_3__.UseApi.login is not a function控制台错误 - 类型错误:_services__WEBPACK_IMPORTED_MODULE_3__.UseApi.login 不是 function

//API services page //API服务页面

export const UseApi = () => {
    const { dispatch } = useContext(GlobalContext);

    const login = (email, password) => {
        axios.post('/api/login', { email: email, password: password})
        .then((result) => {
            let resultEmail = result.email;
            console.log( resultEmail )
            dispatch({ type: 'LOGIN', login: { email } });  
            console.log(result)
        });
    }
}




 //login component
    import UseApi from "./services" ;

    function Login() {
         const handleSubmit = (event) => {
             UseApi.login(email, password) 
         }
    };

edited: services.js编辑:services.js

import axios from "axios";
import React, { useState, useContext, useEffect } from "react";
import { GlobalContext } from "../../store/globalProvider";

const { dispatch } = useContext(GlobalContext);
const Login = (email, password) => {
  axios
    .post("/api/login", { email: email, password: password })
    .then(result => {
      let resultEmail = result.email;
      console.log(resultEmail);
      dispatch({ type: "LOGIN", login: { email } }); //dispatch function will pass the action up to BookContext
      console.log(result);
    });
};

useEffect(() => {});

export const UseApi = {
  Login
};

let me know if this works for you or not让我知道这是否适合您

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

相关问题 如何从reactJs中的另一个组件调用函数 - How to call function from another component in reactJs 从Reactjs中的函数内部调用组件函数 - Calling a component function from within a function in Reactjs ReactJS如何从同一文件上的另一个函数调用组件函数? - ReactJS how to call a component function from another function on the same file? 如何从ReactJS中的onClick函数调用另一个组件 - How to call another component from onClick function in ReactJS 如何从子组件的子组件调用处理程序函数 (ReactJS) - How to call handler function from a child of a child component (ReactJS) 如何在基于函数的 ReactJS 组件中调用函数? - How to call a function in a function based ReactJS component? 从reactjs中的两个独立组件传递数据 - Pass data from two independent component in reactjs ReactJS class 组件和功能组件如何从普通 Javascript ZC1C425268E68385D1AB50741? - How do I call ReactJS class component and functional component from normal Javascript function? 如何在 ReactJS 中将数量从一个组件传递到另一个组件,其中它们是独立的但位于同一个父级中 - How to pass quantity from component to component in ReactJS where they are independent but live in the same parent 如何避免为组件的 prop 调用相同的函数:ReactJS - How to avoid calling same function for a prop for a component : ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM