【问题标题】:Vue Component Reactive V-Model from parent update来自父级更新的 Vue 组件反应式 V-Model
【发布时间】:2021-07-23 08:26:23
【问题描述】:

我正在努力尝试将 Jquery 插件制作成 Vue 组件。我有其他一切工作,但我需要检测 v-model 值何时从子组件的父级更改。例如,如果 vue 应用程序中的其他内容更改了子组件上的绑定值,我需要运行一些特定的代码。到目前为止,我无法弄清楚如何具体检测到这一点:

Vue.component(
            'c5-page-selector',
            {
                template: '<div class="page-selector"></div>',
                prop: ['value'],
                mounted: function () {
                    let self = this;
                    $(this.$el).concretePageSelector({inputName: 'test', cID: this.value});
                    $(this.$el).find('.ccm-item-selector-clear').click(function () {
                        self.updatePage();
                    });

                    $(this.$el).click(function () {
                        self.updatePage();
                    });
                    Concrete.event.bind('ConcreteSitemap', function(e, instance) {
                        var instance = instance;
                        Concrete.event.bind('SitemapSelectPage', function(e, data) {
                            if (data.instance == instance) {
                                self.updatePage();
                            }
                        });
                    });
                },
                methods: {
                    updatePage() {
                        let value = $(this.$el).find('input').val();
                        if (value && value.length && value != 0) {
                            value = parseInt(value, 10);
                        } else {
                            value = null;
                        }
                        this.$emit('input', value)
                    }
                }
            }
        );

【问题讨论】:

  • 使用观察者观察道具变化?

标签: vue.js vue-component


【解决方案1】:
computed: {
  yourDesiredVariable: {
     get() {
       return this.value
     },
     set(val) {
       this.$emit('input', val)
     }
  }
}

尝试在 DOM 中输出 yourDesiredValue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2023-02-03
    • 2019-02-14
    • 2021-08-19
    • 2020-09-24
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多