vue移动端项目经验(三)

摘要:
在成功登录之后,跳回上一页而不是主页评论页:sendComment(){axios.get(`${common.commentapi}/comment?注意路由和路由之间的区别。使用this.$route.replace//为什么使用replace而不是push?T=${Math.random()}`}路由路由打开页面goIndex(){letgoIndex=this.$rote.resolve//这里使用解析模式。push,go模式无效。open}银行卡号每隔四个空格显示一次规则性:if{this.$toast return}使用slice或substry或substring将银行卡号分成四个空格:银行卡号:{{`${item.bankcardid.slice(0,4)}${tem.bankcardid.sice(4,8)${item.bankcardid.slice}${item.bankcardiad.slic}ice}`}}子字符串、切片之间的差异https://blog.csdn.net/qq_38047742/article/details/82144266axios设置请求头zfbpay(){letconfig={//配置请求头头:{“Content-Type”:“application/x-www-form-urlencoded;charset=UTF-8”}letparams=newURLSearchParams()参数。appendparams。附加此$轴邮递然后catch},判断它是否是微信isWechat(){letua=navigator.userAgent.toLowerCase();如果{this.iswx=true}否则{this.iswx=false;}}判断是否为iosiosCheck(){letu=navigator.userAgent;if(!!CPU.+MacOSX/)){this.isIos=true}否则{this.isIos=false}},移动终端下载文件功能注意:微信内置浏览器中没有移动终端下载功能,

登录成功后跳转回上一页而非主页

评论页:
sendComment(){
    axios.get(`${common.commentapi}/comment?aid=${this.aid}`).then(res=>{
        if(res.data.code==200){
            this.$toast({
                message:'收藏成功',
                duration:2000,	
            })
        }else if(res.data.code==401){
            Dialog.confirm({
                message: '登录后才能收藏哦',
                confirmButtonText: "去登录",
                cancelButtonText: "下次吧"
            }).then(()=>{
                this.$router.replace({    //将当前页面路由替换成登录页路由,并将当前页面路由保存在query中为后面跳转回来做准备
                    path:"/user/login",
                    query: {redirect: this.$router.currentRoute.fullPath}
                })
            }).catch(()=>{})
        }
    })
}

登录页:
login(){
    axios.post(`${common.userapi}/login`,params).then(res=>{
        if(res.data.code==200){
            this.$toast({
                message:'登录成功',
                duration:2000,	
            })
            if(this.$route.query.redirect){  //判断若是路由中保存了上一页的路由信息,则执行以下代码跳转回上一页。这里注意route与router的区别使用
                this.$router.replace({path:decodeURIComponent(this.$route.query.redirect)})  //这里为什么用replace不用push?因为直接替换路由不会产生新的history记录。以防止产生新的history记录导致相关页面的router.go(-1)出现问题
            }else{
                this.$router.push('/')  //否则则跳往首页
            }
        }
    })
}

数据轮询,使用定时器每过60秒请求一次数据

data(){
    return{
        setTimeGet:null  //轮询定时器
    }
},
created(){
    this.getData()   //初始化页面时执行函数(不设的话,则初始时不执行定时器,在60000ms后开始执行)
},
beforeDestroy() {   //关闭页面之前清除定时器
    clearInterval(this.setTimeGet)  
},
//若该页面使用了keepAlive缓存,则beforeDestroy生命周期不可用,改为deactivated生命周期
//deactivated(){
//    clearInterval(this.setTimeGet)  
//},
methods:{
    getData(){
        axios.get(`${common.demoapi}/getdemo?id=${this.rid}`).then(res=>{  //初始化页面时请求一次
            if(res.data.code==200){
                ......
            }
        })
        this.setTimeGet=setInterval(()=>{   //设置定时器,继第一次请求之后,每60秒轮询一次
            axios.get(`${common.demoapi}/getdemo?id=${this.rid}`).then(res=>{
                if(res.data.code==200){
                    ......
                }
            })
        },60000)
    },  
}

获取验证码

getVerifyCode(){
    this.verifyImg=`${common.base}/getVerification?t=${Math.random()}`
}

router路由在新窗口打开页面

goIndex(){
    let goIndex=this.$router.resolve({name:'Index'})   //这里要用resolve方式。 push,go方式无效
    window.open(goIndex.href,'_blank')
}

银行卡号每四位空格显示

银行卡号正则:
if(/^([1-9]{1})(d{11}|d{15}|d{16}|d{17}|d{18})$/.test(this.cardId)==false){
    this.$toast('请检查您的银行卡号是否正确')
    return
}
使用slice或substr或substring分割达到每4位一个空格效果:
<div class="cardId"><span>银行卡号:</span><span>{{`${item.bankcardid.slice(0,4)} ${item.bankcardid.slice(4,8)} ${item.bankcardid.slice(8,12)} ${item.bankcardid.slice(12,16)} ${item.bankcardid.slice(16,20)}`}}</span></div>

substr,substring,slice的区别

https://blog.csdn.net/qq_38047742/article/details/82144266

axios设置请求头

zfbpay(){
    let config={   //配置请求头
        headers:{
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        }
    }
    let params=new URLSearchParams()
    params.append('money',this.moneyAmount)
    params.append('ispc',2)
    this.$axios.post(`${common.orderApi}/amstc/userRechargeAccountByAliPay`,params,config).then(res=>{
        if(res.data.code==200){
            let divbody=document.createElement('div')
            divbody.innerHTML=res.data.data
            document.body.appendChild(divbody)
            document.forms[0].submit()
        }
    }).catch(err=>{})
},

判断是否是微信

isWechat() {
    let ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        this.iswx = true
    } else {
        this.iswx = false;
    }
}

判断是否是ios

iosCheck(){
    let u = navigator.userAgent;
    if (!!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/)) {
        this.isIos=true
    }else{
        this.isIos=false
    }
},

