是否可以使用Vue.js将数据从模板发送到脚本?(与v-bind相反)
我想知道是否可以将v-for循环中的当前数据(在模板中)发送到方法中的函数是否可以使用Vue.js将数据从模板发送到脚本?(与v-bind相反),vue.js,Vue.js,我想知道是否可以将v-for循环中的当前数据(在模板中)发送到方法中的函数 <li v-for="annonce in annonces"> <article> //Here for example I want to send annonce.categorie to the methods //object of the component to change the color in function of the //anon
<li v-for="annonce in annonces">
<article>
//Here for example I want to send annonce.categorie to the methods
//object of the component to change the color in function of the
//anonce.categorie string
<a v-bind:style="['color' : {{getColorCat(annonce)}}]">
{{annonce.categorie}}
</a>
</article>
</li>
//举个例子,我想将annonce.categorie发送到方法
//组件的对象,以在函数中更改颜色
//分类字符串
{{annonce.categorie}
编辑
只需在v-bind:style中添加要发送的参数的函数,并创建一个在循环中当前元素的函数中返回字符串颜色的方法
<a v-bind:style="getStyle(annonce)">{{annonce.categorie}}</a>
methods:{
getStyle(annonce) {
return { color: this.getColorCat(annonce) };
},
getColorCat(annonce) {
switch(annonce.categorie)
{
case this.categories[0] :
return 'limegreen'
break;
case this.categories[1] :
return 'grey'
break;
case this.categories[2] :
return 'deepskyblue'
break;
case this.categories[3] :
return '#E3E01F'
break;
case this.categories[4] :
return 'silver'
break;
case this.categories[5] :
return 'pink'
break;
case this.categories[6] :
return 'red'
break;
case this.categories[7] :
return 'green'
break;
case this.categories[8] :
return 'purple'
break;
case this.categories[9] :
return 'magenta'
break;
default :
}
}
},
{{annonce.categorie}
方法:{
getStyle(annonce){
返回{color:this.getColorCat(annonce)};
},
getColorCat(annonce){
开关(annonce.categorie)
{
这种情况。类别[0]:
返回“limegreen”
打破
这种情况。类别[1]:
返回“灰色”
打破
这种情况。类别[2]:
返回“深蓝”
打破
这种情况。类别[3]:
返回“#E3E01F”
打破
这种情况。类别[4]:
返回“银”
打破
这种情况。类别[5]:
返回“粉红色”
打破
这种情况。类别[6]:
返回“红色”
打破
这种情况。类别[7]:
返回“绿色”
打破
这种情况。类别[8]:
返回“紫色”
打破
这种情况。类别[9]:
返回“洋红”
打破
违约:
}
}
},
是的,您可以用一种简单的方式完成
<ul>
<li v-for="(item, index) in items">
{{getText(index)}}
</li>
</ul>
更新为添加:
您也可以使用方法来设置属性值,但示例中的语法是错误的。有几种方法可以解决此问题,但有一种方法是:
<a v-bind:style="getStyles(annonce)">
是的,你可以用一种直截了当的方式
<ul>
<li v-for="(item, index) in items">
{{getText(index)}}
</li>
</ul>
更新为添加:
您也可以使用方法来设置属性值,但示例中的语法是错误的。有几种方法可以解决此问题,但有一种方法是:
<a v-bind:style="getStyles(annonce)">
哇,谢谢你的快速回答。我现在在我的代码中测试它。{{annonce.categorie}}可以这样做吗?我想在字符串annonce.categie的函数中更改{annonce.categie}的颜色。您想做的当然是可能的,但语法不正确。也许可以看看魔兽世界谢谢你的快速回答。我现在在我的代码中测试它。{{annonce.categorie}}可以这样做吗?我想在字符串annonce.categie的函数中更改{annonce.categie}的颜色。您想做的当然是可能的,但语法不正确。也许可以看看