admin管理员组文章数量:1356869
Suppose I want to use the ponents to make a list that would disappear if I click on it, and use the transition-group
to do the animation part.
The following code can perform well:
HTML:
<transition-group name="testanim">
<p key="1" v-if='open1' @click='open1 = false'>Can You See Me?</p>
<p key="2" v-if='open2' @click='open2 = false'>Can You See Me?</p>
</transition-group>
CSS:
.testanim-enter-active, .testanim-leave-active {
transition: all .5s;
}
.testanim-enter, .testanim-leave-to {
transform: translateX(1rem);
opacity: 0;
}
.testanim-leave-active {
position: absolute;
}
.testanim-move {
transition: all .5s;
}
open1
and open2
are defined in data
in Vue.js.
However, the following code would not perform the animation at all.
HTML:
<transition-group name="testanim">
<test-sth key="1"></test-sth>
<test-sth key="2"></test-sth>
</transition-group>
CSS: the same with above
JavaScript:
Vueponent ("test-sth", {
template: "<p v-if='open' @click='open = !open'>Can You See Me?</p>",
data: function () {
return {
open: true,
}
}
})
So the problem is that how I can animate the ponents inside the transition-group
. I've searched for a few hours but did not find some question or documents related to it.
Update:
The key problem is that the animation in the former example that the second sentence move upwards smoothly when the first sentense disappear do not show in the latter one. Although I may put the transition
inside the template
, That do not solve the problem.
Should I write the whole transition-group
inside the template
, or something else...?
Suppose I want to use the ponents to make a list that would disappear if I click on it, and use the transition-group
to do the animation part.
The following code can perform well:
HTML:
<transition-group name="testanim">
<p key="1" v-if='open1' @click='open1 = false'>Can You See Me?</p>
<p key="2" v-if='open2' @click='open2 = false'>Can You See Me?</p>
</transition-group>
CSS:
.testanim-enter-active, .testanim-leave-active {
transition: all .5s;
}
.testanim-enter, .testanim-leave-to {
transform: translateX(1rem);
opacity: 0;
}
.testanim-leave-active {
position: absolute;
}
.testanim-move {
transition: all .5s;
}
open1
and open2
are defined in data
in Vue.js.
However, the following code would not perform the animation at all.
HTML:
<transition-group name="testanim">
<test-sth key="1"></test-sth>
<test-sth key="2"></test-sth>
</transition-group>
CSS: the same with above
JavaScript:
Vue.ponent ("test-sth", {
template: "<p v-if='open' @click='open = !open'>Can You See Me?</p>",
data: function () {
return {
open: true,
}
}
})
So the problem is that how I can animate the ponents inside the transition-group
. I've searched for a few hours but did not find some question or documents related to it.
Update:
The key problem is that the animation in the former example that the second sentence move upwards smoothly when the first sentense disappear do not show in the latter one. Although I may put the transition
inside the template
, That do not solve the problem.
Should I write the whole transition-group
inside the template
, or something else...?
1 Answer
Reset to default 6When using Vue transitions, for internal reasons, the transition/transition-group ponents must be in the same template as the state that's being toggled.
Also, Vue ponents require that there always be a single root element for a ponent. A v-if
breaks this rule because it gives the possibility of the element not being there, if the v-if
happens to be false.
To solve your issue, move the transitioning to the test-sth
ponent. Since it manages its own toggling, it should manage its own transitioning as well.
Vue.ponent("test-sth", {
template: `
<transition name='testanim'>
<p v-if='open' @click='open = !open'>Can You See Me?</p>
</transition>
`,
data: () => ({
open: true,
}),
})
new Vue({
el: "#app",
template: `
<div>
<test-sth></test-sth>
<test-sth></test-sth>
</div>
`,
})
See this fiddle for a working example.
本文标签: javascriptHow does lttransitiongroupgt work with VuecomponentsStack Overflow
版权声明:本文标题:javascript - How does <transition-group> work with Vue.components? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743964186a2569584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论