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


相关推荐

  • 简历项目

    简历项目

    2021年5月20日
    123
  • C语言 stat 函数「建议收藏」

    C语言 stat 函数「建议收藏」C语言stat函数stat头文件:#include<sys/stat.h>#include<unistd.h>函数原型:intstat(constchar*file_name,structstat*buf)**函数说明:**stat函数获取file_name指向文件的文件状态,并将文件信息保存到结构体buf中,执行成功返回0,失…

    2022年8月21日
    17
  • win10恶意软件删除工具[通俗易懂]

    win10恶意软件删除工具[通俗易懂]win+r,输入mrt弹出操作界面点击下一步,根据需要进行扫描,扫描完成后根据情况进行处理

    2022年6月24日
    41
  • 手机打开照相机_安卓11调用第三方相机

    手机打开照相机_安卓11调用第三方相机打开相机布局代码<Buttonandroid:id=”@+id/take_photo”android:layout_width=”match_parent”android:layout_height=”wrap_content”android:layout_gravity=”center”android:text=”打开相机”/><Buttonandroid:id=”@+id/chose_photo”andro

    2022年9月22日
    0
  • python获取当前时间的时间戳_python精确到毫秒时间戳

    python获取当前时间的时间戳_python精确到毫秒时间戳在Python中可以使用来自模块time、datetime或calendar的函数来获取当前时间戳,代码语句如【importtime;ts=time.time()print(ts)】。在Python中,有多种方法可以获得当前时间戳。如果希望在Python中获得时间戳,可以使用来自模块time、datetime或calendar的函数。使用模块time模块time是提供各种与时间相关的功能。…

    2022年10月2日
    0
  • 设置ntp服务器同步时间_安卓设置ntp服务器地址

    设置ntp服务器同步时间_安卓设置ntp服务器地址有时服务器需要调整时区并调整时间,需要用到的命令:ntpdate一般Linux系统都默认安装了NTP服务,如果没有安装的话,也可以直接使用yum安装,yum安装命令为:yuminstall-yntpdate首先修改一下时区为上海时区:cp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime然后选择国家授时中心的服务器地址:ntpdate210…

    2022年5月3日
    307

发表回复

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

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