Javascript 未捕获的类型错误:$template.get 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36641618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:21:22  来源:igfitidea点击:

Uncaught TypeError: $template.get is not a function

javascriptjquerywordpress

提问by roykasa

I am getting this error in WordPress when I am using a plugin called WpBakery Visual Composer.

当我使用名为 WpBakery Visual Composer 的插件时,我在 WordPress 中收到此错误。

I am using the latest version of WordPress (4.5), using the latest Google Chrome version and all plugins are updated. I cannot seem to be able to add any elements or templates using Visual Composer.

我正在使用最新版本的 WordPress (4.5),使用最新的 Google Chrome 版本,并且所有插件都已更新。我似乎无法使用 Visual Composer 添加任何元素或模板。

Can someone help me or tell me what could be going on and how I can fix this error.

有人可以帮助我或告诉我可能会发生什么以及我如何解决此错误。

The error I get:

我得到的错误:

enter image description here

在此处输入图片说明

回答by Ben

Please see my answer here.

在此处查看我的回答

I fixed this bug by updating the html2elementfunction to:

我通过将html2element函数更新为:

html2element: function(html) {
        var $template, attributes = {},
            template = html;
        $template = $(template(this.model.toJSON()).trim()), _.each($template.get(0).attributes, function(attr) {
            attributes[attr.name] = attr.value
        }), this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()
    },

in /wp-content/plugins/js_composer/assets/js/backend/composer-view.js

/wp-content/plugins/js_composer/assets/js/backend/composer-view.js

or in wp-content/plugins/js_composer/assets/js/dist/backend.min.js`

或在 wp-content/plugins/js_composer/assets/js/dist/backend.min.js`

Hope this works for you!

希望这对你有用!

回答by Renegade_Mtl

@Sorin Haidau

@索林海道

Hi guys, I am using the Astra theme. This fix is 99.9 % working. For some tho, this only stops the spinning wheel, but once the page loads visual composer does not.

大家好,我正在使用 Astra 主题。此修复程序 99.9% 有效。对于某些人来说,这只会停止旋转轮,但是一旦页面加载视觉作曲家就不会。

I made a slight change to this code (that is posted everywhere by now)

我对这段代码做了一点改动(现在到处都张贴了)

Original Astra theme code here (composer-view.js)

此处的原始 Astra 主题代码(composer-view.js)

        html2element:function (html) {
        var attributes = {},
            $template;
        if (_.isString(html)) {
            this.template = _.template(html);
            $template = $(this.template(this.model.toJSON()).trim());
        } else {
            this.template = html;
            $template = html;
        }
        _.each($template.get(0).attributes, function (attr) {
            attributes[attr.name] = attr.value;
        });
        this.$el.attr(attributes).html($template.html());
        this.setContent();
        this.renderContent();
    },

The code that works :

有效的代码:

html2element: function(html) {
    var $template, 
    attributes = {},
    template = html;
    $template = $(template(this.model.toJSON()).trim()), 
     _.each($template.get(0).attributes, function(attr) {
    attributes[attr.name] = attr.value
}); this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()
},

The main difference is located here (versus original code)

主要区别在这里(相对于原始代码)

}); this.$el.attr

There is a semicolon instead of the original comma :) :

有一个分号而不是原来的逗号 :) :

}), this.$el.attr

Cheers folks :)

干杯人:)

Update : This fixed about 19 out of 20 sites I had with theme astra with the same error as mentioned above... all except one site.

更新:这修复了我使用主题 astra 的 20 个站点中的大约 19 个,并出现了与上述相同的错误......除了一个站点之外的所有站点。

I had this error after the visual composer finally appeared (missing half of the design elements)

我在视觉作曲家终于出现后出现了这个错误(缺少一半的设计元素)

Uncaught Error: Syntax error, unrecognized expression: .ui-tabs-nav [href=#tab-1415196282-1-8]

I fixed this by updating the line 552 of the composer-custom-views.js :

我通过更新 composer-custom-views.js 的第 552 行来解决这个问题:

$('.ui-tabs-nav [href="#tab-' + params.tab_id + '"]').text(params.title);

And voilà everything now works. Im sorry if it does not work for all themes, do try to use the code mentioned by others above. If that does not work try my solutions :)

瞧,现在一切正常。对不起,如果它不适用于所有主题,请尝试使用上面其他人提到的代码。如果这不起作用,请尝试我的解决方案:)

回答by Elon Zito

Someone posted on the WordPress forums this solution which worked for me.

有人在 WordPress 论坛上发布了这个对我有用的解决方案。

Replace the html2elementfunction in /wp-content/plugins/js_composer/assets/js/backend/composer-view.jswith the following.

html2element函数替换/wp-content/plugins/js_composer/assets/js/backend/composer-view.js为以下内容。

html2element: function(html) {
            var $template, attributes = {},
                template = html;
            $template = $(template(this.model.toJSON()).trim()), _.each($template.get(0).attributes, function(attr) {
                attributes[attr.name] = attr.value
            }), this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()
        },

Edit: I had to make this replacement a second time under different circumstances, and it didn't start working until I disabled and then reenabled both the Visual Composer plugin andthe Ultimate Visual Composer add-on.

编辑:我不得不在不同情况下再次进行此替换,直到我禁用并重新启用 Visual Composer 插件Ultimate Visual Composer 附加组件后,它才开始工作。

回答by Amritosh pandey

