示例代码如下:

<template>
  <div>
    <button
      class="btn"
      :class="{ green: !showForm, red: showForm }"
      @click="toggleForm"
    >
      {{ showForm ? '取消' : '添加' }}
    </button>
    <form v-show="showForm">
      <!-- 表单内容 -->
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showForm: false,
    };
  },
  methods: {
    toggleForm() {
      this.showForm = !this.showForm;
    },
  },
};
</script>

<style>
.btn {
  border: none;
  padding: 10px;
  border-radius: 5px;
  color: #fff;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.green {
  background-color: green;
}

.red {
  background-color: red;
}
</style>

原文地址: https://www.cveoy.top/t/topic/beM4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录