【问题标题】:How to use the knockoutJS foreach in Magento 2如何在 Magento 2 中使用 knockoutJS foreach
【发布时间】:2020-07-03 09:05:26
【问题描述】:

我正在与 knockoutJS 和 Magento 2 进行一场伟大的战斗。我很享受它,因为我知道我会学到一些新的和有用的东西。目前,我正在尝试使用 foreach。我从这里得到了关于 magento 核心文件的示例,但我仍然缺少一些东西。这是我的代码:

文件.html

<div class="component-wrapper">
<ul>
<!-- ko foreach: { data: items, as: 'item' } -->
    <li data-bind="text: item.code"></li>
<!-- /ko -->
</ul>


<span data-bind="text: getTitle()"></span>

文件.js:

define([
    'ko',
    'jquery',
    'uiComponent',
    'Magento_Checkout/js/model/quote'
], function(ko, $, Component, quote) {
    'use strict';
    return Component.extend({


        getTitle : function() {
            return "My Function is here";
        },

        items : function(){
           const  items = [
                    {
                        id : '0001',
                        code : '123123'
                    },
                    {
                        id : '0001',
                        code : '123123'
                    }
                ];
            return items;
        },

        initialize: function () {
            console.log(this.items());
            this._super();
        },

        isActive : function(){
            return true;
        },

    });
});

前端的打印屏幕:

我正在尝试在前端显示项目。 有人可以在这里救我吗:S:D

谢谢

【问题讨论】:

    标签: knockout.js magento2


    【解决方案1】:

    我明白了。

    我错过了“()”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2012-07-06
      相关资源
      最近更新 更多