【问题标题】:How to pass data variable from each to the with in Meteor?如何将数据变量从每个传递到流星中的with?
【发布时间】:2020-12-17 03:13:12
【问题描述】:

我正在尝试将 git 变量传递给使用 with 包装的设置,如下所示 如果我们现在可以看到它是 setting.gitlab,但我想让它像 setting.git 一样动态,其中 git 是每个循环中提到的变量。

 {{#each git in gitlabFields}}
        {{#with settings.gitlab}}
        <div data-value={{@index}}>{{git}}</div>
        <div>hihi</div>
            <div class="rc-user-info__row">
                <div class="rc-input">
                    <label class="rc-input__label">
                        <div >
                                
                                <div class="rc-input__title" style="display: inline-block;" >{{_ label}}{{equal default value '*'}}</div>
                                
                                
                        </div>
                    <!-- {{#each gitlabFields}} -->
                    <div id="dynamicFields">
                            <div class="rc-input__wrapper" >
                                    <input type="text" name="{{git}}" value="{{value}}" class="rc-input__element js-input" disabled="{{./disabled}}"/>
                                </div>
                    </div>
                        
                        <!-- {{/each}} -->
                    </label>
                </div>
            </div>
        {{/with}}
        {{/each}}

git 变量在 settings.git 中不可访问 它显示未定义。

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    假设settings 是一个可访问的对象,您可以编写一个帮助器来解析该值:

    Template.myTemplate.helpers({
      getGitSettings (settings, key) {
        return settings[key]
      }
    })
    

    如果您想将settings 从模板中解耦或避免将其传递给整个显示列表,您也可以在模板模块中将其定义为私有变量:

    const gitSettings = { ... };
    
    Template.myTemplate.helpers({
      getGitSettings (settings, key) {
        return gitSettings[key]
      }
    })
    

    如果在许多模板中使用此模式,您还可以定义一个全局帮助器:

    const gitSettings = { ... };
    
    Template.registerHelper('gitSettings', function (key) {
     return gitSettings[key]
    })
    

    并通过

    使用它
    {{#each field in gitlabFields}}
      {{#with gitSettings field}}...{{/with}}
    {{/each}}
    

    【讨论】:

    • 如何动态传递我们从数组中获取的数据上下文,如 settings.x , settings.y
    • 请改进您的问题。如果您有更复杂的案例,那么您的问题代码不够明确。
    猜你喜欢
    • 2020-08-21
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2023-03-24
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多