nodejs创建vue项目_vue数据不渲染

nodejs创建vue项目_vue数据不渲染tl;dr:GivenaVueJSVNodeobject,howdoIgettheHTMLelementthatwouldbegeneratedifitwererendered?e.g.:>temp1VNode{tag:”h1″,data:undefined,children:Array(1),text:undefined,elm:…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

nodejs创建vue项目_vue数据不渲染

tl;dr:

Given a VueJS VNode object, how do I get the HTML element that would be generated if it were rendered?

e.g.:

> temp1

VNode {tag: “h1”, data: undefined, children: Array(1), text: undefined, elm: undefined, …}

> temp1.children[0]

VNode {tag: undefined, data: undefined, children: undefined, text: “Test”, elm: undefined, …}

> doSomething(temp1)

Test

Goal

I’m attempting to build a small VueJS wrapper around the DataTables.net library.

To mimic the behavior of HTML tables in my markup, I want something like the following:

NameAgeSalary

{
{ person.name }}{
{ person.age }}{
{ person.salary }}

What I’ve done so far

I’ve started to implement this as follows:

DataTable.vue

/* global $ */

export default {

data: () => ({

instance: null

}),

mounted() {

this.instance = $(this.$refs.table).dataTable();

this.$el.addEventListener(“dt.row_added”, function(e) {

this.addRow(e.detail);

});

},

methods: {

addRow(row) {

// TODO

console.log(row);

}

}

};

DataTableRow.vue

/* global CustomEvent */

export default {

mounted() {

this.$nextTick(() => {

this.$el.dispatchEvent(new CustomEvent(“dt.row_added”, {

bubbles: true,

detail: this.$slots.default.filter(col => col.tag === “td”)

}));

});

},

render() { return “”; }

};

What this currently does:

When the page loads, the DataTable is initialized. So the column headers are properly formatted and I see “Showing 0 to 0 of 0 entries” in the bottom left

The CustomEvent is able to bubble up past the

and be caught by the DataTable element successfully (circumventing the limitation in VueJS that you can’t listen to events on slots)

What this does not do:

Actually add the row

My event is giving me an array of VNode objects. There’s one VNode per column in my row. The DataTables API has an addRow function which can be called like so:

this.instance.row.add([“col1”, “col2”, “col3”]);

In my case, I want the resultant element from the rendering of the VNode to be the elements in this array.

var elems = [];

for (var i = 0; i < row.length; i++)

elems[i] = compile(row[i]);

this.instance.row.add(elems);

Unfortunately this compile method eludes me. I tried skimming the VueJS documentation and I tried Googling it, but no dice. I tried manually passing the createElement function (the parameter passed to the render method) but this threw an error. How can I ask VueJS to render a VNode without injecting the result into the DOM?

解决方案

I ran into the same issue wanting to do basically the same thing with a row details template for DataTables.net.

One solution could be to create a generic component that renders out a VNode and instantiate that programmatically. Here is how my setup for a dynamic detail row that I insert using datatable’s row.child() API.

RenderNode.js

export default {

props: [‘node’],

render(h, context) {

return this.node ? this.node : ”

}

}

Datatables.vue

Include the renderer component from above

import Vue from ‘vue’

import nodeRenderer from ‘./RenderNode’

Instantiate and mount the renderer to get the compiled HTML

// Assume we have `myVNode` and want its compiled HTML

const DetailConstructor = Vue.extend(nodeRenderer)

const detailRenderer = new DetailConstructor({

propsData: {

node: myVNode

}

})

detailRenderer.$mount()

// detailRenderer.$el is now a compiled DOM element

row.child(detailRenderer.$el).show()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234437.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 算法–切割的数组

    算法–切割的数组

    2022年1月10日
    51
  • Matlab中length函数的使用

    Matlab中length函数的使用目录一.语法1.输入参数二.说明三.示例1.向量元素数2.矩形矩阵的长度3.字符串数组4.结构体字段的长度length函数是求最大数组维度的长度。一.语法L=length(X)1.输入参数X-输入数组标量|向量|矩阵|多维数组输入数组,指定为标量、向量、矩阵或多维数组。复数支持:是提示 要计算字符串或字符向量中的字符数量,请使用strlength函数。 length不对表执行运算。要检查…

    2022年6月4日
    66
  • java verifycode_JavaWeb基础—VerifyCode源码

    java verifycode_JavaWeb基础—VerifyCode源码1packagecom.jiangbei.verifycodeutils;23importjava.awt.BasicStroke;4importjava.awt.Color;5importjava.awt.Font;6importjava.awt.Graphics2D;7importjava.awt.image.BufferedImage;8importjava.io.IOExce…

    2022年7月15日
    29
  • eruda.js 移动端调试神器使用教程(eruda)

    eruda.js 移动端调试神器使用教程(eruda)在日常的移动端开发时,一般都是试用chrome浏览器的移动端模式进行开发和调试,只有在chrome调试完成,没有问题了才会上到真机测试,移动端开发的一大问题就在于此,各种品牌各种型号手机,手机中各种类型的浏览器APP…还好移动端的相对一致点,但是往往都会有一些各种各样的坑,这时候就蛋疼了,明明chrome调试工具中是正常的,一到某个浏览器中就炸了,怎么办,又无法像在chrome中使用调试工具进行调试,只能通过alert()弹窗来调试,有什么办法可以像PC上那样清晰,可视化的调试呢?使用示例://#

    2025年7月23日
    2
  • BZOJ 2588 Count on a tree (COT) 是持久的段树

    BZOJ 2588 Count on a tree (COT) 是持久的段树

    2022年1月5日
    47
  • 防盗链原理

    防盗链原理引子:明明引用了一个正确的图片地址,但显示出来的却是一个红叉或写有“此图片仅限于***网站用户交流沟通使用”之类的“假图片”(下图便是网易博客的防盗链效果)。用嗅探软件找到了多媒体资源的真实地址用下载软件仍然不能下载。下载一些资源时总是出错,如果确认地址没错的话,大多数情况都是遇上防盗链系统了。常见的防盗链系统,一般使用在图片、音视频、软件等相关的资源上。        一、什么是

    2022年7月23日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号