react-native 点击按钮进行交互

在api/index.js里面封装了一个方法:
/ login登录接口
export const loginFromApiAsync = function (a,b) {
return fetch(URL+'/vhc/cust/api/login',{
method: "POST",
headers: {
Accept: "application/json",
"Content-type": "application/json",
"X-App-Platform": "app"
},
body: JSON.stringify({
account: a,
password: b
})

})
    .then(res => res.json())
    .then(res => {
        return res;
    })
    .catch(err => {
        console.log(err);
    })

};

在登录页面引入后使用
import {loginFromApiAsync} from "../api";

export default class Login extends Component {

constructor(props){
    super(porps);
    this.state = {
        account:''
    }
}

//定义一个方法
_loginFromApiAsync(){

//要获取state里面的数据account, 需要render里面使用函数时绑定this。如果不绑定this,this.state.account取不到
let a = this.state.account;

    loginFromApiAsync()
        .then( data => {
            //data成功后返回的数据
        }
    )
}

render(){
    <Text
        onPress = { this._loginFromApiAsync.bind(this)_}
    >点击</Text>
}

}

转载于:https://www.cnblogs.com/xk-g/p/9519133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值