vue3中通过对象来改变样式颜色

直接上案例代码,学过vue3的一看就懂:

<template>
  <div :class="colorObj">颜色变化</div>
</template>

<script lang="ts">
// eslint-disable
import { defineComponent, reactive, toRefs, onMounted } from "vue";

export default defineComponent({
  name: "App",
  setup() {
    const state = reactive({
      colorObj: {
        red: false,
        green: true,
        blue: false
      },
      status: 2
    });
    onMounted(() => {
      state.status = 1
      state.colorObj = {
        red: state.status == 0,
        green: state.status == 1,
        blue: state.status == 2
      };// eslint-disable
    });
    return {
      ...toRefs(state)
    };
  }
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.red {
  color: red;
}
.green {
  color: green;
}
.blue {
  color: blue;
}
</style>

你可能感兴趣的:(vue,前端开发,vue,css)