简体   繁体   English

使用 js 附加和渲染一个 Vue 组件

[英]Appending and render a Vue component using js

I have a Vue component which is imported inside of a component file.我有一个Vue组件,它是在组件文件中导入的。 What I am trying to do is, I want to render a component which was imported using the append function.我想要做的是,我想渲染一个使用append函数导入的组件。 How can I do that?我怎样才能做到这一点?

<template>
    <JqxGrid :width="width" :source="dataAdapter" :columns="gridValues"
             :pageable="true" :autoheight="true" :sortable="true"
             :altrows="true" :enabletooltip="true" :editable="true"
             :selectionmode="'multiplecellsadvanced'"  :showtoolbar="true" :rendertoolbar="rendertoolbar">
    </JqxGrid>
</template>

<script>

    import JqxGrid from "../jqx-vue/vue_jqxgrid.vue";
    import Button from "./buttonComponent.vue";

    methods: {
        rendertoolbar: function (toolbar) {
        // this is where I am trying to add the other component but it is not rendering as a actual component, it is just spitting out as it is.
            toolbar.append($("<span style='margin: 5px;'>  <jqx-button class='custom-erp-button custom-btn-secondary' :button-name="+`Add`+"></jqx-button>  </span>"));
        },
        cellsrenderer: function (row, columnsfield, value, defaulthtml, columnproperties, rowdata) {
            if (value < 20) {
                return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #ff0000;">' + value + '</span>';
            }
            else {
                return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #008000;">' + value + '</span>';
            }
        }
    }
</script>

<style>
</style>

My first reaction was "you shouldn't be directly manipulating the DOM or constructing HTML strings" (or else why are you using Vue?), but you're using JqxGrid which appears to function this way (I'm not familiar with JqxGrid).我的第一反应是“你不应该直接操作 DOM 或构造 HTML 字符串”(否则你为什么使用 Vue?),但你使用的 JqxGrid 似乎以这种方式运行(我不熟悉 JqxGrid )。

In any case, what you're trying to render isn't strictly HTML, it's a Vue template string which requires compilation to a JavaScript render function (see Vue.compile() ).在任何情况下,您要渲染的内容并不是严格意义上的 HTML,而是需要编译为 JavaScript 渲染函数的 Vue 模板字符串(请参阅Vue.compile() )。 And even if you do manage to do that, I'm not sure it'll work with JqxGrid.即使您确实做到了,我也不确定它是否适用于 JqxGrid。

This is something that really should be done by JqxGrid by using scoped slots .这是真正应该由 JqxGrid 通过使用scoped slots来完成的事情。 If JqxGrid cannot support rendering Vue components like this, then I'm afraid you're out of luck.如果 JqxGrid 不能支持像这样渲染 Vue 组件,那恐怕你就倒霉了。

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

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