बस एक त्वरित प्रश्न।
क्या आप सब कुछ पुनः लोड / पुनर्गणनाVue.js
करने के लिए मजबूर कर सकते हैं ? यदि हां, तो कैसे?
बस एक त्वरित प्रश्न।
क्या आप सब कुछ पुनः लोड / पुनर्गणनाVue.js
करने के लिए मजबूर कर सकते हैं ? यदि हां, तो कैसे?
जवाबों:
इस जादुई मंत्र का प्रयास करें:
vm.$forceUpdate();
किसी भी हैंगिंग वर्जन को बनाने की आवश्यकता नहीं है :)
अद्यतन: मुझे यह समाधान तब मिला जब मैंने केवल VueJS के साथ काम करना शुरू किया। हालाँकि आगे की खोज ने इस दृष्टिकोण को बैसाखी के रूप में सिद्ध किया। जहाँ तक मुझे याद है, कुछ ही समय में मैंने इससे छुटकारा पा लिया, बस उन सभी गुणों को डाल दिया, जो स्वचालित रूप से (ज्यादातर नेस्टेड) को संपीड़ित गुणों में ताज़ा करने में विफल रहे।
अधिक जानकारी यहां: https://vuejs.org/v2/guide/computed.html
$forceUpdate()
कभी भी मेरे लिए 2.5.17 पर काम नहीं किया।
यह इस मुद्दे पर matthiasg से एक बहुत साफ समाधान की तरह लगता है :
:key="someVariableUnderYourControl"
जब आप कंपोनेंट को पूरी तरह से फिर से बनाना चाहते हैं तो आप कुंजी का उपयोग और बदल सकते हैं
मेरे उपयोग के मामले के लिए, मैं एक प्रोप में एक घटक में एक Vuex गेट्टर खिला रहा था। किसी भी तरह Vuex डेटा को लाएगा, लेकिन प्रतिक्रियाशीलता मज़बूती से घटक को रेंडर करने के लिए किक नहीं करेगी। मेरे मामले में, key
प्रोप पर कुछ विशेषता के लिए घटक की स्थापना एक रिफ्रेश की गारंटी देती है जब गेटर्स (और विशेषता) अंततः हल हो जाती है।
mounted
चलाया जाएगा, आदि)
... क्या आपको अपडेट को बाध्य करने की आवश्यकता है?
शायद आप इसकी सबसे अच्छी खोज नहीं कर रहे हैं:
Vue में स्वचालित रूप से मूल्य परिवर्तनों पर प्रतिक्रिया होती है, वस्तुओं को शुरू में घोषित किया
data
जाना चाहिए । या, यदि नहीं, तो उनका उपयोग करके जोड़ाVue.set()
जाना चाहिए ।
नीचे डेमो में टिप्पणियां देखें। या यहाँ एक JSFiddle में एक ही डेमो खोलें ।
new Vue({
el: '#app',
data: {
person: {
name: 'Edson'
}
},
methods: {
changeName() {
// because name is declared in data, whenever it
// changes, Vue automatically updates
this.person.name = 'Arantes';
},
changeNickname() {
// because nickname is NOT declared in data, when it
// changes, Vue will NOT automatically update
this.person.nickname = 'Pele';
// although if anything else updates, this change will be seen
},
changeNicknameProperly() {
// when some property is NOT INITIALLY declared in data, the correct way
// to add it is using Vue.set or this.$set
Vue.set(this.person, 'address', '123th avenue.');
// subsequent changes can be done directly now and it will auto update
this.person.address = '345th avenue.';
}
}
})
/* CSS just for the demo, it is not necessary at all! */
span:nth-of-type(1),button:nth-of-type(1) { color: blue; }
span:nth-of-type(2),button:nth-of-type(2) { color: red; }
span:nth-of-type(3),button:nth-of-type(3) { color: green; }
span { font-family: monospace }
<script src="https://unpkg.com/vue"></script>
<div id="app">
<span>person.name: {{ person.name }}</span><br>
<span>person.nickname: {{ person.nickname }}</span><br>
<span>person.address: {{ person.address }}</span><br>
<br>
<button @click="changeName">this.person.name = 'Arantes'; (will auto update because `name` was in `data`)</button><br>
<button @click="changeNickname">this.person.nickname = 'Pele'; (will NOT auto update because `nickname` was not in `data`)</button><br>
<button @click="changeNicknameProperly">Vue.set(this.person, 'address', '99th st.'); (WILL auto update even though `address` was not in `data`)</button>
<br>
<br>
For more info, read the comments in the code. Or check the docs on <b>Reactivity</b> (link below).
</div>
Vue के इस भाग में महारत हासिल करने के लिए, प्रतिक्रियाशीलता पर आधिकारिक डॉक्स - चेक डिटेक्शन कैविट्स बदलें । इसे अवश्य पढ़ना चाहिए!
Vue.set()
घटकों के अंदर भी काम करता है।
कृपया इसे पढ़ें http://michaelnthiessen.com/force-re-render/
भयानक तरीका: पूरे पृष्ठ को पुनः लोड करना
भयानक तरीका: v-if हैक
का उपयोग करना बेहतर तरीका: Vue के अंतर्निहित बल प्रयोग विधि
का उपयोग करना सबसे अच्छा तरीका है: आपके घटक पर की-चेंजिंग
<template>
<component-to-re-render :key="componentKey" />
</template>
<script>
export default {
data() {
return {
componentKey: 0,
};
},
methods: {
forceRerender() {
this.componentKey += 1;
}
}
}
</script>
मैं भी घड़ी का उपयोग करता हूं: कुछ स्थितियों में।
this.$router.go(0);
वर्तमान पृष्ठ को मैन्युअल रूप से पुनः लोड करने के लिए उपयोग करने का प्रयास करें ।
का उपयोग करें vm.$set('varName', value)
। Change_Detection_Caveats में विवरण देखें ।
ज़रूर .. आप किसी भी समय फिर से प्रस्तुत करने (मनोरंजन) के लिए मजबूर करने के लिए प्रमुख विशेषता का उपयोग कर सकते हैं।
<mycomponent :key="somevalueunderyourcontrol"></mycomponent>
एक उदाहरण के लिए https://jsfiddle.net/mgoetzke/epqy1xgf/ देखें
यहां भी चर्चा की गई: https://github.com/vuejs/Discussion/issues/356#issuecomment-336060875
<my-component :key="uniqueKey" />
इसके साथ ही यह हर अपडेट के लिए ऑब्जेक्ट (obj) वैल्यू में हर this.$set(obj,'obj_key',value)
अपडेट के uniqueKey
लिए उपयोग और अपडेट करता हैthis.uniqueKey++
इसने मेरे लिए इस तरह से काम किया
तो ऐसा करने के दो तरीके हैं,
1)। आप $forceUpdate()
अपने तरीके हैंडलर के अंदर उपयोग कर सकते हैं
<your-component @click="reRender()"></your-component>
<script>
export default {
methods: {
reRender(){
this.$forceUpdate()
}
}
}
</script>
2)। जब आप :key
रेंडर करना चाहते हैं तो आप अपने कंपोनेंट और ग्रेजुएशन को एक विशेषता दे सकते हैं
<your-component :key="index" @click="reRender()"></your-component>
<script>
export default {
data() {
return {
index: 1
}
},
methods: {
reRender(){
this.index++
}
}
}
</script>
v-if निर्देश का उपयोग करना
<div v-if="trulyvalue">
<component-here />
</div>
तो बस असत्य से असत्य के मूल्य को बदलकर सत्य को फिर से भाजक के बीच के घटक का कारण बनेगा
घटक को पुनः लोड / री-रेंडर / रीफ़्रेश करने के लिए, लंबे कोडिंग को रोकें। ऐसा करने का एक Vue.JS तरीका है।
बस :key
विशेषता का उपयोग करें ।
उदाहरण के लिए:
<my-component :key="unique" />
मैं उस एक का प्रयोग कर रहा हूँ BS Vue Table Slot। यह बताते हुए कि मैं इस घटक के लिए कुछ करूंगा ताकि यह अद्वितीय हो।
मुझे एक रास्ता मिल गया है। यह थोड़ा हैकी है लेकिन काम करता है।
vm.$set("x",0);
vm.$delete("x");
vm
आपका दृश्य-मॉडल ऑब्जेक्ट कहां है, और x
एक गैर-मौजूद चर है।
Vue.js कंसोल लॉग में इसके बारे में शिकायत करेंगे लेकिन यह सभी डेटा के लिए एक ताज़ा ट्रिगर करता है। 1.0.26 संस्करण के साथ परीक्षण किया गया।
बस इस कोड को जोड़ें:
this.$forceUpdate()
शुभ लाभ
: key = "$ path.params.param1" बस अपने किसी भी परम के साथ कुंजी का उपयोग करें अपने बच्चों को फिर से लोड करें ।।
मेरे पास एक छवि गैलरी के साथ यह मुद्दा था जिसे मैं एक अलग टैब पर किए गए परिवर्तनों के कारण रेंडर करना चाहता था। तो tab1 = imageGallery, tab2 = पसंदीदाइमेज
टैब @ परिवर्तन = "अपडेटगैलरी ()" -> यह मेरे वी-फॉर निर्देश को फ़िल्टर किए गए फ़ंक्शन को हर बार जब मैं टैब स्विच करता है, संसाधित करने के लिए मजबूर करता है।
<script>
export default {
data() {
return {
currentTab: 0,
tab: null,
colorFilter: "",
colors: ["None", "Beige", "Black"],
items: ["Image Gallery", "Favorite Images"]
};
},
methods: {
filteredImages: function() {
return this.$store.getters.getImageDatabase.filter(img => {
if (img.color.match(this.colorFilter)) return true;
});
},
updateGallery: async function() {
// instance is responsive to changes
// change is made and forces filteredImages to do its thing
// async await forces the browser to slow down and allows changes to take effect
await this.$nextTick(function() {
this.colorFilter = "Black";
});
await this.$nextTick(function() {
// Doesnt hurt to zero out filters on change
this.colorFilter = "";
});
}
}
};
</script>
क्षमा करें, पृष्ठ पुनः लोड विधि (टिमटिमाते हुए) को छोड़कर, उनमें से कोई भी मेरे लिए काम नहीं करता है: (कुंजी काम नहीं किया)।
और मुझे पुरानी v..ss फोरम से यह विधि मिली जो मेरे लिए काम कर रही है:
https://github.com/vuejs/Discussion/issues/356
<template>
<div v-if="show">
<button @click="rerender">re-render</button>
</div>
</template>
<script>
export default {
data(){
return {show:true}
},
methods:{
rerender(){
this.show = false
this.$nextTick(() => {
this.show = true
console.log('re-render start')
this.$nextTick(() => {
console.log('re-render end')
})
})
}
}
}
</script>