【问题标题】:Update Vue.js data更新 Vue.js 数据
【发布时间】:2018-06-30 18:29:54
【问题描述】:

嗨,当我无法更新 vue.js 的日期时,我遇到了一些麻烦,它是“浏览器进程崩溃”

这里是代码

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;
        }
    }
});

但是当我调用这个函数时

app.UpdateData();

浏览器有货,无法使用

我这样做对吗? 如何更新来自服务器站点的数据

【问题讨论】:

  • 老是用jquery,用vue有什么意义呢?

标签: javascript jquery ajax vue.js


【解决方案1】:

您似乎至少需要将方法放在 beforeMount() 钩子中?

另外,为组件定义数据模型是一种很好的做法

// methods: {...},
data(){
    return {
        items: null
    }
},
// mounted: {...}

【讨论】:

    【解决方案2】:

    在您的 ajax 方法的 success: 部分将 that.items 更改为 this.items 其中(如 @mannok 所说)项目在您的 vue 实例数据属性中定义。此外,this.$forceUpdate() 可能是不必要的。 Vue 会在数据发生变化时自动更新(只要您没有向现有对象或数组添加属性)。

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2017-06-28
      • 2018-08-08
      • 2017-10-24
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多