【问题标题】:The handlebar helper function did not working?车把辅助功能不起作用?
【发布时间】:2018-11-02 15:52:11
【问题描述】:

我正在观看视频中的 fn 关键字或函数,无论它是什么都没有显示给我。 我尝试了太多时间也没有在文档中得到正确的参考,如果显示我做错的问题,这对我很有帮助..

edit.handlebars

{{#select post.status}}
            <option value="public">Public</option>
            <option value="private">Private</option>
            <option value="draft">Draft</option>
    {{/select}}

这是我的 handlebar_helper.js 文件

handlebar_helper.js

    module.exports = (Handlebars)=>{
  Handlebars.registerHelper('select', function(selected, options) {
    return options.fn(this).replace(new RegExp('value = \"'+ selected + '\"'), '$&selected="selected"');
  });

};

主文件 app.js 包括设置视图引擎

app.js

const {select} = require('./helpers/handlebars_helpers')

app.engine('handlebars', exphbs({defaultLayout: 'home', helpers: {select: select}}));
app.set('view engine', 'handlebars');

【问题讨论】:

  • 你确定options.fn 不是未定义的吗?

标签: node.js regex express


【解决方案1】:

来自 handlerbars 文档:

Handlebars.registerHelper('select', function(options) { return options.fn(this); });

您的辅助函数中的参数似乎有误。

【讨论】:

  • 它显示错误句柄。registerHelper 显示 ':' 预期。将其更改为冒号后显示此错误(ReferenceError: registerHelper is not defined)
  • 我是否必须为此车把或 registerhelper 安装任何模块我只安装了 express-handlebars 模块
  • #Bwaxxlo 请帮帮我
  • Bwaxxlo 我做了些微改动它显示错误缺少帮助选择
猜你喜欢
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多