NaN problem of calculation result in vue

html: {{this.starts}}

js:

     data() {
        return {
           seconds1: 86400,
           starts: (Date.parse(new Date()) / 1000) - Number(this.seconds1),
       
     

result: NaN

Why is it NaN??

Jun.24,2021

where this.seconds1 , vue can often be seen that many places can be accessed through this , because first, props/data/method is merged into vm during initialization, and secondly, call/apply/bind replaces this < / hr class= answer > with vm .

add to the above answer:

your usage is equivalent to

var a = {
    time : 1,
    count : a.time + 1
}

what does this expression do: first declare a variable, then declare an object, assign the time,count property, and finally assign the object to the a scalar. This order should be understood. When assigning the count attribute, an is still undefined, so a.time will report an error, so to you, it is this.seconds1 or undefined, so the result is NaN

.

solution: declare a variable in advance to save the data

data() {
    let seconds = 86400
    return {
       seconds1: seconds,
       starts: (Date.parse(new Date()) / 1000) - Number(seconds),

as said upstairs, it is impossible to get it. Values can be assigned in created.
created () {

this.starts= (Date.parse(new Date()) / 1000) - Number(this.seconds1);

}

Menu