将变量值从js文件传递给hbs组件
Passing variable value from js file to hbs component
您好,我在 js 文件中有一个全局变量,我想在 hbs 文件中检索该变量的值
export default () => {
selectedViolationTypeId: 0,
addProgram(program) {
this.set('selectedViolationTypeId', program.ViolationTypeId);
},
}
selectedViolationTypeId 的值在 addProgram 函数调用中发生变化,我想在 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 字段中,这个选定的值显示为空,错误消息如下
: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 文件中检索它的值吗?
您可以通过在组件/控制器中定义一个 属性 来使全局变量值在 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}}
您好,我在 js 文件中有一个全局变量,我想在 hbs 文件中检索该变量的值
export default () => {
selectedViolationTypeId: 0,
addProgram(program) {
this.set('selectedViolationTypeId', program.ViolationTypeId);
},
}
selectedViolationTypeId 的值在 addProgram 函数调用中发生变化,我想在 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 字段中,这个选定的值显示为空,错误消息如下
: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 文件中检索它的值吗?
您可以通过在组件/控制器中定义一个 属性 来使全局变量值在 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}}