日期没有正确加载vue.js
Date not loaded properly vue.js
使用getDate()
函数,我想在输入字段中显示current date
。这就是我将ride.date
绑定到其中一个输入字段的原因。
但由于某种原因,日期被放在我在其他字段中填写一个字母之后…
你听懂了吗?)
下面是一段简短的视频:
https://drive.google.com/file/d/0ByeuR1N3nhXIMFpZVXRrU1Y2NlE/view那么我如何在页面加载后立即显示日期呢?
<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>
在data
函数中,不是将ride
初始化为空对象,而是应该这样初始化它:
ride: { name: '', date: '' },
这将解决您的问题。
要了解发生了什么,请阅读http://vuejs.org/guide/reactivity.html上的"变更检测注意事项"answers"初始化数据"文档部分
您将看到另一个可能的解决方案是使用组件的$set
方法。
因此,在getDate
函数的末尾,您可以使用它设置ride.date
属性:
this.$set('ride.date', year + "-" + month + "-" + day + " " + "00:00:00");
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Emberjs应用程序加载在除Index之外的所有路由上
- 在chrome.tabs.onCreated之后加载HTML页面
- 单击F5时如何停止页面加载
- HTML5音频加载和播放获胜'我不能在iPad上工作
- 跟踪在页面加载时应用内联样式的JavaScript
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- jQuery Lazy加载动画滚动
- 如何创建带有插槽的vue js组件预加载程序
- 将自定义脚本加载到Vue.js组件中
- Vue 不加载数据
- vue.js使用定时器自动重新加载/刷新数据
- Vue.js - 无法使用 vue-resource 加载 json 数据
- AJAX部分加载后Vue绑定
- 如何在Vue.js中加载页面后使用$http.get中的数据
- 日期没有正确加载vue.js
- 如何在 vue.js 的组件中加载数据时触发转换