Vue와 함께 Jquery Chosen을 사용하고 있습니다. 상태는 그것이 기대에 따라 잘 작동하지만 가져올 경우 상태는 동적입니다 선택한 가치를 정적 값을 할당하는 경우Vue 지시어가 값을 업데이트하지 않습니다 - Jquery Chosen
<chosen-select v-model="basicDetailsModel.stateID" v-validate="'required'" data-vv-as="state" :state="errors.has('stateID') ? 'invalid' : 'valid'" name="stateID">
<option :value="null">Please select an option</option>
<option v-for="(state, index) in states" :key="index" :value="state.sid">{{state.nm}}</option>
</chosen-select>
:
Vue.component("chosen-select", {
props: {
value: [String, Array],
multiple: Boolean
},
template: `<select :multiple="multiple"><slot></slot></select>`,
mounted() {
$(this.$el)
.val(this.value)
.chosen({ width: '100%' })
.on("change", e => this.$emit('input', $(this.$el).val()))
},
watch: {
value(val) {
$(this.$el).val(val).trigger('chosen:updated');
}
},
destroyed() {
$(this.$el).chosen('destroy');
}
});
그리고 이런 식으로 그것을 사용 : 이것은 내 뷰 지시자이다 최신 값으로 업데이트되지 않았습니다. 초기 값으로 유지됩니다.
어떻게이 문제를 해결할 수 있습니까?
편집 : 이것은 작동합니다. 이것이 올바른 방법이라고 생각하십니까?
Vue.component("chosen-select", {
data() {
return { observer: null }
},
props: {
value: [String, Array],
multiple: Boolean
},
template: `<select :multiple="multiple"><slot></slot></select>`,
mounted() {
// Create the observer (and what to do on changes...)
this.observer = new MutationObserver(function (mutations) {
$(this.$el).trigger("chosen:updated");
}.bind(this));
// Setup the observer
this.observer.observe(
$(this.$el)[0],
{ childList: true }
);
$(this.$el)
.val(this.value)
.chosen({ width: '100%' })
.on("change", e => this.$emit('input', $(this.$el).val()))
},
watch: {
value(val) {
$(this.$el).val(val);
}
},
destroyed() {
$(this.$el).chosen('destroy');
}
});
처음에는 업데이트 작업을 시도했지만 v- 모델의 속성이 변경 될 때마다 업데이트가 호출되므로 비효율적 일 수 있습니다 (변경 선택 발생). https://stackoverflow.com/questions/38148297/in-vue-js-can-a-component-detect-when-the-slot-content-changes에서 영감을 얻어 내 코드를 변경했습니다. 내 편집을 참조하십시오. 그게 뭔지 알려주세요. –