jul*_*les 3 javascript vue.js vue-component
我想一个变量传递给应添加作为一个组件class到div。但是,Vue 添加了名称variable而不是内容。
所以我传递了color包含red.
我想要<div class="red">2</div>
什么:我得到什么:<div class="color">2</div>
我认为这是一个菜鸟问题,很抱歉。也许有更好的方法来做到这一点。
谢谢你的协助。
这是我的组件:
<template>
<div class="btn btn-outline-primary cell"
:class="{color}"
:id="id">
{{ number }}
</div>
</template>
<script>
export default {
name: "TileElement",
props: ["id", "number", "color"]
}
</script>
<style scoped>
.green {
color: green;
}
.red {
color: red;
}
.yellow {
color: yellow;
}
.cell {
display: inline-block;
float: left;
margin: 0.1em;
text-align: center;
background-color: lightgray;
width: 2.7em;
height: 2.7em;
}
</style>
Run Code Online (Sandbox Code Playgroud)
父组件:
<template>
<div class="row">
<TileElement
v-for="tile in tiles"
v-bind:key="tile.id"
v-bind:number="tile.number"
v-bind:color="tile.color"
></TileElement>
</div>
</template>
<script>
import TileElement from './TileElement.vue';
export default {
name: "TileRow",
components: {
TileElement
},
data: function () {
return {
tiles: [
{id: 1, number: 1, color: "green"},
{id: 2, number: 2, color: "red"},
{id: 3, number: 3, color: "yellos"}
]
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果你只需要传递一个类,没有任何条件或其他类似的东西,那么你可以简单地将数组用于一个和任何其他数量的类:
<div :class="[color]"></div>
Run Code Online (Sandbox Code Playgroud)
但这不仅是你能做到的。
https://vuejs.org/v2/guide/class-and-style.html
| 归档时间: |
|
| 查看次数: |
5415 次 |
| 最近记录: |