我試圖將axios
函數分離到單獨的服務層。請建議如何在反應js中做到這一點?react js - 如何做服務層調用
```
class xxx extends Component {
constructor(props) {
super(props)
this.state = {
ownerName: '',
}
this.handleKeyUp = this.handleKeyUp.bind(this)
}
handleKeyUp(e) {
if (e.target.value.length > 4) {
var self = this
axios.get(`/https://exampleService.com/${e.target.value}`)
.then(function (response) {
self.setState({ownerName: response.data['name']})
})
.catch(function (error) {
if (error.response) {
if (error.response.status === 404) {
self.setState({ownerName: `\u2014`})
}
}
})
}
}
render() {
return (
<div>
<Input OnKeyUp={(event) => this.handleKeyUp(event)} ></Input>
</div>
);
}
}
```
我試圖分開像下面使用module.exports
,但我無法從模塊組件的輸出,並把它傳遞給的xxx組件。
```
module.exports = {
axios.get ......
.....
}
```
謝謝!它工作得很好 – user7700138