var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
function promiseForEach(arr, cb) {
let realResult = []
let result = Promise.resolve()
arr.forEach((a, index) => {
result = result.then(() => {
if (typeof cb === "function") {
return cb(a).then((res) => {
realResult.push(res)
})
}
})
})
return result.then(() => {
return realResult
})
}
promiseForEach(arr, (ele) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(ele)
return resolve(ele)
}, Math.random() * 1000)
})
}).then((data) => {
console.log("成功")
console.log(data)
}).catch((err) => {
console.log("失败")
console.log(err)
})
// refresh.vue
<template>
<div></div>
</template>
<script>
export default {
name: "refresh",
data() {
return {}
},
beforeRouteEnter(to, from, next) {
next(vm => {
vm.$router.replace({ path: from.path, query: to.query })
})
}
}
</script>
// router.vue
{
path: "/refresh",
name: "refresh",
component: () =>
import(/* webpackChunkName: "page" */ "@/components/refresh"),
meta: {
keepAlive: false,
isTab: false,
isAuth: false
}
}
// main.js (如果需要设置所有的)
router.beforeEach((to, from, next) => {
if (from.path === to.path) {
next({ path: "/refresh", query: to.query })
return
}
}
/**
* 复制文本
* @param text
*/
export function copyText(text) {
const input = document.createElement("input")
input.value = text
document.body.appendChild(input)
input.select()
document.execCommand("Copy")
window.setTimeout(function () {
document.body.removeChild(input)
}, 0)
}
/**
* esc监听全屏
*/
export const listenfullscreen = (callback) => {
function listen() {
callback()
}
document.addEventListener("fullscreenchange", function () {
listen()
})
document.addEventListener("mozfullscreenchange", function () {
listen()
})
document.addEventListener("webkitfullscreenchange", function () {
listen()
})
document.addEventListener("msfullscreenchange", function () {
listen()
})
}
/**
* 浏览器判断是否全屏
*/
export const fullscreenEnable = () => {
return document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
}
/**
* 浏览器全屏
*/
export const re
let timer = setTimeout(()=> {})
this.$once("hook:beforeDestroy", () => {
clearTimeout(timer)
timer = null
})
<template>
<div class="countDown_container">
<slot v-bind="dateData"></slot>
</div>
</template>
<script>
import dayjs from "dayjs";
export default {
name: "countDown",
props: {
computeEndTimeFlag: Boolean, // 是否计算结束时间
nowTime: {
type: String | Number,
default: () => Date.now()
},
startTime: {
type: String | Number,
},
endTime: {
type: String | Number,
required: true,
},
showDay: Boolean,
},
data() {
return {
dateData: {},
realEndTime: null,
}
},
created() {
const realEndTime = this.computeEndTimeFlag ? new Date(this.endTime).getTime() : this.getRealEndTime()
this.realEndTime = realEndTime
this.$emit("getRealEndTime", realEndTime)
this.setDateData(realEndTime - new Date().getTime())
this.updateState();
// this.$options.data()//是原始data中的数据
// this.$data //是改变后的data中的数据
this.$root //表示app.vue中data的数据
Object.assign(this.$data, this.$options.data())