【问题标题】:VueJS re-compile HTML in an inline-template componentVueJS 在内联模板组件中重新编译 HTML
【发布时间】:2016-03-09 07:38:22
【问题描述】:

我已将 bootstrapTable (https://github.com/wenzhixin/bootstrap-table) 包装到指令中,如下所示:

Vue.directive('bootstraptable', {
    priority: 1000,

    params: ['url', 'resource-name'],

    bind: function () {

        var _self = this;

        $(this.el)
            .bootstrapTable({
                pagination: true,
                pageSize: 15,
                pageList: [],
                sidePagination: 'server',
                url: this.params.url,
                queryParams: function (params) {
                    return params;
                },
                cookie: true,
                cookieExpire: '24h',
                cookieIdTable: this.params.resourceName + '-table',
                locale: 'it-IT'
            }).on('load-success.bs.table', function (e, data) {

                $('[data-toggle="tooltip"]').tooltip();
                _self.vm.$compile(_self.vm.$el);
            });
    },
    update: function (value) {
        $(this.el).val(value)
    },
    unbind: function () {
        $(this.el).off().bootstrapTable('destroy')
    }
});

从服务器返回的 JSON 包含一个带有 v-on 指令的按钮,因此我必须重新编译注入的 HTML 行以使按钮指令正常工作。 无论如何,下面的代码似乎不起作用:

_self.vm.$compile(_self.vm.$el);

我是否遗漏了一些明显的东西?

【问题讨论】:

  • 你试过$mount吗?
  • 组件已经挂载,事实上,如果我调用$mount,这就是我得到的:[Vue warn]: $mount() should be called only once.

标签: javascript vue.js bootstrap-table


【解决方案1】:

$compile 方法需要在必须编译的元素上调用,而不是在 vm 根元素上。

我换行了:

_self.vm.$compile(_self.vm.$el);

与:

            _.each($('[recompile]'), function(el){
                _self.vm.$compile(el);
            });

并为所有需要重新编译的 HTML 元素添加了“recompile”属性。

这似乎按预期工作,如果有更传统的方法,请不要犹豫回答。

【讨论】:

  • 这对我来说非常有效。非常感谢你。就我而言:var th = this; _.each($('[recompile=true]'), function(el){ th.$compile(el); $(el).removeAttr('recompile'); });
猜你喜欢
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 2019-07-22
  • 1970-01-01
  • 2017-09-19
  • 2015-06-06
  • 1970-01-01
相关资源
最近更新 更多