vue将函数封装,借用公共的方式
原创封装接口并调用通用方法
1.调用通用方法
1.1首先插入接口。api文件定义如下。
在api下面
export function getDownValue(type) { return request({ url: /project-mgt/project/v1.0/getDownValue?type= + type, method: get }) }
在utils/index.js
import { getDownValue } from "@/code/projectSelection/api/projectSelectionApi"
export function dictionaries(that, key, code) {
getDownValue(code).then(res => {
if (res.data) {
that[key] = [{ name: "全部", value: "" }]
that[key] = [...that[key], ...res.data]
console.log("that[key]", that[key]);
}
});
}
1.2 在vue页面使用
import { dictionaries } from "@/utils";
export default {
name: "",
data() {
// isLike:
return {
pojectScheduleList:[],
},
},
created() {
let that = this;
dictionaries(that, "pojectScheduleList","project_type");//project_type为传参,pojectScheduleList此页面的数据
},
}
2.调用公共格式时间的方法。
2.1.定义
在utils/index.js
export function parseTime(time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || {y}-{m}-{d} {h}:{i}:{s}
let date
if (typeof time === object) {
date = time
} else {
if ((typeof time === string)) {
if ((/^[0-9]+$/.test(time))) {
// support "1548221490638"
time = parseInt(time)
} else {
time = time.replace(new RegExp(/-/gm), /)
}
}
if ((typeof time === number) && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === a) { return [日, 一, 二, 三, 四, 五, 六][value] }
return value.toString().padStart(2, 0)
})
return time_str
}
2.2使用
import { parseTime } from "@/utils";
this.fillTime = parseTime(new Date(), "{y}-{m}-{d}");
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除