【发布时间】:2016-11-04 04:52:40
【问题描述】:
我有以下函数,可以调用 api 并获取数据。
export function getUserData(url){
return fetch(url, { credentials : 'include'})
.then((response) => { return response.json()})
.then((data) =>{ return new User(data.uid, data.givenName, data.familyName)})
}
这是我的用户对象。
class User{
constructor(uid, givenName, familyName){
this.uid = yguid;
this.givenName = givenName;
this.familyName = familyName;
}
}
export default User;
现在在我的 react 组件中,我正在尝试实例化 User 对象。
class Header extends React.Component {
constructor(props) {
super(props);
this.state = { fullName: null, yguid: null };
}
componentWillMount(){
user = getUserData(url);
console.log(user);
//set state with User data
}
我的 React 组件中有以下导入语句。
import User from '../models/user';
import '../api/util';
我的用户和函数 getUserData 都未定义。我做错了什么,我该如何解决。提前致谢。
【问题讨论】:
-
url 参数从何而来?您是否尝试过通过破坏导入您的功能?
import { getUserData } from '../api/util';?
标签: javascript reactjs fetch-api