在vue3+js中使用Class类对象
单独将一个类作为一个js文件,然后引入到vue3页面使用
// @/utils/RecordEntity.js
class RecordEntity {
entity;
constructor(entity) {
this.entity = entity;
}
getTime() {
return this.formatDate(this.entity.start_timestamp)
}
/**
* 格式化时间戳
* @param date
* @returns {string}
*/
formatDate(date) {
let time = date;
let dateTime = new Date(time).toJSON();
return new Date(+new Date(dateTime) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
}
export default RecordEntity
使用的vue页面
<template>
<div>Hello World</div>
</template>
<script setup>
import RecordEntity from "@/utils/RecordEntity.js";
// 使用类
const updateMap = (data) => {
let recordEntity = {
start_timestamp: 1696946167
};
let e = new RecordEntity(recordEntity)
console.log("实体类结果time:",e.getTime())
}
</script>