【发布时间】:2021-10-02 05:21:17
【问题描述】:
如何在 Vue.js 的单个按钮上应用多个事件?
我有一个按钮组件
当在父级中单击一个按钮时,会发生几个事件 必须申请
- 样式应从“购买”更改为“在篮子中”
- 图标应用完成
- 样式应更改为 button_1
我的按钮组件
<template>
<div class="btn"
@click="click"
:class="className">
<i class="material-icons"> {{ icon }} </i>
<span> {{ title }} </span>
</div>
</template>
<script>
export default {
props: {
title: {
type: String,
default: 'Buy'
},
disabled: {
type: Boolean,
default: false
},
button_1: {
type: Boolean,
default: false
},
icon: {
type: String,
default: ''
},
},
data() {
return {}
},
computed: {
className() {
return {
'btn__disabled': this.disabled,
'btn__button_1': this.button_1,
}
}
},
methods: {
click() {
this.$emit('click')
}
},
name: "BaseButton"
}
</script>
<style lang="scss" scoped>
.material-icons {
font-size: 16px;
}
.btn {
position: relative;
font-family: 'Merriweather', sans-serif;
color: var(--color-primary-light);
left: 45%;
bottom: 18%;
height: 48px;
width: 122px;
cursor: pointer;
background-color: var(--color-grey-light-4);
display: flex;
justify-content: space-evenly;
align-items: center;
&:hover,
&:active {
background-color: var(--color-grey-light-2);
border: none;
}
&__button_1 {
color: var(--color-primary-light);
background-color: var(--color-grey-light-3);
border: none;
}
&__disabled {
background-color: var(--color-grey-light-1);
border: none;
pointer-events: none;
}
}
</style>
我的父组件
<template>
<base-button @click="clickBtn"></base-button>
</template>
<script>
import BaseButton from '../components/ui/BaseButton'
export default {
name: "GalleryOverview",
components: {BaseButton},
methods: {
clickBtn() {
console.log('BTN clicked')
}
}
}
}
</script>
如何在单个按钮上应用多个事件?
【问题讨论】:
-
你试过什么,我没有看到任何代码。
-
您没有“应用事件”。你监听一个事件(“点击”是一个事件),当这个事件发生时,你执行一个函数(在你的例子中,
clickBtn())。在此功能中,您可以执行任意数量的操作。更改您的标题和其中的其他内容。 -
@Grumpy 如果你没有看到任何代码,那是你的问题,因为问题显然包括两个组件的代码
-
@JeremyThille 我的错,要快。
-
你能建议一个简单的方法与button_1请
标签: javascript vue.js sass vuejs2 vue-component