【问题标题】:Passing variable value from js file to hbs component将变量值从 js 文件传递​​给 hbs 组件
【发布时间】:2021-10-14 22:26:09
【问题描述】:

您好,我在 js 文件中有一个全局变量,我想在 hbs 文件中检索该变量的值

    export default () => {
    selectedViolationTypeId: 0,
    addProgram(program) {
         this.set('selectedViolationTypeId', program.ViolationTypeId);
     },
    }

在 addProgram 函数调用中 selectedViolationTypeId 的值发生了变化,我想在 hbs 文件中检索更改的值,在 URL 字段中,如下所示: url='api/branch/inspectionitemcategories/dt?violationTypeId={selectedViolationTypeId}'

 <div class="col-md-8">
                    {{log 'selectedViolationTypeId'}}
                    {{log selectedViolationTypeId}}
                        <div class="form-group chosen-fix {{if failTest 'has-error q'}}">
                            <label class="control-label">Inspection Item Categories</label>
                            {{#drop-down url='api/branch/inspectionitemcategories/dt?violationTypeId={selectedViolationTypeId}'
                            id='idInspectionItemCategories'
                            required=false
                            placeholder='Add Program (Search)'
                            showSelected=false f=f as |item|
                            }}
                            {{log 'item 1'}}
                            {{log item}}
                            {{#if item}}
                            {{partial 'components/edit-item/partial-select1'}}
                            {{else}}
                            <i>Nothing Here</i>
                            {{/if}}
                            {{/drop-down}}
                        </div>
                    </div>

但它没有发生,在 url 字段中,这个选定的值是 null 并带有如下错误消息

:56974/api/branch/inspectionitemcategories/dt?violationTypeId={{this.selectedViolationTypeId}}:1 Failed to load resource: the server responded with a status of 400 (Bad Request)
api.js:77 api/branch/inspectionitemcategories/dt?violationTypeId={{this.selectedViolationTypeId}} error undefined Object

有人可以帮我如何在前端 hbs 文件中检索它的值吗?

【问题讨论】:

    标签: javascript html ember.js


    【解决方案1】:

    您可以通过在组件/控制器中定义一个属性来使全局变量值在 hbs 模板中可见。所以,在你的组件代码中,定义一个 getter:

    import Component from '@glimmer/component';
    
    export default class MyComponent extends Component {
      get violationTypeId() {
        return selectedViolationTypeId;
      }
    }
    

    现在你可以在模板中使用它了:

    {{log this.violationTypeId}}
    

    P。 S. 你可以通过服务使用这个全局变量。如果值在逻辑上属于现有服务,则将其添加到现有服务中,或者创建一个新服务:

    import Service from '@ember/service';
    import { action } from '@ember/object';
    import { tracked } from '@glimmer/tracking';
    
    export default class MyService extends Service {
      @tracked selectedViolationTypeId = DEFAULT_VALUE;
    
      @action addProgram(program) {
        this.selectedViolationTypeId = program?.ViolationTypeId;
      }
    }
    

    然后,您可以将此服务注入到您的组件/控制器中:

    import Component from '@glimmer/component';
    import { inject as service } from '@ember/service';
    
    export default class MyComponent extends Component {
      @service myService;
    }
    

    并使用模板中的值:

    {{log this.myService.selectedViolationTypeId}}
    

    【讨论】:

    • 如果我没有此组件的类但它具有导出默认功能,并且如果我在另一个函数中设置此值,例如 saveProgram(program) { this.set('selectedViolationTypeId ', program.ViolationTypeId);},那么我该如何找回呢?这个值在 saveProgram 函数中发生了变化,那我该怎么办呢?
    • 这与您最初提出的问题完全不同。我建议遵循官方的“数据下降,行动上升”范式。如果您需要在您的应用中全局访问此值,另一种选择是考虑将其作为某些服务的属性进行访问。
    • 但是怎么做呢?你能给我的朋友举个例子吗
    • 我的朋友,我已经更新了我的答案。我不确定这是否有帮助,但我希望有帮助。
    • 我相信您将能够轻松地自己实现该功能:)
    猜你喜欢
    • 2020-10-29
    • 2010-11-18
    • 1970-01-01
    • 2017-05-19
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多