问题描述
这是我试用的代码:
它在jsfiddle中没有任何错误地正确运行
尝试在我的计算机上通过本地web服务器运行相同的程序不起作用。
正在加载以下文件:
在Firefox 中出现此错误:
TypeError:this._input is null @ http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js:364
在chrome 中出现此错误:
未捕获的类型错误:无法在匹配= this._input.match(this.rules [rules [i]]下调用null
的匹配方法'match' ); in handlebars-1.0.0.beta.6.js
前面提到的一个非常类似的问题这个,但似乎仍然是开放的。
所以问题是为什么当这个错误在jsfiddle中正常工作时会发生?
在本地运行的正确方法是什么?
该错误意味着#当你尝试使用它时,entry-template
不在DOM中:
var source = $( #入门模板)HTML()。 // DOM中没有#entry-template这里
var template = Handlebars.compile(source);
这意味着你最终试图编译 undefined
作为Handlebars模板,并且不起作用。您可以运行以下代码查看错误:
打开您的控制台。
jsfiddle的工作原理是因为你已经在DOM加载后运行了所有代码,这就是默认的jsfiddle行为。
在你的真实代码中加载顺序问题,试着把它包装在 $(function(){/*...*/})
wrapper中。
This is the code I am trying out: http://jsfiddle.net/sbrsK/10/
It runs correctly without any error in jsfiddle
Trying to run the same via a web-server locally on my computer does not work.The following files are being loaded:
- index.html
- app.js
In Firefox I get this error:
TypeError: this._input is null @ http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js:364
In chrome I get this error:
Uncaught TypeError:Cannot call method 'match' of null
under match = this._input.match(this.rules[rules[i]]); in handlebars-1.0.0.beta.6.js
There is a very similar problem raised earlier in this link, but seems that it is still open.
So the question is why is this error happening when it works correctly in jsfiddle?What is the correct way to run this locally?
That error means that #entry-template
isn't in the DOM when you try to use it:
var source = $("#entry-template").html(); // There is no #entry-template in the DOM here
var template = Handlebars.compile(source);
That means that you end up trying to compile undefined
as a Handlebars template and that doesn't work. You can see the error by running this:
with your console open.
The jsfiddle works because you have all your code running after the DOM has been loaded, that's the default jsfiddle behavior.
You probably have a load order problem in your real code, try wrapping it in a $(function() { /*...*/ })
wrapper.
这篇关于为什么浏览器返回错误“TypeError:this._input为空”在使用handlebars.js时,Firefox(以及类似的Chrome)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!