到此为止,我们只差一个退出登陆功能了。
回到index.vue页面,之前我们已经预留了退出按钮,现在给它加上功能
<button class="mybutton" type="warn" @click="logout">退出</button>
在功能区加上
logout() {
uni.removeStorageSync('UID');
uni.showToast({title: "您已经退出",icon: "none"});
setTimeout(function() {
uni.redirectTo({
url: '/pages/login/login'
});
}, 1000);
}
还是用到之前uni.removeStorageSync
完整代码如下:
index.vue
<template>
<view class="body">
<!-- 显示区正式开始 -->
<view class="myfrom">
<form @submit="goLink">
<textarea name="mytxt" class="mytext" v-model="desc" maxlength="-1" adjust-position auto-height="true"
placeholder="请输入文本" />
<view class="mybutton">
<button class="mybutton" type="primary" formType="submit">保存</button>
<button class="mybutton" type="warn" @click="logout">退出</button>
</view>
</form>
</view>
<!-- 显示区结束 -->
</view>
</template>
<script>
var _self, loginUID;
export default {
data() {
return {
desc: ''
}
},
methods: {
logout() {
uni.removeStorageSync('UID');
uni.showToast({
title: "您已经退出",
icon: "none"
});
setTimeout(function() {
uni.redirectTo({
url: '/pages/login/login'
});
}, 1000);
},
goLink() {
if (_self.desc == '') {
uni.showToast({
title: "内容必填",
icon: "none"
});
return;
}
uni.request({
url: _self.apiServer + 'add',
method: 'POST',
header: {
'content-type': "application/x-www-form-urlencoded"
},
data: {
'uid': loginUID,
'txt': _self.desc
},
success: res => {
_self.desc = '' //清空输入内容
uni.showToast({
title: res.data.datas,
icon: "none"
});
setTimeout(function() {
uni.setStorageSync("pop", 1); //给刷新标记赋值
uni.switchTab({
url: "/pages/list/list"
}); //跳到 看日记页面
}, 2000);
}
}); //golink结束
},
onLoad() {
_self = this;
loginUID = _self.checkLogin('/index/index');
if (!loginUID) {
return;
} else(console.log(loginUID));
}
}
}
</script>
<style>
.myfrom {
margin: 10px;
}
.num {
text-align: right;
color: gray;
font-size: 14px;
}
.mytext {
border: 1px solid #ccc;
border-radius: 5px;
width: 100%;
min-height: 300px;
}
.mybutton {
margin-top: 20px;
}
</style>
以上就完成了小程序的基本开发。
众所周知,微信小程序不面向个人开放,要申请一个appid必须要企业账号,那么我们个人有没有办法不花钱申请一个呢?答案是肯定的,就是申请一个小程序测试帐号。
网址如下:微信公众平台
只要用自己的微信扫一下,就会得到申请 成功,登陆后会得到appID,把它复制出来,一会儿用到

在这里,如果自己有服务器,可以只填 request就行了
回到Hbudiler中,找到 manifest.json,将申请好的appid输入就可以使用。
若想到直接在手机上测试,可下载 微信小程序 开发工具,运行到它上面,手机扫码测试,就可一直使用下去。

好了,以上就是全部内容,若觉得对学习uniapp入门有所帮忙,欢迎在文章右下方打赏。
本应用所用到的文件源程序 与数据库文件,已打包发布,若需要可整体下载。
本文介绍如何在uni-app中实现退出登录功能,并提供完整的代码示例。此外还讲解了如何申请小程序测试账号及配置appid的方法。
2128

被折叠的 条评论
为什么被折叠?



