将日期道具绑定到多个类

时间:2017-03-13 16:07:41

标签: javascript vue.js vuejs2

我有一个倒计时组件,并有一个日期道具。我有多个叫做计时器的课程,我有自己的方法,天,等等。你明白了。我所拥有的是ProjectCountdown组件中的静态日期。我希望能够使用多个不同的日期,因此每个项目都有自己的倒计时。

这是代码

ProjectCountdown.vue

  <template>
    <grid :position="grid">
      <Countdown date="March 20, 2017 12:00"></Countdown>
    </grid>
    </template>

    <script>
    import Grid from './Grid';
    import Vue from 'vue';
    import Countdown from './Countdown';
    export default {
      components: {
        Countdown, Grid
      },
      props: {
        grid: {
          type: String,
        }
      }
    }
    Vue.filter('two_digits', function (value) {
      if (value.toString().length <= 1) {
        return "0" + value.toString()
      }
      return value.toString();
    });
    </script>

Countdown.vue

    <template>
    <div id="slideshow">
    <div class="timer">
    <div class="title-block">
        <p class="title">KNHB</p>
        <p class="description">Sprint 1</p>
    </div>
    <div class="column">
      <div class="block">
          <p class="digit">{{ days | two_digits }}</p>
          <p class="text">Days</p>
      </div>
      <div class="block">
          <p class="digit">{{ hours | two_digits }}</p>
          <p class="text">Hours</p>
      </div>
      <div class="block">
          <p class="digit">{{ minutes | two_digits }}</p>
          <p class="text">Minutes</p>
      </div>
    </div>  
    </div>
    <div class="timer">
    <div class="title-block">
        <p class="title">MG de Jong</p>
        <p class="description">Sprint 2</p>
     </div>
     <div class="column">
      <div class="block">
          <p class="digit">{{ days | two_digits }}</p>
          <p class="text">Days</p>
      </div>
      <div class="block">
          <p class="digit">{{ hours | two_digits }}</p>
          <p class="text">Hours</p>
      </div>
      <div class="block">
          <p class="digit">{{ minutes | two_digits }}</p>
          <p class="text">Minutes</p>
      </div>
    </div>  
    </div>
    <div class="timer">
    <div class="title-block">
        <p class="title">ITIP</p>
        <p class="description">Sprint 3</p>
     </div>
     <div class="column">
      <div class="block">
          <p class="digit">{{ days | two_digits }}</p>
          <p class="text">Days</p>
      </div>
      <div class="block">
          <p class="digit">{{ hours | two_digits }}</p>
          <p class="text">Hours</p>
      </div>
      <div class="block">
          <p class="digit">{{ minutes | two_digits }}</p>
          <p class="text">Minutes</p>
      </div>
    </div>  
    </div>
    </div>
    </template>
    <script>

    export default {
    props: {
      date: {
          type: Number
      },
    },
    data() {
      return {
          now: Math.trunc((new Date()).getTime() / 1000)
      }
    },
    mounted() {
      window.setInterval(() => {
          this.now = Math.trunc((new Date()).getTime() / 1000);
      },1000);      
    },
    computed: {
      seconds() {
          return (this.modifiedDate - this.now) % 60;
      },
      minutes() {
          return Math.trunc((this.modifiedDate - this.now) / 60) % 60;
      },
      hours() {
          return Math.trunc((this.modifiedDate - this.now) / 60 / 60) % 24;
      },
      days() {
          return Math.trunc((this.modifiedDate - this.now) / 60 / 60 / 24);
      },
      modifiedDate: function() {
         return Math.trunc(Date.parse(this.date) / 1000)
      }
    },
    }
  </script>

1 个答案:

答案 0 :(得分:0)

https://github.com/niugm/countDown 我写了一个演示,你需要吗?

Timer.vue

GuzzleHttp\\Ring\\Exception\\RingException did not resolve future

CountDown.vue

<template>
  <div class="timer">
    <div class="title-block">
      <slot>
        <!--slot-->
      </slot>
    </div>
    <div class="column">
      <div class="block">
        <p class="digit">{{ days }}</p>
        <p class="text">Days</p>
      </div>
      <div class="block">
        <p class="digit">{{ hours }}</p>
        <p class="text">Hours</p>
      </div>
      <div class="block">
        <p class="digit">{{ minutes }}</p>
        <p class="text">Minutes</p>
      </div>
      <div class="block">
        <p class="digit">{{ seconds }}</p>
        <p class="text">Seconds</p>
      </div>
    </div>
  </div>
</template>
<style>
  .timer{
    background-color: #42b983;
    padding: 20px;
    margin: 20px;
  }
  .title-block{
    border: solid 1px #f2f2f2;
  }
  .title{
    font-size: 18px;
    font-weight: 700;
  }
  .timer .column .block{
    display: inline-block;
    margin: 0 10px;
    background-color: #FFF;
    padding: 10px;
  }
</style>
<script>
  export default {
    props: {
      date: {
        type: String
      }
    },
    data () {
      return {
        now: 0,
        count: 0
      }
    },
    methods: {
      loop () {
        this.count++
        this.now = Math.trunc(new Date().getTime() / 1000)
        console.log(this.now)
        console.log(this.count)
        this.count < 200 && setTimeout(this.loop, 1000)
      }
    },
    mounted () {
      this.loop()
    },
    computed: {
      seconds () {
        return (this.modifiedDate - this.now) % 60
      },
      minutes () {
        return Math.trunc((this.modifiedDate - this.now) / 60) % 60
      },
      hours () {
        return Math.trunc((this.modifiedDate - this.now) / 60 / 60) % 24
      },
      days () {
        return Math.trunc((this.modifiedDate - this.now) / 60 / 60 / 24)
      },
      modifiedDate: function () {
        return Math.trunc(Date.parse(this.date) / 1000)
      }
    }
  }
</script>

App.vue

    <template>
      <div id="slideshow">
        <Timer :date="date">
          <p class="title">KNHB</p>
          <p class="description">Sprint 1</p>
        </Timer>
        <Timer :date="date">
          <p class="title">MG de Jong</p>
          <p class="description">Sprint 2</p>
        </Timer>
        <Timer :date="date">
          <p class="title">ITIP</p>
          <p class="description">Sprint 3</p>
        </Timer>
      </div>
    </template>
    <style>
      .timer{
        background-color: #42b983;
        padding: 20px;
        margin: 20px;
      }
      .title-block{
        border: solid 1px #f2f2f2;
      }
      .title{
        font-size: 18px;
        font-weight: 700;
      }
      .timer .column .block{
        display: inline-block;
        margin: 0 10px;
        background-color: #FFF;
        padding: 10px;
      }
    </style>
    <script>
      import Timer from './Timer'
      export default {
        components: {
          Timer
        },
        props: {
          date: {
            type: String
          }
        },
        data () {
          return {
            now: Math.trunc((new Date()).getTime() / 1000)
          }
        },
        mounted () {
          window.setInterval(() => {
            this.now = Math.trunc((new Date()).getTime() / 1000)
          }, 1000)
        },
        computed: {
          seconds () {
            return (this.modifiedDate - this.now) % 60
          },
          minutes () {
            return Math.trunc((this.modifiedDate - this.now) / 60) % 60
          },
          hours () {
            return Math.trunc((this.modifiedDate - this.now) / 60 / 60) % 24
          },
          days () {
            return Math.trunc((this.modifiedDate - this.now) / 60 / 60 / 24)
          },
          modifiedDate: function () {
            return Math.trunc(Date.parse(this.date) / 1000)
          }
        }
      }
    </script>