Uncaught TypeError: this.template is not a function Backbonejs

Uncaught TypeError: this.template is not a function Backbonejs

我正在 Rails 工作,在车把模板中使用 BackboneJS。

我在这里遇到一个奇怪的错误.. 这是我的 header 视图

class App.Views.Header extends Backbone.View

  className: "navbar-inner"
  template: HandlebarsTemplates['app/templates/header']

  render: ->
    @$el.html(@template())
    @

主要的应用文件是这个

#= require_self
#= require_tree ./templates
#= require_tree ./views
#= require_tree ./routers

window.App =
  Routers: {}
  Views: {}
  Collections: {}
  Models: {}
  initialize: ->
    new App.Routers.MainRouter()
    Backbone.history.start()

我的主路由器文件是这个

class App.Routers.MainRouter extends Backbone.Router

  routes:
    "": "index"

  initialize: ->
    @headerView = new App.Views.Header()

  index: ->
    $("#header").html(@headerView.render().el)

当我点击 localhost:3000.. 我预先得到了这个错误。 Uncaught TypeError: this.template is not a function..

我完全陷入困境,我们将不胜感激谢谢

听起来像HandlebarsTemplates['app/templates/header']中的Handlebars模板不存在,还没有被编译,或者在编译时可能有错误。如果该值为 null 或未定义,这就是您会得到的错误。

您可能想尝试在调用 render 时在浏览器的 javascript 调试器中设置断点,然后使用调试器检查 this.template 的值并查看发生了什么上。

template: HandlebarsTemplates['header']

模板路径应该只是模板本身。

可能是由于预编译的版本或资产导致的版本