Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24 - 激活的样式-指令 #2912

Open
cone41 opened this issue Jan 20, 2025 · 0 comments
Open

24 - 激活的样式-指令 #2912

cone41 opened this issue Jan 20, 2025 · 0 comments

Comments

@cone41
Copy link

cone41 commented Jan 20, 2025

<script setup lang="ts">
import { ref, watchEffect } from 'vue';

/**
 * Implement the custom directive
 * Make sure the list item text color changes to red when the `toggleTab` is toggled
 *
 */
const VActiveStyle = {
  mounted(el, binding) {
    const [style, fn] = binding.value;
    watchEffect(() => {
      const flag = fn();
      Object.keys(style).forEach((key) => {
        el.style[key] = flag ? style[key] : '';
      });
    });
  },
};

const list = [1, 2, 3, 4, 5, 6, 7, 8];
const activeTab = ref(0);
function toggleTab(index: number) {
  activeTab.value = index;
}
</script>

<template>
  <ul>
    <li
      v-for="(item, index) in list"
      :key="index"
      v-active-style="[{ color: 'red' }, () => activeTab === index]"
      @click="toggleTab(index)"
    >
      {{ item }}
    </li>
  </ul>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant