javascript - VueJS 和“vue-markdown”,当触发 `rendered` 事件时 DOM 仍未准备好
问题描述
我有这个:
<vue-markdown :source="content" v-on:rendered="afterRenderContent"></vue-markdown>
但是当afterRenderContent
触发该方法时,HTML 元素仍然不存在。
我想在渲染后进行 DOM HTML 元素操作,但我不能。等待DOM准备好的正确方法是什么?
我试图创建一个 JSFiddle 来重现该问题,但看起来 CDN 库没有正确加载:
解决方案
Try moving your afterRenderContent method to the mounted lifecycle hook. You can only manipulate DOM elements after the component has been mounted.
mounted() {
this.afterRenderContent()
}
More on lifecycle methods here: https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram.
I notice you are also using some vanilla javascript, in your js fiddle, refs can be used in vue to access DOM elements in a more intuitive fashion .
<template>
<h1 ref="header-one">Header One</h1>
</template>
// in your component method or mounted hook
mounted() {
// access the header element
this.$refs['header-one']
}
EDIT: On a deeper dive Perhaps this might work, hooking into the mounted lifecycle method from a child component.
<vue-markdown :source="content" @hook:mounted="afterRenderContent"</vue-markdown>
Decent blog on how to use refs with vue here https://blog.logrocket.com/how-to-use-refs-to-access-your-application-dom-in-vue-js/
推荐阅读
- php - php时间计算问题
- python - Sklearn OneVsRestClassifier - 获取目标类所有可能性的概率
- codenameone - 仅在选择特定选项卡时解析数据
- php - 使用 PHP 阅读电子邮件内容
- machine-learning - 如何将训练数据集拆分为训练、验证和测试数据集?
- python - 使用 skimage 提取图像区域
- android - 录制暂停后如何在同一视图中播放录制的视频?
- reactjs - 反应选择显示日期选择
- twilio - Twilio 调查外拨电话
- javascript - 随着javascript websockets的使用,内存使用量逐渐增加。为什么?