Vuejs对设置值的反应性

时间:2019-10-07 21:20:03

标签: vuejs2 vue-reactivity

我有一个自定义表,该表带有通过模式弹出窗口设置行上值的操作。大部分情况下工作都很好(将Foo和Bar的更新发送到后端,并在数据库中进行了设置,页面的重新加载从数据库中提取了数据,并显示foo / bar的设置正确)。唯一不重要的部分是Foo的设置,表中的值不会更新。酒吧获得更新/反应。 (模板使用foo.name和bar.id)。是否有人对如何在表中更新Foo有任何想法?我已经更改了胡子模板以使用{{foo.id}}并进行更新,但是我需要foo.name。

<template>
  <div>
    <c-dialog
      v-if="foo_modal"
      title="Set Foo"
      :actions="foo_modal_options.actions"
      @cancel="foo_modal = null">
      <slot>
        <div class="form-group">
          <label>Foo:</label>
          <select class="form-control" v-model="foo_modal.thing.foo.id">
            <option v-for="foo in foos" :key="foo.id" :value="foo.id">{{ foo.name }}</option>
          </select>
        </div>
      </slot>
    </c-dialog>
    <c-dialog
      v-if="bar_modal"
      title="Set Rod/Stick"
      :actions="bar_modal_options.actions"
      @cancel="bar_modal = null">
      <slot>
        <div class="form-group">
          <label>Rod:</label>
          <select class="form-control" v-model="bar_modal.thing.rod.id">
            <option v-for="bar in bars" :key="bar.id" :value="bar.id" v-if="bar.type === 'rod'">{{ bar.id }}</option>
          </select>
        </div>
        <div class="form-group">
          <label>Stick:</label>
          <select class="form-control" v-model="bar_modal.thing.stick.id">
            <option v-for="bar in bars" :key="bar.id" :value="bar.id" v-if="bar.type === 'stick'">{{ bar.id }}</option>
          </select>
        </div>
      </slot>
    </c-dialog>

    <c-table-paginated
      class="c-table-clickable"
      :rows="grid.data"
      :columns="grid.columns"
      :actions="grid.actions"
      :key="componentKey">
    </c-table-paginated>
  </div>
</template>

<script>
    import fooModal from '../../components/fooModal.vue';
    import barModal from '../../components/barModal.vue';
    import CTablePaginated from "../../components/custom/cTable/cTablePaginated";
    import cTooltip from '../../components/custom/cTooltip/cTooltip.vue';
    import cDialog from '../../components/custom/cDialog/cDialog.vue';
    import moment from 'moment';

    export default {
        components: { CTablePaginated, cTooltip, cDialog },
        methods: {
            loadData() {
                let that = this;
                that.$http.get('/things', { params: that.param || {} })
                    .then(function (things) {
                        that.things = things.data;
                        that.grid.data = that.things;
                    });
            },
            setBar(thing_id, options, cb) {
                let that = this;
                this.$http.patch(`/things/${thing_id}`, { rod_id: options.rod, stick_id: options.stick })
                    .then(function () {
                        cb();
                    });
            },
            setFoo(thing_id, options, cb) {
                let that = this;
                this.$http.patch(`/things/${thing_id}`, { foo_id: options.foo_id })
                    .then(function () {
                        cb();
                    })
            },
        },
        data() {
            return {
                componentKey: 0,
                things: null,
                foos: [],
                bars: [],
                foo_modal: null,
                foo_modal_options: {
                    actions: [
                        {
                            label: "Save",
                            class: "btn-primary",
                            action: (function (ctx) {
                                return function () {
                                    const thing = ctx.foo_modal.thing;
                                    const options = {
                                        foo_id: thing.foo.id,
                                    };
                                    ctx.setFoo(thing.id, options, function () {
                                        ctx.foo_modal = null;
                                    });
                                }
                            })(this)
                        },
                        {
                            label: "Cancel",
                            action: (function (ctx) {
                                return function () {
                                    ctx.foo_modal = null;
                                }
                            })(this)
                        }
                    ]
                },
                bar_modal: null,
                bar_modal_options: {
                    actions: [
                        {
                            label: "Save",
                            class: "btn-primary",
                            action: (function (ctx) {
                                return function () {
                                    const thing = ctx.bar_modal.thing;
                                    const options = {
                                        rod: thing.rod.id,
                                        stick: thing.stick.id
                                    };
                                    ctx.setBar(thing.id, options, function () {
                                        ctx.bar_modal = null;
                                    });
                                }
                            })(this)
                        },
                        {
                            label: "Cancel",
                            action: (function (ctx) {
                                return function () {
                                    ctx.bar_modal = null;
                                }
                            })(this)
                        }
                    ]
                },
                grid: {
                    data: [],
                    columns: [
                        {
                            label: "Foo",
                            value: function (row) {
                                if (!row.foo) return "No foo set";
                                return `${row.foo.name }`;
                            }
                        },
                        {
                            label: "Rod/Stick",
                            value: function (row) {
                                if (!row.rod && !row.stick) return "No rod/stick set";
                                if (!row.rod) return `No rod set/${row.stick.id}`;
                                if (!row.stick) return `${row.rod.id}/no stick set`;
                                return `${row.rod.id}/${row.stick.id}`;
                            }
                        }
                    ],
                    actions: [
                        {
                            label: "Set Foo",
                            visible: function (thing) {
                                return !thing.active;
                            },
                            action: (function (ctx) {
                                return function (thing) {
                                    if (!thing.foo) thing.foo = {};
                                    ctx.foo_modal = {
                                        thing: thing
                                    };
                                }
                            })(this)
                        },
                        {
                            label: "Set Bar",
                            visible: function (thing) {
                                return !thing.active;
                            },
                            action: (function (ctx) {
                                return function (thing) {
                                    if (!thing.rod) thing.rod = {};
                                    if (!thing.stick) thing.stick = {};
                                    ctx.bar_modal = {
                                        thing: thing
                                    };
                                }
                            })(this)
                        },
                    ],
                }
            };
        },
        props: {
            title: {
                type: String
            },
            param: {
                type: Object,
                required: true
            },
            events: {
                type: Object,
                required: true
            }
        },
        created() {
            let that = this;
            this.loadData();
            this.$http.get('/bars')
                .then(function (bars) {
                    that.bars = bars.data;
                });
            this.$http.get('/foos')
                .then(function (foos) {
                    that.foos = foos.data;
                });
        },
    }
</script>

1 个答案:

答案 0 :(得分:0)

如果有两种方法可以帮助您,您可以尝试两种方法。

  1. 您可以使用Vuejs this。$ set方法设置值以提高反应性。 Click here.
  2. 您可以使用this。$ nextTick(()=> {//在此处设置变量})。 Click here.