javascript 未捕获的类型错误:无法调用未定义 underscore.js 的方法“替换”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18994799/
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-10-27 13:59:59  来源:igfitidea点击:

Uncaught TypeError: Cannot call method 'replace' of undefined underscore.js

javascriptjquerybackbone.jsunderscore.js

提问by titi

I'm newbie to backbone.jsand underscore.js.

我是backbone.js和 的新手underscore.js

HTML :

HTML :

<div id="cartlist">
<script type="text/template" id="cart_template">

</script>
</div>

Where I called the view file :

我在哪里调用视图文件:

<script type="text/javascript" src="webcore/views/CartView.js"></script>
</body>

JS function(it works well with the javascript project):

JS 函数(它适用于 javascript 项目):

 function Cart(){
    ......
    this.showCart = function (){
    var item = deserializeJSONToObj(window.localStorage.getItem(Cart.storageName));
    var str = '<table id="showcart">';
    str += '<tr><td class="cartitemhead">Item to buy</td><td class="cartitemhead" style="text-align: center;">Quantity</td></tr>';
    $.each(item, function(i, item) {
        str += '<tr><td><table class="verticallist"><tr><td rowspan="4" style="width: 120px;"><img src="' + item.PictureName + '" alt="Product" width="95px"/></td><td style="font-weight: bold;">'+trimString(item.Name,50)+'</td></tr><tr><td><i>Available in Stock(s)!</i></td></tr><tr><td><i>Rating:   650Va-390w Input:   Single</i></td></tr></table></td><td class="centertxt">'+item.QuantityInCart+'</td></tr>';
    });
    str += '</table>';
    return str;
 } 

This is the Views :

这是意见:

var myCart = new Cart();
CartList = Backbone.View.extend({
initialize:function(){
    this.render();
},
render: function(){
    var template = _.template( $("#cart_template").html(), {} );
    this.$el.html( template );
}
});
var cart_view = new CartList({ el: $("#cartlist").html(myCart.showCart()) });
  1. when I trying to call the view template, I am getting error Uncaught TypeError: Cannot call method 'replace' of undefined - underscore.js. Please help me to find the mistake.

  2. How can I convert strstring in Cartclass to a templateof underscore.js.

  1. 当我尝试调用视图模板时,出现错误Uncaught TypeError: Cannot call method 'replace' of undefined - underscore.js。请帮我找出错误。

  2. 如何将类中的str字符串转换为.Carttemplateunderscore.js

Any help would be much appreciated, thank you.

任何帮助将不胜感激,谢谢。

回答by mu is too short

I'd guess that your problem is where you're storing #cart_templatein the DOM:

我猜你的问题是你#cart_template在 DOM 中存储的位置:

<div id="cartlist">
    <script type="text/template" id="cart_template">
    </script>
</div>

You create your CartListlike this:

你创建你CartList这样的:

var cart_view = new CartList({ el: $("#cartlist").html(myCart.showCart()) });

When you say this:

当你这样说时:

$("#cartlist").html(myCart.showCart())

everything that was inside #cartlistis gone, in particular, #cart_templateis gone. Then inside CartList, you try to do this:

里面的一切都#cartlist消失了,尤其#cart_template是消失了。然后在里面CartList,你尝试这样做:

_.template( $("#cart_template").html(), {} );

But there is no more #cart_templateat that point so $('#cart_template').html()is undefinedand that's where your error comes from: _.templatewill call replaceinternally but you're giving it undefinedas the template instead of a string.

但没有更多的#cart_template在这一点所以$('#cart_template').html()undefined和您的错误来自于的:_.template将调用replace内部,但你给它undefined作为模板,而不是一个字符串。

The solution is to move your #cart_templateoutside #cartlist.

解决办法是移动你的#cart_template外部#cartlist

I'd also recommend that you not pass the elto the view's constructor, your view should create its own eland the caller should put that elwhere it wants it in the DOM. That way the view is wholly responsible for its element and you'll have fewer zombie and event problems.

我还建议您不要将 传递el给视图的构造函数,您的视图应该创建自己的视图el,并且调用者应该将el它放在 DOM 中它想要的位置。这样,视图对其元素完全负责,并且您将有更少的僵尸和事件问题。