通过“ this”关键字无法访问的车把对象值

时间:2020-07-07 09:04:58

标签: node.js express-handlebars handlebarshelper

我正在hbs模板上渲染一个数组,但无法使用'this'关键字渲染对象值。也尝试了没有“ this”但没有用的方法。请检查图片以获取更多说明。

{{#each products }}
<div class="row">
    {{#each this }}
    {{!-- {{ printing oject  }} --}}

    <p>{{this}}</p>

    {{!-- {{ actual-render  }} --}}
    <div class="col-sm-6 col-md-4">
        <div class="thumbnail">
            <img src="" alt="..." class="image-responsive">
            <div class="caption">
                <h3>{{ this.title }}</h3>
                <p class="description">{{ this.description }}</p>
                <div class="clearfix">
                    <div class="price float-left">{{ this.price }}$</div>
                    <a href="#" class="btn btn-success float-right" role="button">Button</a>
                </div>
            </div>
        </div>
    </div>
    {{/each }}
</div>
{{/each }}

Code

output

1 个答案:

答案 0 :(得分:0)

该问题已通过允许对hbs的原型访问得以解决

我在app.js上关注了

第1步

npm install @handlebars/allow-prototype-access

第2步在app.js的开头添加这些行

 const Handlebars = require('handlebars')
 const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')

第3步。更改行

const hbs = exphbs.create({
  defaultLayout: 'main',  
  extname: 'hbs',
});

const hbs = exphbs.create({
  defaultLayout: 'main', 
  extname: 'hbs',
  handlebars: allowInsecurePrototypeAccess(Handlebars)
});