True return function returns false in a Vue expression

Asked

Viewed 216 times

0

I created a method that serves to take the difference of a certain date with today’s date. With the code I put, it returns true. But when I use it within a computed value, it returns undefined. And in the computed value I make a conditional that will anyway return true but when I use it as an expression in Vue, it returns false.

<template>
    <p v-if="dataqualquer">Hello World</p>
</template>

<script>
    computed: {
        dataqualquer() {
            const data = this.dataDiferenca("05/02/2018")
            data ? true : true
        }
    },
    methods: {
        dataDiferenca(data) {
           // console.log(data)
           // data = data.split("/")
           // data.reverse();
           // data = data.join("-")
           // data = this.moment(data)
           // console.log(data.diff())

           // data.diff() < 0 ? false : true
           data = data.split("/");
           data.reverse();
           data = data.join("-")
           data = new Date(data)

           const hoje = new Date()

           data - hoje < 0 ? false : true
        }
    }
</script>

1 answer

1


You forgot to give return in his method dataDiferenca and in computed value dataqualquer

var vue = new Vue({
    el: '#app',
    computed: {
        dataqualquer () {
            const data = this.dataDiferenca("05/02/2018")
            return data
        }
    },
    methods: {
        dataDiferenca (data) {     
           data = data.split("/");
           data.reverse();
           data = data.join("-")
           data = new Date(data)

           const hoje = new Date()

           return data - hoje < 0 ? false : true
        }
    }
})
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="app">
    <p v-if="dataqualquer">Hello World</p>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.