[英]Update Vue.js data
hi I am having some trouble to update the date of vue.js when I cann the function to update this it is "crashing the browser process" 嗨,我无法更新vue.js的日期时,我无法通过函数来更新它,这是“崩溃了浏览器进程”
here is the code 这是代码
var app = "";
app = new Vue({
methods: {
selfClick: function (valor) {
$("#" + valor).click();
},
UpdateData: function () {
var that = this;
$.ajax({
url: "/api/json/cte",
type: "POST",
dataType: "json",
data: {
Tomador: var_grid_tomador,
Pagina: var_grid_pagina,
TotalShow: var_grid_toalshow,
OrdenarPor: var_grid_ordernarpor,
OrdernarAscDes: var_grid_AscDes,
Empresa: var_gird_empresa,
},
success: function (rt) {
console.log(rt);
that.items = rt.Data;
$(".loading").hide();
$("#maxShow").val(rt.MaxShow);
$("#GridTTRows").html("Total de itens: " + rt.TotalItens);
footernizer(rt.AllPages, rt.CurrentPage);
console.log(rt);
$("tobdy .odd").hide();
},
error: function (rt) {
toastShow("toast-error", "Houve um erro, verifique sua conexão");
console.log(rt.responsetext);
$(".loading").hide();
},
})
this.$forceUpdate();
}
},
filters: {
formatDate: function (value) {
if (!value) return ''
var data = value.split("T");
data = data[0].split("-");
return data[2] + "/" + data[1] + "/" + data[0];
},
formatDateTime: function (value) {
if (!value) return ''
var data = value.split("T");
var date = data[0].split("-");
var times = data[1].split(":");
return date[2] + "/" + date[1] + "/" + date[0] + " " + times[0] + ":" + times[1];
},
brl: function (value) {
if (!value) return ''
var numero = value.toFixed(2).split('.');
numero[0] = "" + numero[0].split(/(?=(?:...)*$)/).join('.');
return numero.join(',');
}
},
el: '#app',
created: function () {
this.UpdateData();
},
computed: {
// Propriedade customizada utilizada como filtro dinâmico
filteredData: function () {
var articles_array = this.items,
searchString = this.searchString;
if (!searchString) {
return articles_array;
}
searchString = searchString.trim().toLowerCase();
articles_array = articles_array.filter(function (item) {
if (item.destinatario.toLowerCase().indexOf(searchString) !== -1) {
return item;
}
})
// Return an array with the filtered data.
return articles_array;
}
}
});
But when I call this function 但是当我调用这个函数
app.UpdateData();
the browser get stocked and doesn't work 浏览器存货不足,无法正常工作
am I doing this right? 我这样做对吗? How can I update this data that cames from the server site 如何更新来自服务器站点的数据
Seems you at least need to place the method inside beforeMount() hook? 似乎您至少需要将方法放在beforeMount()挂钩中?
Also, defining a data model for a component is a good practice 同样,为组件定义数据模型也是一种好习惯
// methods: {...},
data(){
return {
items: null
}
},
// mounted: {...}
in the success:
portion of your ajax method change that.items
to this.items
where (as @mannok said) items is defined in your vue instance data property. success:
ajax方法的that.items
将this.items
更改为this.items
,其中(如@mannok所说)在vue实例数据属性中定义了项。 Also, this.$forceUpdate()
is probably unnecessary. 另外, this.$forceUpdate()
可能是不必要的。 Vue will automatically update when data changes (as long as you aren't adding properties to an existing object or array). Vue将在数据更改时自动更新(只要您不向现有对象或数组添加属性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.