export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App', arr: [ {msg: '666', index: '1'}, {msg: '777', index: '2'}, {msg: '888', index: '1'}, {msg: '999', index: '2'} ] } } }
标签遍历并判断class类
<template> <div class="hello"> <h1>{{ msg }}</h1> <h2>Essential Links</h2> <ul> <li v-for="(item, index) in arr" :key="index" :class="item.index === '1'?'hello2':''"> {{item.msg}} </li> </ul> </div> </template>
定义css
.hello2{ color: red }
完整代码
<template> <div class="hello"> <h1>{{ msg }}</h1> <h2>Essential Links</h2> <ul> <li v-for="(item, index) in arr" :key="index" :class="item.index === '1'?'hello2':''"> {{item.msg}} </li> </ul> </div> </template> <script> export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App', arr: [ {msg: '666', index: '1'}, {msg: '777', index: '2'}, {msg: '888', index: '1'}, {msg: '999', index: '2'} ] } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .hello2{ color: red } h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>
本文作者为gengboxb,转载请注明。