简体   繁体   English

渲染后的Vue组件事件

[英]Vue component event after render

Is there an event in Vue that gets fired after an element re-renders? Vue 中是否存在在元素重新渲染后触发的事件? I have an object that updates, which causes my template to update.我有一个更新的 object,这会导致我的模板更新。 After it updates I want to trigger an event which runs a jQuery function.更新后,我想触发一个运行 jQuery function 的事件。 The code looks like this:代码如下所示:

template: `
    <div class="undo-margin">
        <div class="gradient">
            <img v-bind:src="blogPost.thumbnail"/>
            <h1 class="align-bottom">{{blogPost.title}}</h1>
        </div>

        <div class="container bg-faded">
            <article v-html="blogPost.article"></article>
        </div>
    </div>

`,
props: ["blogid"],
data: function () {
    return blogPageData;
},
mounted: function () {
    const blogid = jQuery("#blogPage").attr("blogid");
    if (this.authdata.roles.indexOf("Administrator") !== -1) {
        this.isAdmin = true;
    }

    this.getBlogPost(blogid);
},
methods: {
    getBlogPost: function (blogid) {
        var element = this;
        $.ajax({
            url: `/api/blog/${blogid}`,
            type: "get",
            headers: headers,
            success: function (data) {
                if (data === undefined) {
                    window.location.replace("/blog");
                } else {
                    element.isDetails = true;
                    element.blogPost = data;
                }
            },
            error: function (data) {
                window.location.replace("/blog");
                errorData.error = data.responseText;
            }
        });
    }
},
watch: {
    blogPost: function () {
        console.log(this.blogPost);
        jQuery("pre").each((i, e) => {
            hljs.highlightBlock(e);
        });
    }
}

As you see, I tried using the watch event, however when the watch event triggers, my page is not updated yet.如您所见,我尝试使用 watch 事件,但是当 watch 事件触发时,我的页面还没有更新。 blogPost however has been changed, but vue is still rendering the page.然而 blogPost 已更改,但 vue 仍在渲染页面。 I know I could theoretically fix this by adding a setTimeout, but I would prefer something cleaner.我知道理论上我可以通过添加 setTimeout 来解决这个问题,但我更喜欢更干净的东西。

updated might be what you're looking for. updated的可能是您正在寻找的。

Vue2 https://v2.vuejs.org/v2/api/#updated Vue2 https://v2.vuejs.org/v2/api/#updated

Edit: Now that Vue3 is the current version here are the links to the relevant lifecycle hooks.编辑:现在 Vue3 是当前版本,这里是相关生命周期挂钩的链接。

Vue3 Composition API https://vuejs.org/api/composition-api-lifecycle.html#onupdated Vue3 组合 API https://vuejs.org/api/composition-api-lifecycle.html#onupdated

Vue3 Options API https://vuejs.org/api/options-lifecycle.html#updated Vue3 选项 API https://vuejs.org/api/options-lifecycle.html#updated

updated() should be what you're looking for: updated()应该是您要查找的内容:

Called after a data change causes the virtual DOM to be re-rendered and patched.在数据更改后调用会导致重新渲染和修补虚拟 DOM。

The component's DOM will have been updated when this hook is called, so you can perform DOM-dependent operations here.调用此钩子时,组件的 DOM 将已更新,因此您可以在此处执行依赖于 DOM 的操作。

If you are using Vue 3 , you can utilize the nextTick() method.如果你使用的是Vue 3 ,你可以使用 nextTick() 方法。 This method takes a callback, which will be called after the DOM has been updated due to a state change.此方法接受一个回调,该回调将在由于 state 更改而更新 DOM 后调用。

I have a similar situation where I need to trigger a change event on an element with jQuery on a legacy app.我有类似的情况,我需要在旧应用程序上使用 jQuery 触发元素上的更改事件。 In my case I'm handling an event dispatched in a child component.就我而言,我正在处理在子组件中调度的事件。 Here's a rough example of my use case.这是我的用例的一个粗略示例。

<template>
  <childComponent @valueChanged="triggerElementChange"/>
  <input ref="element" type="hidden" name="elementName"/>
</template>

<script setup>
import {ref, nextTick} from 'vue'
import ChildComponent from './ChildComponent'

// reference to element I want to trigger change on
// (identified by ref attribute with matching name)
const element = ref(null)

function triggerElementChange(){
  // state updated via child component prior to emitting valueChanged event

  // wait until DOM is updated, then trigger jQuery change event on referenced element
  nextTick(() => {
    $(element.value).change()
  })
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM