vue.js - 使用 Vue JS 切换活动类
问题描述
我有一个按钮列表,我想切换活动类,但从列表中的所有其他按钮中删除活动类。目前,我有活动类切换,但是当单击另一个按钮时,其他按钮保持活动状态。
更新:感谢@skirtle 的帮助,我能够解决我的问题。
<li class="tc-product-form__color"
@click="colorToggle"
:class="{ 'tc-product-form__color--selected' : currentColor === color }">
<button role="button" type="button"
:data-color="color" :data-title="name"
class="tc-product-form__color-btn js-product-swatch border border-solid"
:style="backgroundStyle">
</button>
</li>
这是我的组件。
export default{
props: ['swatch', 'currentColor'],
computed: {
backgroundStyle () {
if (this.swatch.background.includes('url')) {
return `backgroundImage: ${this.swatch.background}`
} else {
return `backgroundColor: ${this.swatch.background}`
}
},
color () {
return this.swatch.color
},
name () {
return this.swatch.name
}
},
methods: {
colorToggle () {
this.$emit('change-color', this.color)
}
}
}
https://jsfiddle.net/w37vLL68/158/正是我正在寻找的但是,我不明白我做错了什么无法完成结果。任何输入将不胜感激,在此先感谢您。
解决方案
从您发布的代码中确定有点困难,但看起来问题是每个样本/按钮组件都有自己的currentColor
. 要获得您想要的单选按钮行为,您需要currentColor
在它们之间共享一个属性。
您可以通过让父组件拥有该属性然后使用道具将其传递下来来实现这一点。然后,当单击以更新父级时,样本/按钮组件将触发一个事件。
有点像这样:
const swatch = {
template: `
<li class="tc-product-form__color"
@click="colorToggle"
:class="{ 'tc-product-form__color--selected' : currentColor === color }">
<button role="button" type="button"
:data-color="color" :data-title="name"
class="tc-product-form__color-btn js-product-swatch border border-solid"
:style="backgroundStyle">
</button>
</li>
`,
props: ['swatch', 'currentColor'],
computed: {
backgroundStyle () {
if (this.swatch.background.includes('url')) {
return `backgroundImage: ${this.swatch.background}`
} else {
return `backgroundColor: ${this.swatch.background}`
}
},
color () {
return this.swatch.color
},
name () {
return this.swatch.name
},
needsBorder: function() {
}
},
methods: {
colorToggle () {
this.$emit('change-color', this.color)
}
}
}
new Vue({
el: '#app',
components: {
swatch
},
data () {
return {
currentColor: null,
swatches: [
{name: 'Red', color: '#f00', background: '#f77'},
{name: 'Green', color: '#0f0', background: '#7f7'},
{name: 'Blue', color: '#00f', background: '#77f'}
]
}
},
methods: {
onChangeColor (color) {
this.currentColor = color
}
}
})
.tc-product-form__color {
list-style: none;
}
.tc-product-form__color-btn {
border: 1px solid #000;
height: 40px;
width: 40px;
}
.tc-product-form__color--selected {
background: #fdd;
border: 1px solid #f00;
}
<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
<div id="app">
<ul>
<swatch
v-for="swatch in swatches"
:key="swatch.color"
:swatch="swatch"
:current-color="currentColor"
@change-color="onChangeColor"
></swatch>
</ul>
</div>
其他几点注意事项:
- 避免
this
在模板中使用。我已更改this.currentColor
为currentColor
. - 属性
color
和name
应该是计算属性。将值从道具复制到data
通常是不必要的并且通常是有害的,因为如果道具更改,值将不会更新。在某些情况下,这是理想的行为,但通常不是。
我试图让你的大部分代码保持原样,这样你就可以看到我做了什么。要关注的关键部分是current-color
道具和change-color
事件。
推荐阅读
- expo - @react-native-async-storage/async-storage 在使用 expo 生成 ipa 或 apk 时不起作用
- laravel - 在Laravel 8中将文件上传到firebase后如何获取下载链接
- .net - EF Core 迁移在更改身份 PK 数据类型时失败
- flutter - 如何将 csv 数据从文件选择器转换为动态列表?
- c# - C#:将证书链导出到 Base64 编码文件
- javascript - 我正在尝试覆盖小部件 DomainLeaf 中的函数 _getOperatorsFromType 但它没有用
- python - 在 google colab 上使用 randomsplit() 时出现 Py4JJavaError
- excel - Excel VBA中对表的动态引用
- r - 闪亮的数据表,按背景颜色过滤列(就像我们在 excel 中一样)
- java - 将带有字符串集的 Map 转换为带有字符串的 Map (Java)