移动端下载文件功能

注:移动端下载文件功能在微信内置浏览器中不可用,需要先判断网页是否在微信中打开,判断方法如上
download(url){
    window.location.href=url
}

全局部署404(在router/index.js添加)

import NotFound from '@/pages/notFound/notFound'   //404

export default new Router({
    mode: 'history',  //去掉url中的#
    routes: [
        {
            path: '/',
            name: 'Index',
            component: Index,
            meta:{title:'首页'}
        },
        。。。
        {                     //在路由最末尾部署,记住要在最末尾
            path:'/notFound',
            name:'NotFound',
            component:NotFound,
            meta:{
                title:'页面不存在'
            }
        },
        {
            path:'*',
            redirect:'/notFound',
        }
    ]

vue获取上一页路由和当前页路由

beforeRouteEnter(to, from, next) {
    next(()=>{          
        console.log(from)   //上一页路由信息
        console.log(to)   //当前页路由信息
    })
},

input框ios端去除输入法首字母大写状态

<input type="text" autocapitalize="off" autocorrect="off" />

微信分享后返回上一页(有上一页则返回上一页,没有则返回首页)

goBack(){
    if(window.history.length==1){
        this.$router.push('/')
    }else{
        this.$router.go(-1)
    }
},

免责声明:文章转载自《vue移动端项目经验(三)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS自动化探索(十)代码覆盖率统计解决winform中mdi子窗体加载时显示最大化最小化按钮的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

解放双手!用 Python 控制你的鼠标和键盘

QQ群:1097524789那么如何使用Python来控制我们的鼠标和键盘呢?...

R包的安装 卸载 加载 移除等

R包的安装1)有四个窗口用于使用R studio手动安装R studio,所有安装的R包将显示在右下角窗口中的包下。单击安装-˃输入R包名称-˃单击安装。2) 使用命令安装安装。packagesR包,请卸载删除。packagesR包,加载libraryR包,删除...

MyBatisPlus使用

简介MyBatis Plus是MyBatis的增强工具。基于MyBatis,只进行了增强而不进行更改。它旨在简化开发并提高效率。...

【资料】2021年最网红的FPGA开发板之一——DE10-Nano (SOC FPGA入门推荐!)

DE10 Nano开发板是2021最受欢迎的FPGA开发板之一。除了广泛应用于物联网、边缘计算、硬件加速、AI和EDA教育课程之外,许多爱好者还在网络上日益流行的开源复古游戏项目Mister中使用它。让我们来看看DE10 Nano提供的材料:Youjing官方网站上的材料(中文手册可用!!!23~课程培训材料2018产学合作培训材料基于2018产学协作培训材...

Qt 调用本地浏览器打开URL

单击一些Qt控件以查找本地浏览器传递的URL以打开前端。...

java报表实现excel一样冻结表头的功能

增加了几个新的指标,后台sql改了,拿过来只须在一个dao类中修改就足够了,可恨的是客户又提出来改报表表样,加个类似excel冻结表头的功能。...