我可以在react.xml中定义一个非Component类吗?我正在尝试定义一个类来管理 api 请求,但无法配置字段(我在 BaseUrl 值上收到未定义的错误)
class fetchFromAPI {
constructor(){
//super();
this.BaseURL = "myUrlvalue";
}
fetchConfig (payload){
return {
method: 'POST',
body: JSON.stringify(payload),
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
};
}
fetchUse (/*componentThis,*/command,options){
var fullUrl = this.BaseURL+command;
return fetch(fullUrl, this.fetchConfig(options));
}
fetchResponse (data,onSucessFunc,onSucessJson,onFailureFunc,onFailureJson){
if(data.response==1){
onSucessFunc(data,onSucessJson);
}
else{
onFailureFunc(data,onFailureJson);
}
}
}
export default fetchFromAPI;
Run Code Online (Sandbox Code Playgroud)
我使用课程的方式
class App extends React.Component{
constructor(props){
super(props);
const payload = {
sender: "senderId"
};
var myAPI = new fetchFromAPI();
var myRequest = myAPI.fetchConfig(payload);
var …Run Code Online (Sandbox Code Playgroud)