admin管理员组文章数量:1024627
I have following kind of code:
<div>
<pA />
<pB />
</div>
How do I make sure that first pA
is rendered only after it pB
is rendered.
Why I want is I have some dependency on few elements of pA
, and style of pB
depends on presence of those elements.
Why in details:
I have some plex UI design, where one box will bee fixed when you scroll. SO It will not go above the screen when you scroll, it will be fixed once you start scrolling and it start touching the header. So I am using jquery-visible to find if a div
with a particular id
is visible on the screen, if it is not visible, I change the style and make that box fixed. Following code should give the idea what I am doing:
methods: {
onScroll () {
if ($('#divId').visible(false, false, 'vertical')) { // This is div from the pA, so I want to make sure it is rendered first and it is visible
this.isFixed = false
} else {
this.isFixed = true
}
}
},
mounted () {
window.addEventListener('scroll', this.onScroll() }
},
destroyed () {
window.removeEventListener('scroll', this.onScroll)
}
I dont want to make those in same ponent as one reason is it dont make sense as the nature of these ponents, and other I use pA
at many places, while pB
is specific to only one page. Also layout of these does not allow me to make pB
child of pA
as suggested in ments.
Any suggestions are wele.
I have following kind of code:
<div>
<pA />
<pB />
</div>
How do I make sure that first pA
is rendered only after it pB
is rendered.
Why I want is I have some dependency on few elements of pA
, and style of pB
depends on presence of those elements.
Why in details:
I have some plex UI design, where one box will bee fixed when you scroll. SO It will not go above the screen when you scroll, it will be fixed once you start scrolling and it start touching the header. So I am using jquery-visible to find if a div
with a particular id
is visible on the screen, if it is not visible, I change the style and make that box fixed. Following code should give the idea what I am doing:
methods: {
onScroll () {
if ($('#divId').visible(false, false, 'vertical')) { // This is div from the pA, so I want to make sure it is rendered first and it is visible
this.isFixed = false
} else {
this.isFixed = true
}
}
},
mounted () {
window.addEventListener('scroll', this.onScroll() }
},
destroyed () {
window.removeEventListener('scroll', this.onScroll)
}
I dont want to make those in same ponent as one reason is it dont make sense as the nature of these ponents, and other I use pA
at many places, while pB
is specific to only one page. Also layout of these does not allow me to make pB
child of pA
as suggested in ments.
Any suggestions are wele.
Share Improve this question edited Mar 29, 2017 at 4:47 Saurabh asked Mar 28, 2017 at 14:04 SaurabhSaurabh 73.7k44 gold badges191 silver badges251 bronze badges 8- My best shot is to use events. – Jonatas Walker Commented Mar 28, 2017 at 14:07
-
1
Could
CompB
not be a child ofCompA
sinceCompB
depends onCompA
? – Amresh Venugopal Commented Mar 28, 2017 at 14:11 - @AmreshVenugopal No I will not like to do that due to nature of these ponents. – Saurabh Commented Mar 28, 2017 at 14:18
- @JonatasWalker How to use events here? – Saurabh Commented Mar 28, 2017 at 14:19
-
2
Okay, another question I have is why is
CompB
depending on elements ofCompA
why not the data ofCompA
? Since the difference in the kinds of different elements would also be some kind ofv-if='true': make input tag'
v-else: make p-tag
like implementation? – Amresh Venugopal Commented Mar 28, 2017 at 14:23
2 Answers
Reset to default 3An option with events:
<!-- Parent -->
<div>
<p-a @rendered="rendered = true"></p-a>
<ponent :is="pB"></ponent>
</div>
<script>
// import ...
export default {
ponents: { CompA, CompB },
watch: {
rendered: function (val) {
if (val) this.pB = 'p-b';
}
},
data() {
return {
rendered: false,
pB: null
}
}
}
</script>
<!-- Component B -->
<script>
export default {
mounted() {
this.$emit('rendered');
}
}
</script>
After going through the edit I realised that the dependency is not data driven but event driven (onscroll). I have tried something and looks like it works (the setTimeout in the code is for demonstration).
My implementation is slightly different from that of Jonatas.
<div id="app">
RenderSwitch: {{ renderSwitch }} // for demonstration
<template v-if='renderSwitch'>
<p-a></p-a>
</template>
<p-b @rendered='renderSwitchSet'></p-b>
</div>
When the
ponent-B
is rendered it emits an event, which just sets a data property in the parent of bothponent-A
andponent-B
.The surrounding
<template>
tags are there to reduce additional markup for av-if
.The moment
renderSwitch
is set totrue
.ponent-a
gets created.
I have following kind of code:
<div>
<pA />
<pB />
</div>
How do I make sure that first pA
is rendered only after it pB
is rendered.
Why I want is I have some dependency on few elements of pA
, and style of pB
depends on presence of those elements.
Why in details:
I have some plex UI design, where one box will bee fixed when you scroll. SO It will not go above the screen when you scroll, it will be fixed once you start scrolling and it start touching the header. So I am using jquery-visible to find if a div
with a particular id
is visible on the screen, if it is not visible, I change the style and make that box fixed. Following code should give the idea what I am doing:
methods: {
onScroll () {
if ($('#divId').visible(false, false, 'vertical')) { // This is div from the pA, so I want to make sure it is rendered first and it is visible
this.isFixed = false
} else {
this.isFixed = true
}
}
},
mounted () {
window.addEventListener('scroll', this.onScroll() }
},
destroyed () {
window.removeEventListener('scroll', this.onScroll)
}
I dont want to make those in same ponent as one reason is it dont make sense as the nature of these ponents, and other I use pA
at many places, while pB
is specific to only one page. Also layout of these does not allow me to make pB
child of pA
as suggested in ments.
Any suggestions are wele.
I have following kind of code:
<div>
<pA />
<pB />
</div>
How do I make sure that first pA
is rendered only after it pB
is rendered.
Why I want is I have some dependency on few elements of pA
, and style of pB
depends on presence of those elements.
Why in details:
I have some plex UI design, where one box will bee fixed when you scroll. SO It will not go above the screen when you scroll, it will be fixed once you start scrolling and it start touching the header. So I am using jquery-visible to find if a div
with a particular id
is visible on the screen, if it is not visible, I change the style and make that box fixed. Following code should give the idea what I am doing:
methods: {
onScroll () {
if ($('#divId').visible(false, false, 'vertical')) { // This is div from the pA, so I want to make sure it is rendered first and it is visible
this.isFixed = false
} else {
this.isFixed = true
}
}
},
mounted () {
window.addEventListener('scroll', this.onScroll() }
},
destroyed () {
window.removeEventListener('scroll', this.onScroll)
}
I dont want to make those in same ponent as one reason is it dont make sense as the nature of these ponents, and other I use pA
at many places, while pB
is specific to only one page. Also layout of these does not allow me to make pB
child of pA
as suggested in ments.
Any suggestions are wele.
Share Improve this question edited Mar 29, 2017 at 4:47 Saurabh asked Mar 28, 2017 at 14:04 SaurabhSaurabh 73.7k44 gold badges191 silver badges251 bronze badges 8- My best shot is to use events. – Jonatas Walker Commented Mar 28, 2017 at 14:07
-
1
Could
CompB
not be a child ofCompA
sinceCompB
depends onCompA
? – Amresh Venugopal Commented Mar 28, 2017 at 14:11 - @AmreshVenugopal No I will not like to do that due to nature of these ponents. – Saurabh Commented Mar 28, 2017 at 14:18
- @JonatasWalker How to use events here? – Saurabh Commented Mar 28, 2017 at 14:19
-
2
Okay, another question I have is why is
CompB
depending on elements ofCompA
why not the data ofCompA
? Since the difference in the kinds of different elements would also be some kind ofv-if='true': make input tag'
v-else: make p-tag
like implementation? – Amresh Venugopal Commented Mar 28, 2017 at 14:23
2 Answers
Reset to default 3An option with events:
<!-- Parent -->
<div>
<p-a @rendered="rendered = true"></p-a>
<ponent :is="pB"></ponent>
</div>
<script>
// import ...
export default {
ponents: { CompA, CompB },
watch: {
rendered: function (val) {
if (val) this.pB = 'p-b';
}
},
data() {
return {
rendered: false,
pB: null
}
}
}
</script>
<!-- Component B -->
<script>
export default {
mounted() {
this.$emit('rendered');
}
}
</script>
After going through the edit I realised that the dependency is not data driven but event driven (onscroll). I have tried something and looks like it works (the setTimeout in the code is for demonstration).
My implementation is slightly different from that of Jonatas.
<div id="app">
RenderSwitch: {{ renderSwitch }} // for demonstration
<template v-if='renderSwitch'>
<p-a></p-a>
</template>
<p-b @rendered='renderSwitchSet'></p-b>
</div>
When the
ponent-B
is rendered it emits an event, which just sets a data property in the parent of bothponent-A
andponent-B
.The surrounding
<template>
tags are there to reduce additional markup for av-if
.The moment
renderSwitch
is set totrue
.ponent-a
gets created.
本文标签: javascriptHow to control order of rendering in vuejs for sibling componentStack Overflow
版权声明:本文标题:javascript - How to control order of rendering in vue.js for sibling component - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609591a2158925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论