Vue中是否有任何函数来检查子组件中的DOM是否已更新
问题描述:
我有这个问题,其中,我无法选择子组件内的DOM元素。据记载,我正在使用更新的方法,但在根组件内。Vue中是否有任何函数来检查子组件中的DOM是否已更新
而且它的工作原理有点不可思议,
Vue.component('newdiv', {
template: `
<div class="msg-wrap">
<p v-html='this.$parent.message' class="msg-p"></p>
<p v-html='this.$parent.message1' class="msg-p"></p>
</div>
`,
updated: function() {
this.$nextTick(function() {
$('.blue').css('color', 'blue');
});
}
});
new Vue({
el: '#app',
data: {
message: 'Hello this is para 1',
message1: 'Hello this is para 2',
},
methods: {
ajaxCallEx() {
// think ajax call has happened and returns an html
this.message = "<p> Hiiii This is a p1 <span class='red'> make me red </span> </p>";
this.message1 = "<p> Hiiii This is a p2 <span class='blue'> make me blue </span> </p>";
}
},
updated: function() {
this.$nextTick(function() {
// you will see that this dosen't work as the element is not present
$('.red').css('color', 'red');
});
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/jquery"></script>
<div id="app">
<newdiv></newdiv>
<button @click="ajaxCallEx">Clik Me</button>
</div>
这里按钮点击蓝色的第变为蓝色,而红色remmain,因为它是(因为它不存在于DOM然而)。
所以我的第一个假设是父组件的更新方法先被调用,然后发生子元素的渲染。这适用于这种情况。但只是为了证明我的权利,我没有尝试以下方法,
Vue.component('newdiv', {
template: `
<div class="msg-wrap">
<p v-html='this.$parent.message' class="msg-p"></p>
<p v-html='this.$parent.message1' class="msg-p"></p>
</div>
`,
updated: function() {
this.$nextTick(function() {
$('.blue').css('color', 'blue');
});
}
});
new Vue({
el: '#app',
data: {
message: 'Hello this is para 1',
message1: 'Hello this is para 2',
message3: 'Hello this is root para'
},
methods: {
ajaxCallEx() {
// think ajax call has happened and returns an html
this.message = "<p> Hiiii This is a p1 <span class='red'> make me red </span> </p>";
this.message1 = "<p> Hiiii This is a p2 <span class='blue'> make me blue </span> </p>";
this.message3 = "<p> Hiiii This is a root para <span class='green'> make me green</span> </p>";
}
},
updated: function() {
this.$nextTick(function() {
// you will see that this dosen't work as the element is not present
$('.red').css('color', 'red');
// this will work and it will only color the p directly within the root.
$('.green').css('color', 'green');
});
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/jquery"></script>
<div id="app">
<p v-html='this.message3'></p>
<newdiv></newdiv>
<button @click="ajaxCallEx">Clik Me</button>
</div>
按照从最后的实验我的理解,在按钮的点击,我希望绿转绿,蓝转蓝色,而红色不应该工作。但在这种情况下,检测到的所有元素和颜色都已正确涂漆。
我想知道更新的工作原因,为什么在第一种情况下,它无法获取子组件中的元素,为什么在第二种情况下它能够获取元素。 是否有一个钩子或事件让root知道它的所有孩子已经被渲染?
答
你在这里做的事情很奇怪。你不应该达到的message
从父,但将它作为一个道具:
Vue.component('newdiv', {
template: `
<div class="msg-wrap">
<p v-html='message' class="msg-p"></p>
<p v-html='message1' class="msg-p"></p>
</div>
`,
props: ['message', 'message1']
});
new Vue({
el: '#app',
methods:{
ajaxCallEx(){
this.message = "<p> Hiiii This is a p1 <span class='red'> make me red </span> </p>";
this.message1 = "<p> Hiiii This is a p2 <span class='blue'> make me blue </span> </p>";
}
},
data:{
message: 'Hello this is para 1',
message1: 'Hello this is para 2'
}
})
这里的的jsfiddle:https://jsfiddle.net/craig_h_411/e6nvb63m/
显然,这并不风格做任何事,这样做,你只是需要将这些类添加到您的文件中,而不是注入它们。
.red{
color:red
}
.blue{
color:blue
}
参见:https://jsfiddle.net/craig_h_411/60o3ypbf/
如果由于某种原因,你就需要在运行时应用样式,你可以使用一个watcher
观看道具和应用的颜色(这需要在一个$nextTick
做),这里的整个事情:
Vue.component('newdiv', {
template: `
<div class="msg-wrap">
<p v-html='message' class="msg-p"></p>
<p v-html='message1' class="msg-p"></p>
</div>
`,
props: ['message', 'message1'],
methods: {
applyColor(className, color) {
let els = document.getElementsByClassName(className)
Array.prototype.forEach.call(els, el => {
el.style.color = color
})
},
applyClassStyles() {
this.applyColor('red', 'red')
this.applyColor('blue', 'blue')
}
},
watch: {
message() {
this.$nextTick(() => {
this.applyClassStyles();
});
},
message1() {
this.$nextTick(() => {
this.applyClassStyles();
});
}
},
});
new Vue({
el: '#app',
methods: {
ajaxCallEx() {
this.message = "<p> Hiiii This is a p1 <span class='red'> make me red </span> </p>";
this.message1 = "<p> Hiiii This is a p2 <span class='blue'> make me blue </span> </p>";
}
},
data: {
message: 'Hello this is para 1',
message1: 'Hello this is para 2'
}
})
我只是用香草JavaScript,但你能坚持到jQuery的达到相同的效果,而且这里的的jsfiddle:https://jsfiddle.net/craig_h_411/zdfxodyg/
这个问题只是一个不同场景的模型。这不是造型。当我必须触发在jQuery中实现的特定脚本时,我有一定的场景。在这种情况下,我依赖于来自根的数据。我需要一个钩子来从根目录中激发jquery(钩子可以是一个事件或生命周期类),但是它只能在子呈现后触发。 –
Infact我想知道如何以及何时调用这个更新的函数。在子元素更新之前还是在什么时候?实际的情况。 –
很难确切地知道你需要什么而不知道你在做什么,但是当子组件完全呈现时你不能直接发送到总线上:https:// jsfiddle。净/ craig_h_411/mqLeLL02/ –