简体   繁体   English

VueJS 渲染没有被调用

[英]VueJS render doesn't get called

At work we are working with VueJS and we want to render one element with the contents from a 2D array.在工作中,我们正在使用 VueJS,我们希望使用 2D 数组中的内容来渲染一个元素。 This needs 2 for loops to iterate through which isn't possible with a template.这需要 2 个 for 循环来迭代,而模板是不可能的。 We found out we can do this with the render() function.我们发现我们可以使用render()函数来做到这一点。

Our code (but simplified a bit) is:我们的代码(但稍微简化了一点)是:

export default {
    name: 'od-table',
    data() {
        return {
            testData: []
        }
    },
    render() {
        console.log("render function")
    }
};

When we include this component in the project the render function doesn't get called.当我们在项目中包含这个组件时,渲染函数不会被调用。 I can see the component has loaded because I can read the test-data.我可以看到组件已加载,因为我可以读取测试数据。 No logs or any other sign the render function has been called.没有日志或任何其他迹象表明render函数已被调用。

It could be it's an obvious mistake since we have never got this working before, but since this is something we have to use for this problem we would like to know what is wrong with the code.这可能是一个明显的错误,因为我们以前从来没有这样工作过,但是由于这是我们必须用于解决这个问题的东西,我们想知道代码有什么问题。

Given it worked when you recreated your component file from scratch, the issue might have been that in your original file you had mistakenly left a section.鉴于它在您从头开始重新创建组件文件时有效,问题可能是您在原始文件中错误地留下了一个部分。 That's what happened to me.这就是发生在我身上的事。 If your component comes with its own <template> , it will be used for rendering and your render() function will never get called at all.如果您的组件带有自己的<template> ,它将用于渲染,并且您的render()函数将永远不会被调用。

Any particular reason why you're using render ?您使用render的任何特殊原因? You might be looking for mounted .您可能正在寻找已mounted的 .

Here is a complete list of lifecycle hooks for Vue.js 2.0.这是 Vue.js 2.0 的生命周期钩子的完整列表。 https://v2.vuejs.org/v2/api/#Options-Lifecycle-Hooks https://v2.vuejs.org/v2/api/#Options-Lifecycle-Hooks

我仍然不知道出了什么问题,但是删除组件并在新文件中复制旧代码似乎可以解决问题。

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

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