简体   繁体   English

在来自 App.js 的 React js 如何调用另一个 javascript 文件(服务)方法来使用 API 调用

[英]In React js from App.js how to call another javascript file (service) method to consume API calls

I am new to React js, I am trying to consume REST API results.我是 React js 的新手,我正在尝试使用 REST API 结果。 So that I have written one separate file called /shared/job-service.js.这样我就编写了一个名为 /shared/job-service.js 的单独文件。 From App.js I would like to call a method from job-service.js file and return these results to UI.从 App.js 我想从 job-service.js 文件中调用一个方法并将这些结果返回给 UI。 Problem: I am unable to get the syntax to call the service js file method from the App.js file.问题:我无法从 App.js 文件中获取调用服务 js 文件方法的语法。

/shared/job-service.js /shared/job-service.js

import React, {Component} from 'react';
import Jobs from './model/Jobs';

class JobService extends Component {
    render() {
        return (
            <Jobs jobs={this.state.jobs} />
        )
    }

    state = {
        jobs: []
    };

    componentDidMount() {
        fetch('http://localhost:8080/api/v1/job/')
            .then(res => res.json())
            .then((data) => {
                this.setState({ contacts: data })
            })
            .catch(console.log)
    }
}

export default JobService;

/src/App.js /src/App.js

import React from 'react';
import { Column, Row } from 'simple-flexbox';
import { StyleSheet, css } from 'aphrodite';
import SidebarComponent from './components/sidebar/SidebarComponent';
import HeaderComponent from './components/header/HeaderComponent';
import './App.css';

const styles = StyleSheet.create({
    container: {
        height: '100vh'
    },
    content: {
        marginTop: 54
    },
    mainBlock: {
        backgroundColor: '#F7F8FC',
        padding: 30
    }
});

class App extends React.Component {

    state = { selectedItem: 'Tickets' };

    render() {
        const { selectedItem } = this.state;
        return (
            <Row className={css(styles.container)}>
                <SidebarComponent selectedItem={selectedItem} onChange={(selectedItem) => this.setState({ selectedItem })} />
                <Column flexGrow={1} className={css(styles.mainBlock)}>
                    <HeaderComponent title={selectedItem} />
                    <div className={css(styles.content)}>
                        <span>Content....</span>
         //call the job-service.js file componentDidMount() method and return the result to the UI.
                    </div>
                </Column>
            </Row>
        );
    }
}

export default App;

I would like to know the syntax to call the method from another js file and consume the results.我想知道从另一个 js 文件调用该方法并使用结果的语法。 thanks in advance.提前致谢。

Move /shared/job-service.js into /src/service/job.js移动/shared/job-service.js/src/service/job.js

Then, replace all the content on job.js by:然后,将 job.js 上的所有内容替换为:

export function getJobs() {
  return fetch('http://localhost:8080/api/v1/job/')
    .then(res => res.json())
    .then((data) => data)
}

Of course, you can add a catch, async/await.. all what you want当然,你可以添加一个catch,async/await..所有你想要的

After that, you have to make some changes on App.js, like this:之后,您必须对 App.js 进行一些更改,如下所示:

/src/App.js /src/App.js

import React from 'react';
import { Column, Row } from 'simple-flexbox';
import { StyleSheet, css } from 'aphrodite';
import SidebarComponent from './components/sidebar/SidebarComponent';
import HeaderComponent from './components/header/HeaderComponent';
import './App.css';

import { getJobs } from './service/job';

const styles = StyleSheet.create({
    container: {
        height: '100vh'
    },
    content: {
        marginTop: 54
    },
    mainBlock: {
        backgroundColor: '#F7F8FC',
        padding: 30
    }
});

class App extends React.Component {

    state = { selectedItem: 'Tickets', jobs: [] };

    componentDidMount() {
        getJobs()
        .then(data => {
            this.setState({
                jobs: data
            })
        });
    }

    render() {
        const { selectedItem } = this.state;
        return (
            <Row className={css(styles.container)}>
                <SidebarComponent selectedItem={selectedItem} onChange={(selectedItem) => this.setState({ selectedItem })} />
                <Column flexGrow={1} className={css(styles.mainBlock)}>
                    <HeaderComponent title={selectedItem} />
                    <div className={css(styles.content)}>
                        <span>Content....</span>
         //call the job-service.js file componentDidMount() method and return the result to the UI.
                    </div>
                </Column>
            </Row>
        );
    }
}

export default App;

And now you can map (Loop) this.state.jobs, in order to display jobs on Content Section现在您可以 map(循环)this.state.jobs,以便在内容部分显示作业

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

相关问题 如何从 React JS 的公共文件夹中的 App.js 文件调用 function - How to call function from App.js file inside public folder in React JS 如何在Node JS应用程序JavaScript中从index.html调用函数到app.js文件 - How to call a function from index.html to app.js file in node js application javascript 如何在 React 中将状态从组件传递到文件 app.js - How to pass state from component to file app.js in React 试图将我的 api 调用从 App.js 移动到操作文件 - tried to move my api calls from App.js to actions file 无法加载从 React/redux 中的另一个 js 文件导入的 App.js 中的函数 - can't load a function In App.js imported from another js file in React/redux React Native:如何将 props 变量从其他 js 文件传递到 app.js 文件 - React Native: How to pass a props variable from other js file to app.js file NodeJS - 来自 app.js 的第三方 Api 调用 - NodeJS - Third party Api call from app.js 如何通过app.js将另一个.js文件中所需的功能传递到index.ejs视图中 - How to pass function required from another .js file into an index.ejs view via app.js React-从根app.js文件导出无法正常工作 - React - exports from root app.js file not working Google云端硬盘上的app.js JavaScript API - app.js JavaScript API on Google Drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM