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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • win11安装node并且配置环境变量

    win11安装node并且配置环境变量npm使用过程中的一些错误解决办法及npm常用命令和技巧-世有因果知因求果-博客园用户名是自己的C:\Users\KenKen\AppData\Roaming\npmNODE_PATHC:\ProgramFiles\nodejs\node_modules

    2022年5月31日
    156
  • java代码大全_java新手入门-java新手代码大全[通俗易懂]

    java代码大全_java新手入门-java新手代码大全[通俗易懂]在开发中,我们常常需要使用到测试功能,因为我们不确定运行真正的代码会发生什么事情,今天我们就来介绍一下该如何在idea中进行junit单元测试吧。一、依赖安装Junit4Idea默认是使用的arquillianjunit4作为测试框架,这里我们我们把它改为Junit4。Settings->Plugins->搜索栏中搜索Junit并勾选->安装完成二、开…

    2022年7月7日
    24
  • mariadb安装教程linux,在Ubuntu 18.04系统上安装MariaDB 10.4的步骤「建议收藏」

    mariadb安装教程linux,在Ubuntu 18.04系统上安装MariaDB 10.4的步骤「建议收藏」以下介绍在Ubuntu18.04系统上安装MariaDB10.4,也适用在Ubuntu16.04系统上,我们可以从APT存储库在Ubuntu上安装MariaDB10.4。MariaDB是MySQL的直接替代品,具有更多功能,新存储引擎和更好的性能。在写本文时,MariaDB10.4可在Beta版本中使用了,如果你想使用正式版本,就用MariaDB10.3。相关链接在Ubuntu18….

    2022年5月27日
    91
  • CentOS7 安装 kafka「建议收藏」

    CentOS7 安装 kafka「建议收藏」文章目录一、安装JDK二、安装zookeeper三、安装kafka一、安装JDKrpm-qa|grepjavarpm-qa|grepjdkrpm-qa|grepgcjrpm-qa|grepjava|xargsrpm-e–nodeps #卸载老版本yumlistjava-1.8*yuminstalljava-1.8.0-openjdk*-yjava-version二、安装zookeeper官方地址:https://ar

    2022年6月6日
    26
  • Linux pstack命令[通俗易懂]

    Linux pstack命令[通俗易懂]概要打印运行进程的栈信息(快照),包括一个进程下的所有线程的栈信息。语法pstackpid解释pstack是封装了gdb功能的shell脚本,通过”threadapplyallbt”的命令获得输出所有的线程堆栈信息,再用sed进行替换和过滤#RunGDB,stripoutunwantednoise.$GDB–quiet$readnever-nx/proc/$1/exe$1<<EOF2>&1|

    2022年9月14日
    0
  • OpenProcessToken令牌函数使用方法

    OpenProcessToken令牌函数使用方法转载自:https://www.cnblogs.com/mengfanrong/p/5185850.html GetCurrentProcessID得到当前进程的IDOpenProcessToken得到进程的令牌句柄LookupPrivilegeValue查询进程的权限AdjustTokenPrivileges调整令牌权限要对一个随意进程(包括系统安全进程和服务进程)进行指定…

    2022年6月25日
    20

发表回复

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

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