3

I'm trying to do a Dark mode button toggle, the fist step that I want to make is a icon that changes when I click in the button, but my code isn't running. That's my code:

<button href="" class="px-2 mb-1" @click="isDark = !isDark">
        <img src="../Assets/Icons/moon.svg" alt="" class="w-6 h-5 hidden lg:flex md:flex" v-if="isDark = true">
        <img src="../Assets/Icons/sun.svg" alt="" class="w-6 h-5 hidden lg:flex md:flex" v-if="isDark = false">
        </button>
<script>
export default {
  setup(){
    const showSidebar = ref(false)
    const stayInDropdown = ref(true)
    const isDark = ref(true)
    return{
      showSidebar,
      stayInDropdown,
      isDark,
    }
  },
</script>
1
  • Do you get an error message when you run this? If so, post it in your question. Also, a better way to do this would be to make a computed property called something like imgSrc, which returns one url if isDark is true and another if false, and then in your template, just have one <img> tag, like this: <img :src="imgSrc" class="....."> Commented Jan 11, 2022 at 19:26

1 Answer 1

2

v-if="isDark = true" means assigning true to isDark not comparing them, the comparison should be like v-if="isDark === true" but you could just do v-if='isDark':

 <img v-if='isDark' src="../Assets/Icons/moon.svg" alt="" class="w-6 h-5 hidden lg:flex md:flex" >
 <img v-else src="../Assets/Icons/sun.svg" alt="" class="w-6 h-5 hidden lg:flex md:flex" >

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.