uni.$u.http.post('player/login', {mobile: this.login.number, password: this.login.password, area: this.login.code}) .then(resp => { if(resp.code == 0) { this.login.number = ''; this.login.password = ''; try { uni.setStorageSync('token', resp.data.token); } catch (e) { console.error(e) } this.toPage('/pages/index/weapon', 'launch'); uni.$emit('afterUserLogin'); } else { this.uUtil.showToast(resp.msg); } }) .finally(() => { this.$hideLoading(); })
时间: 2023-06-19 09:09:31 浏览: 78
这段代码是使用uni-app框架中的$http模块向服务器发送一个POST请求,请求的地址是'player/login'。请求的数据是一个对象,包含三个属性:mobile、password和area。请求成功后,如果返回的code等于0,则将number和password清空,并将服务器返回的token存储到本地缓存中。然后跳转到'/pages/index/weapon'页面,并通过uni.$emit('afterUserLogin')触发一个自定义事件。如果返回的code不等于0,则显示服务器返回的错误信息。不管请求成功还是失败,最后都会执行finally中的代码,隐藏Loading图标。
相关问题
this.$emit和uni.$emit分别怎么用
this.$emit和uni.$emit都是用于在Vue或uni-app中进行事件的触发和传递。它们的用法如下:
1. this.$emit:
在Vue组件中,this.$emit用于触发一个自定义事件,并传递数据给父组件。具体用法如下:
```javascript
// 子组件
this.$emit('eventName', data);
// 父组件
<child-component @eventName="handleEvent"></child-component>
methods: {
handleEvent(data) {
// 处理接收到的数据
}
}
```
2. uni.$emit:
在uni-app中,uni.$emit用于触发一个自定义事件,并传递数据给其他页面或组件。具体用法如下:
```javascript
// 发送方页面或组件
uni.$emit('eventName', data);
// 接收方页面或组件
uni.$on('eventName', function(data) {
// 处理接收到的数据
});
```
需要注意的是,uni.$emit和uni.$on必须在同一个uni-app实例中才能正常工作。
this.$emit和uni.$emit
this.$emit和uni.$emit都是用于在Vue组件中触发自定义事件的方法。它们的用法类似,都需要传递两个参数:事件名称和要传递的数据。具体区别在于this.$emit是Vue实例的方法,而uni.$emit是uni-app框架的方法。
下面是一个使用this.$emit触发自定义事件的例子:
```javascript
// 子组件中触发自定义事件
this.$emit('my-event', 'hello world')
// 父组件中监听自定义事件
<template>
<child-component @my-event="handleEvent"></child-component>
</template>
<script>
export default {
methods: {
handleEvent(data) {
console.log(data) // 输出:hello world
}
}
}
</script>
```
下面是一个使用uni.$emit触发自定义事件的例子:
```javascript
// 子组件中触发自定义事件
uni.$emit('my-event', 'hello world')
// 父组件中监听自定义事件
<template>
<child-component></child-component>
</template>
<script>
export default {
mounted() {
uni.$on('my-event', data => {
console.log(data) // 输出:hello world
})
}
}
</script>
```