Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

vue v-show为隐藏的时候内部元素的v-if条件还是会去执行?如何优化这种情况?

<div class="container">
  <div  v-show="currentNum === 1">
      <div  v-if="elJudge(1)">1</div>
      <div  v-else="elJudge(2)">2</div>
  </div>
  <div  v-show="currentNum === 2">
      <div  v-if="elJudge(3)">3</div>
      <div  v-else="elJudge(4)">4</div>
  </div>
  <button type="button" @click="toggle">btn</button>
</div>

类似这样,当currentNum === 1的时候,currentNum === 2中的v-if条件中的函数也会被执行,我觉得这个是没有必要的,如果优化这种情况?


有同学提到两层都使用v-if,我这边有想过这个,但是根据项目实际情况我这边不能使用两层v-if,因为v-if会让组件重新实例化,这不是我想要的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

两层都用v-if就可以避免这个情况


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...