Package Exports
- vue-timer-countdown
- vue-timer-countdown/dist/vue-timer-countdown.cjs.js
- vue-timer-countdown/dist/vue-timer-countdown.esm.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-timer-countdown) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
vue-timer-countdown
Vue@2的倒计时组件
- npm:https://www.npmjs.com/package/vue-timer-countdown
- demo:https://realgeoffrey.github.io/vue-timer-countdown/demo/index.html
安装
Node.js安装
npm install vue-timer-countdown --save
浏览器引用
<!-- 需要先引入vue:<script src="//unpkg.com/vue@2"></script> --> <script src="//unpkg.com/vue-timer-countdown"></script>
注册组件
Node.js注册
全局注册
import Vue from 'vue' import vueTimerCountdown from 'vue-timer-countdown' // 全局注册 Vue.use(vueTimerCountdown, { component: 'TimerCountdown' }) // 组件名默认是:timer-countdown // 或:Vue.component('TimerCountdown', vueTimerCountdown)
局部注册
import vueTimerCountdown from 'vue-timer-countdown' export default { components: { // 局部注册 TimerCountdown: vueTimerCountdown } }
浏览器注册
全局注册
<!-- 需要先引入vue:<script src="//unpkg.com/vue@2"></script> --> <!-- 需要先引入vue-timer-countdown:<script src="//unpkg.com/vue-timer-countdown"></script> --> <script> // 全局注册 Vue.use(vueTimerCountdown, { component: 'timer-countdown' }) // 组件名默认是:timer-countdown // 或:Vue.component('timer-countdown', vueTimerCountdown) </script>
局部注册
<!-- 需要先引入vue:<script src="//unpkg.com/vue@2"></script> --> <!-- 需要先引入vue-timer-countdown:<script src="//unpkg.com/vue-timer-countdown"></script> --> <script> new Vue({ components: { // 局部注册 'timer-countdown': vueTimerCountdown } }) </script>
用法
参数
<TimerCountdown :deadline="倒计时剩余时间-秒(必填)" :complete-zero="补全0(true)" :left-second="提前到期-秒(0)" :ignore-day="true:展示到小时;false:展示到天(true)" @done="倒计时结束后回调的方法" @update="每秒渲染结束后回调的方法,带参数{ day, hour, minute, second, restSecond }(展示的天、小时、分钟、秒,总共剩余的秒)" />
任何时候(包括已经结束后触发
done
之后),修改deadline
都可以让组件重新启动。插槽
<TimerCountdown :deadline="倒计时剩余时间-秒(必填)" v-slot="time" > {{ time.day }}天 {{ time.hour }}时 {{ time.minute }}分 {{ time.second }}秒 </TimerCountdown>
Tips
因为传递的deadline
是剩余秒数,而不是时间戳,所以如果想要重启同一个秒数的倒计时,需要想办法触发,比如先设置deadline
为其他值然后再设置回去。(如果改为时间戳实现就可以规避前面说的问题,但是使用者就要每次传入倒计时秒数 + Date.now()
)