Noticed that code was not being passed into the html2element function, but did exist in the function calling it (render)

注意到代码没有被传递到 html2element 函数中,但确实存在于调用它的函数中(渲染)

The following code has completely corrected my problems, I can load the page, add, clone, remove, etc

以下代码完全纠正了我的问题,我可以加载页面,添加,克隆,删除等

render: function () {
   var $shortcode_template_el = $( '#vc_shortcode-template-' + this.model.get( 'shortcode' ) );
   if ( $shortcode_template_el.is( 'script' ) ) {
    var newHtmlCode =  _.template( $shortcode_template_el.html(),
            this.model.toJSON(),
            vc.templateOptions.default );
    if(!_.isString(newHtmlCode)){
     newHtmlCode = $shortcode_template_el.html();
    }
    this.html2element( newHtmlCode );
   } else {
    var params = this.model.get( 'params' );
    $.ajax( {
     type: 'POST',
     url: window.ajaxurl,
     data: {
      action: 'wpb_get_element_backend_html',
      data_element: this.model.get( 'shortcode' ),
      data_width: _.isUndefined( params.width ) ? '1/1' : params.width,
      _vcnonce: window.vcAdminNonce
     },
     dataType: 'html',
     context: this
    } ).done( function ( html ) {
     this.html2element( html );
    } );
   }
   this.model.view = this;
   this.$controls_buttons = this.$el.find( '.vc_controls > :first' );
   return this;
  },

回答by Shamppi

Funny thing... my visual composer version number was way above the latest update (4.8.*).

有趣的是……我的视觉作曲家版本号远高于最新更新 (4.8.*)。

Anyways...I had this same problem and those previous answers did not completelysolve my problems, so I decided to try and buy a new copy of the plugin. It worked out well.

无论如何......我遇到了同样的问题,之前的那些答案并没有完全解决我的问题,所以我决定尝试购买该插件的新副本。效果很好。

I can now confirm that the Visual Composer version 4.12.1 works with the WordPress 4.6.1 without errors.

我现在可以确认 Visual Composer 版本 4.12.1 与 WordPress 4.6.1 一起工作没有错误。

Also note this:

还要注意这一点:

There is no such directory as backendor such a file called composer-view.jsin the latest version of Visual Composer.

在最新版本的 Visual Composer 中没有这样的目录backend或这样的文件composer-view.js

PS.Using these GUI page builders sucks ***.

附注。使用这些 GUI 页面构建器很糟糕 ***。

回答by mindlogixtech

a latest fix in December 2016, for Visual composer fix is this,

2016 年 12 月的最新修复,对于 Visual composer 修复是这样的,

html2element:function(html){var $template,attributes={},template=vc.template(html);$template=$(template(this.model.toJSON()).trim()),_.each($template.get(0).attributes,function(attr){attributes[attr.name]=attr.value}),this.$el.attr(attributes).html($template.html()),this.setContent(),this.renderContent()},

notice this important thing, template=vc.template(html);in the actual code. this is the quick fix in latest version, compatible with WPordpress latest.

注意这个重要的事情,template=vc.template(html); 在实际代码中。这是最新版本的快速修复,与 WPordpress 最新版本兼容。

Remember to update Wordpress and theme accordingly, so this fix should work fine.

请记住相应地更新 Wordpress 和主题,因此此修复程序应该可以正常工作。

thanks and happy coding

感谢和快乐编码

回答by PKInd007

this worked for me for WordPress version 4.9.8

这对我的 WordPress 版本 4.9.8 有用

html2element:function (html) {
    var attributes = {}, 
        $template;
    if (_.isString(html)) {
        this.template = _.template(html);
    } else {
        try {
            this.template = _.template(html());                                                                                                                          
        } catch (err) {
            this.template = html;
        }   
    }   
    $template = $(this.template(this.model.toJSON()).trim());
    _.each($template.get(0).attributes, function (attr) {
        attributes[attr.name] = attr.value;
    }); 
    this.$el.attr(attributes).html($template.html());
    this.setContent();
    this.renderContent();
},

回答by MoJa

for anyone comes here from google search: after change html2element function if you have this error Cannot read property 'attributes' update render: function to below code

对于任何从谷歌搜索来到这里的人:更改 html2element 函数后,如果出现此错误无法读取属性“属性”更新渲染:函数到以下代码

render: function () {
        var $shortcode_template_el = $( '#vc_shortcode-template-' + this.model.get( 'shortcode' ) );
        if ( $shortcode_template_el.is( 'script' ) ) {
            var newHtmlCode =  _.template( $shortcode_template_el.html(),
                                            this.model.toJSON(),
                                            vc.templateOptions.default );
            if(!_.isString(newHtmlCode)){
                newHtmlCode = $shortcode_template_el.html();
            }
            this.html2element( newHtmlCode );
        } else {
            var params = this.model.get( 'params' );
            $.ajax( {
                type: 'POST',
                url: window.ajaxurl,
                data: {
                    action: 'wpb_get_element_backend_html',
                    data_element: this.model.get( 'shortcode' ),
                    data_width: _.isUndefined( params.width ) ? '1/1' : params.width,
                    _vcnonce: window.vcAdminNonce
                },
                dataType: 'html',
                context: this
            } ).done( function ( html ) {
                this.html2element( html );
            } );
        }
        this.model.view = this;
        this.$controls_buttons = this.$el.find( '.vc_controls > :first' );
        return this;
    },