动态请求数据时,有的数据并不是请求后直接渲染到页面上,而是转换成字符串显示到页面上
1. 数字应该如何转成字符串?
请求到的数据是0, 1,2 之类的状态数字,不要在 methods 中写方法把其转换成字符串,因为这样会把请求到的数据转换成字符串,所以最好直接在页面上用三目运算符转换
<template slot-scope="scope">
<span>{{scope.row.status == "0" ? "待审核" : (scope.row.status == "1" ? "审核未通过" : (scope.row.status == "2" ? "审核通过" : " "))}}</span>
</template>
2. 时间戳如何转化?
请求到的时间戳最好用方法先转换成时间再渲染到页面
先引入js
import { user, formatDate } from “…/ServerApi/apis.js”;
js中时间戳的转换
export const formatDate = (str, type) => {
let oDate = new Date(str),
oYear = oDate.getFullYear(),
oMonth = oDate.getMonth() + 1,
oDay = oDate.getDate(),
oHour = oDate.getHours();
let oMin = oDate.getMinutes(),
oSen = oDate.getSeconds(),
oTime = oYear + “-” + oMonth + “-” + oDay; // 最后拼接时间
oMonth = oMonth >= 10 ? oMonth : “0” + oMonth;
oHour = oHour >= 10 ? oHour : “0” + oHour;
oMin = oMin >= 10 ? oMin : “0” + oMin;
oSen = oSen >= 10 ? oSen : “0” + oSen;
oDay = oDay >= 10 ? oDay : “0” + oDay;
if (type && type == 2) {
return (
oYear + “-” + oMonth + “-” + oDay + " " + oHour + “:” + oMin + “:” + oSen
);
}
return oTime;
};
这是 methods 中的方法
if (res.data.ERRORCODE === “0”) {
that.pageList = getBorrowByPage.list;
for(let i = 0, len = that.pageList.length; i < len; i++){
console.log(that.pageList[i].status,“123”);
that.pageList[i].applyDateShow = formatDate(that.pageList[i].applyDat
e, 2);
}
that.pageTotal = res.data.RESULT.total;
}
这是我曾经遇到过得坑,希望可以帮到看到这篇文章的你。
有兴趣的小伙伴可以关注我的公众号,以后遇到问题也相互讨论共同进步呦!