[英]Prop in template not showing with VueJS
I've been trying to pass a prop in a component's template. 我一直在尝试在组件模板中传递道具。 I think I'm missing some points here, and I didn't start to include my components in single files yet.
我想这里缺少一些要点,而且我还没有开始将组件包含在单个文件中。
app.js app.js
Vue.component('chat-response', {
props: ['response', 'senderClass'],
template: '<div>From {{ senderClass }} : {{ response.text }}</div>'
})
var app = new Vue({
el: '#app_chat',
data: {
responseList: [
{ id: 0, text: 'Response 1', type: 'Owner' },
{ id: 1, text: 'Response 2', type: 'Other' },
{ id: 2, text: 'Response 3', type: 'None' }
]
}
})
page.html page.html
...
<chat-response v-for="response in responseList"
v-bind:key="response.id"
v-bind:response="response"
v-bind:senderClass="response.type"></chat-response>
...
Output : 输出 :
From : Response 1
From : Response 2
From : Response 3
As we see, senderClass won't show up. 如我们所见,senderClass将不会显示。 I've tried different methods and only got errors that I could understand after reading around.
我尝试了不同的方法,但在阅读后才发现我可以理解的错误。
I don't wish to use response.type instead of senderClass because in the meantime, I'm setting senderClass after mounted with a real css class. 我不希望使用response.type代替senderClass,因为与此同时,我在安装了真正的CSS类之后设置了senderClass 。
Maybe it's my approach that's completely wrong, could you give me some hints ? 也许是我的方法完全错误,您能给我一些提示吗?
I think the name of your property is wrong. 我认为您的财产名称错误。 Just change in page.html
v-bind:senderClass="response.type"
to v-bind:sender-class="response.type"
只需将page.html
v-bind:senderClass="response.type"
更改为v-bind:sender-class="response.type"
http://jsfiddle.net/eywraw8t/310360/ http://jsfiddle.net/eywraw8t/310360/
HTML attribute names are case-insensitive. HTML属性名称不区分大小写。 Any uppercase character will be interpreted as lowercase.
任何大写字符将被解释为小写。 So camelCased prop names need to use their kebab-cased equivalents.
因此,骆驼式道具名称需要使用与烤肉串相同的名称。
Apart from what Jns said You could get rid of v-bind altogether and just use : varibaleName
for bindings. 除了Jns所说的以外,您可以完全摆脱v-bind,而只需使用:
varibaleName
进行绑定。
Link to fiddle https://jsfiddle.net/50wL7mdz/654614/#&togetherjs=J9vgbNaR7m 链接到小提琴https://jsfiddle.net/50wL7mdz/654614/#&togetherjs=J9vgbNaR7m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.