[英]Date not loaded properly vue.js
With the function getDate()
I would like to show the current date
in a input field. 使用函数
getDate()
我想在输入字段中显示current date
。 That's why I've bind ride.date
to one of the input fields. 这就是为什么我将
ride.date
绑定到输入字段之一的原因。
But for some reason the date is placed after I fill in one letter in one of the other fields... 但是由于某种原因,日期是在我在其他字段之一中填写一个字母之后放置的。
Are you following :) 你在追随:)
Here is a short screencast: 这是一个简短的截屏视频:
https://drive.google.com/file/d/0ByeuR1N3nhXIMFpZVXRrU1Y2NlE/view https://drive.google.com/file/d/0ByeuR1N3nhXIMFpZVXRrU1Y2NlE/view
So how can I show the date immediately after page load? 那么如何在页面加载后立即显示日期?
<template>
<div class="row center">
<div class="panel ride">
<div class="ride-title bar-underline">
<div class="ride-item-title">
<strong class="ride-item-title-body">Maak hier uw rit aan</strong>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Name</p>
</div>
<div class="ride-item-content">
<p><input class="form-group-item-special" v-model="ride.name"/></p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Datum</p>
</div>
<div class="ride-item-content">
<p><input class="form-group-item-special" v-model="ride.date"/></p>
</div>
</div>
</div>
</template>
<script>
import Banner from '../Banner.vue';
import RideService from '../../services/RideService';
import TypeService from '../../services/TypeService';
import LocationService from '../../services/LocationService';
export default {
components: { Banner },
data () {
return {
title: 'Nieuwe rit',
ride: {},
types: {},
locations: {}
}
},
methods: {
getTypes () {
TypeService.All()
.then(function(data)
{
if(data.data.types.data != null)
{
this.types = data.data.types.data;
}
}.bind(this));
},
getLocations () {
LocationService.All()
.then(function(data)
{
if(data.data.locations.data)
{
this.locations = data.data.locations.data;
}
}.bind(this));
},
getDate () {
var _this = this;
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
day = this.checkDateForZero(day);
month = this.checkDateForZero(month);
this.ride.date = year + "-" + month + "-" + day + " " + "00:00:00";
},
checkDateForZero (date) {
if(date <= 9) {
date = "0" + date;
}
return date;
},
store () {
RideService.store(this.ride, this);
},
success (message) {
swal({
title: "Succes",
text: message.success,
type: "success",
timer: 2000,
showConfirmButton: false
});
this.ride = { user: {}, location: {}, type: {} };
this.getDate();
},
showError (message) {
var errorString = '';
if (message.hasOwnProperty('error')) {
for(var prop in message.error) {
if (Array.isArray(prop)) {
for (var msg in prop) {
errorString += message.error[prop] + '<BR>';
}
}
else {
errorString += message.error[prop] + '<BR>';
}
}
};
swal({
title: "Fout",
text: errorString,
type: "error",
timer: 2000,
html: true,
showConfirmButton: false
});
}
},
ready () {
this.getDate();
this.getTypes();
this.getLocations();
}
}
</script>
In the data
function, instead of initializing ride
as an empty object, you should initialize it like this: 在
data
函数中,您不必像这样将ride
初始化为空对象,而是将其初始化:
ride: { name: '', date: '' },
This shall resolve your issue. 这样可以解决您的问题。
To understand what is going on, please read the " Change Detection Caveats " and " Initialize Your Data " documentation sections at http://vuejs.org/guide/reactivity.html 要了解发生了什么,请阅读http://vuejs.org/guide/reactivity.html上的“ 变更检测警告 ”和“ 初始化数据 ”文档部分。
You will see that another possible solution is to use the $set
method of the component. 您将看到另一种可能的解决方案是使用组件的
$set
方法。
So, alternatively, in the end of your getDate
function, you could set the ride.date
property using it: 因此,或者,在
getDate
函数的最后,您可以使用它设置ride.date
属性:
this.$set('ride.date', year + "-" + month + "-" + day + " " + "00:00:00");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.