javascript - Vuetify: Hide a skeleton loader after a element loads
问题描述
I've been trying to create a simple skeleton loader in Vuetify that is shown while the document's resources (like images) are being loaded and then hidden (replaced with a <v-card>
) when everything is fully loaded.
Currently, I have this <v-skeleton-loader>
:
<v-skeleton-loader v-if="loading == true" :loading="loading" transition="fade-transition" type="card">
</v-skeleton-loader>
My v-card
element uses v-show
like this:
<v-card max-width="344" v-show="loaded">
For reactive properties, I have:
data: () => ({
loading: true,
loaded: false
})
I am trying to update the values of those properties using the updated
lifecycle hook of Vue.js:
updated: function() {
if (document.readyState == 'complete') {
this.loading = false;
this.loaded = true;
}
}
Currently, Vue Devtools tells me that the properties are not being updated with their new values, defined in the updated
hook. How can I change that so the values are properly updated when the readyState
of the DOM has changed to complete
?
解决方案
updated
只有在更新组件时才会调用钩子。Vue 生命周期与 DOM 事件无关。如果页面加载后没有更新,updated
则不会触发。
这可以通过常规 DOM 事件侦听器来完成:
created() {
const readyHandler = () => {
if (document.readyState == 'complete') {
this.loading = false;
this.loaded = true;
document.removeEventListener('readystatechange', readyHandler);
}
};
document.addEventListener('readystatechange', readyHandler);
readyHandler(); // in case the component has been instantiated lately after loading
}
推荐阅读
- sql - 选择不在两个日期之间的日期范围内的数据
- python - 在底部的 Dataframe 上插入一行并计算总和
- python - 如何从表中抓取特定的 td 标签?
- javascript - 对 js 中的原型链行为感到困惑
- aws-appsync - 获取 appsync 解析模板中的 count 值
- javascript - ChartJS - 雷达图表选项不起作用
- javascript - ReactJS 货币转换器 :: 如何仅显示选定的货币
- reactjs - 循环作为道具发送的数组时,React 不会渲染子组件数据
- flutter - 是否可以像 HTML 一样使 Flutter 中的文本高亮显示?
- ios - 如何在 Swift 中设置自定义字体的字体粗细