gpt4 book ai didi

javascript - Vue组件渲染时如何触发事件?

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:22 25 4
gpt4 key购买 nike

我用谷歌搜索了很多,但没有找到任何关于这个的信息。

我想在 Vue 渲染内容时淡入我的内容。我的应用程序很大,需要一些时间才能为用户做好准备。但是当 Vue 将内容插入 block 时,CSS 动画不想工作。请参阅下面列出的代码 JSFiddle .

HTML

<div id="my-app">
<p>Weeverfish round whitefish bass tarpon lighthousefish mullet tigerperch bangus knifefish coley Black sea bass tompot blenny madtom tapetail yellow-eye mullet..</p>

<hr>

<example></example>
</div>

CSS

#my-app {
opacity: 0;
transition: 2s;
}

#my-app.visible {
opacity: 1;
transition: 2s;
}

JavaScript

// Fade in animation will work if you comment this ...
Vue.component('example', {
template: `<p>Starry flounder loach catfish burma danio, three-toothed puffer hake skilfish spookfish New Zealand sand diver. Smooth dogfish longnose dace herring smelt goldfish zebra bullhead shark pipefish cow shark.</p>`
});

// ... and this
const app = new Vue({
el: '#my-app',

// Makes content visible, but don't provides fade-in animation
/*
created: function () {
$('#my-app').addClass('visible')
}
*/
});

// Makes content visible, but don't provides fade-in animation
// with enabled Vue
$('#my-app').addClass('visible');

// As you can see, animation with Vue works only here
setInterval(() => {
$('#my-app').toggleClass('visible');
}, 5000);

此外,我没有找到任何内置的 Vue 解决方案(事件、方法等)来在 Vue 渲染时运行代码。像 loadDOMContentLoaded 这样的事件也没有帮助。 created 也没有提供预期的结果:

const app = new Vue({
el: '#my-app',
// Makes content visible, but don't provides fade-in animation
created: function () {
$('#my-app').addClass('visible')
}
});

有人知道解决我的问题的好方法吗?

谢谢。

最佳答案

非常感谢@David L@Bill Criswell用于指向Transition Effects 。我用这段代码达到了预期的结果:

HTML

<div id="my-app">
<app>
<p>Weeverfish round whitefish bass tarpon lighthousefish mullet tigerperch bangus knifefish coley Black sea bass tompot blenny madtom tapetail yellow-eye mullet..</p>

<hr>

<example></example>
</app>
</div>

CSS

.fade-enter-active, .fade-leave-active {
transition: opacity 1s
}

.fade-enter, .fade-leave-active {
opacity: 0
}

JavaScript

Vue.component('app', {
data: function () {
return {
show: false
}
},
mounted: function() {
this.show = true;
},
template: `<div>
<transition name="fade">
<div v-if="show">
<slot></slot>
</div>
</transition>
</div>`,
});

Vue.component('example', {
template: `<p>Starry flounder loach catfish burma danio, three-toothed puffer hake skilfish spookfish New Zealand sand diver. Smooth dogfish longnose dace herring smelt goldfish zebra bullhead shark pipefish cow shark.</p>`
});

const app = new Vue({
el: '#my-app',
});

这里是JSFiddle与工作结果。

关于javascript - Vue组件渲染时如何触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com