Rumah  >  Soal Jawab  >  teks badan

Belajar menggunakan v-show untuk menukar templat

Saya cuba menggunakan v-showv-if untuk menogol paparan templat seperti yang ditunjukkan dalam kod di bawah.

Masalah yang saya hadapi ialah walaupun kandungan v-show不是一个懒惰的条件,但当我将showTemplate设置为false或true时,msg sentiasa dipaparkan.

Sila beritahu saya cara menggunakannya dengan betulv-showv-if.

helloWorld:

<template v-show="showTemplate">
  <div class="hello">
  {{msg}}
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<script setup>
    import { ref } from 'vue'
    let showTemplate = ref(true)
    showTemplate.value=false
</script>

Apl:

template>
  <img alt="Vue logo" src="./assets/logo.png">
  <HelloWorld msg="欢迎使用Vue.js应用程序"/>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<script setup>


</script>
P粉680087550P粉680087550402 hari yang lalu491

membalas semua(1)saya akan balas

  • P粉344355715

    P粉3443557152023-09-13 13:14:18

    Anda mesti menggunakannya seperti ini:

    <template>
      // v-show或v-if都可以
      <div v-show="showTemplate" class="hello">
      {{msg}}
      </div>
    </template>
    
    <script>
    export default {
      name: 'HelloWorld',
      props: {
        msg: String
      }
    }
    </script>
    
    <script setup>
        import { ref } from 'vue'
        let showTemplate = ref(true)
        showTemplate.value=false
    </script>

    Sebab v-if atau v-show tidak boleh digunakan dengan atribut template.

    balas
    0
  • Batalbalas