我想在一个类中两次调用api,但是当我调用另一个类时,第一个不再起作用。我该怎么办?
这是代码---> PS这只是一个请求代码。我不写渲染代码
export class Ajax1 extends Component{
constructor(props){
super(props)
this.state={
info:[],
info2:[]
}
}
async componentDidMount(){
const url="https://jsonplaceholder.typicode.com/users"
const res=await fetch(url)
const data= await res.json()
this.setState({info:data})
}
async componentDidMount(){
const url="https://jsonplaceholder.typicode.com/posts"
const res=await fetch(url)
const data= await res.json()
this.setState({info2:data})
}
componentDidMount
在同一个类中,您不应有多个函数:
async componentDidMount(){
this.fetchInfo();
this.fetchInfo2()
}
async fetchInfo(){
const url="https://jsonplaceholder.typicode.com/users"
const res=await fetch(url)
const data= await res.json()
this.setState({info:data})
}
async fetchInfo2(){
const url="https://jsonplaceholder.typicode.com/posts"
const res=await fetch(url)
const data= await res.json()
this.setState({info2:data})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句