将动态数据属性传递给 Meteor 中的内容块
Pass dynamic data attribute to content block in Meteor
我在 Blaze 中有一个 button
s 的动态模板,看起来像这样(简化):
button.html
<template name="Button">
<button {{attributes}}>
<span class="button__text">{{> UI.contentBlock}}</span>
</button>
</template>
button.js
import {Template} from 'meteor/templating';
import cx from 'classnames';
import './button.html';
Template.Button.helpers({
attributes() {
const instance = Template.instance();
let {data} = instance;
return {
disabled: data.disabled,
'class': cx('button', data.class)
};
}
});
尝试设置动态数据属性:
{{#Button class="js-add-contact" data-phase-index={{index}}}}Add Contact{{/Button}}
将 index
(假设它只是一个简单的动态字符串)插入 data-phase-index
会引发错误:内容块不期望 {{
。我不确定将动态数据导入模板的另一种方法。还有一个问题是在 attributes()
帮助程序中获取 Button 识别的 data-
属性。谁能解决这个问题?
只需 data-phase-index=index
即可。
由于您已经在 Button
模板调用的双花括号内,Meteor 知道它将获得解释值。例如,看到您必须在 class="js-add-contact"
.
中的字符串周围使用引号
像往常一样,Meteor 将尝试从助手或数据上下文中解释 index
。
我在 Blaze 中有一个 button
s 的动态模板,看起来像这样(简化):
button.html
<template name="Button">
<button {{attributes}}>
<span class="button__text">{{> UI.contentBlock}}</span>
</button>
</template>
button.js
import {Template} from 'meteor/templating';
import cx from 'classnames';
import './button.html';
Template.Button.helpers({
attributes() {
const instance = Template.instance();
let {data} = instance;
return {
disabled: data.disabled,
'class': cx('button', data.class)
};
}
});
尝试设置动态数据属性:
{{#Button class="js-add-contact" data-phase-index={{index}}}}Add Contact{{/Button}}
将 index
(假设它只是一个简单的动态字符串)插入 data-phase-index
会引发错误:内容块不期望 {{
。我不确定将动态数据导入模板的另一种方法。还有一个问题是在 attributes()
帮助程序中获取 Button 识别的 data-
属性。谁能解决这个问题?
只需 data-phase-index=index
即可。
由于您已经在 Button
模板调用的双花括号内,Meteor 知道它将获得解释值。例如,看到您必须在 class="js-add-contact"
.
像往常一样,Meteor 将尝试从助手或数据上下文中解释 index
。