vue.js - 变量变换后的条件渲染

时间:2018-05-14 07:20:03

标签: javascript vue.js vuejs2

我尝试在布尔值(waiting)变为true时渲染新元素。但是,我已经意识到,一旦我的代码发出一个v-if语句,我就无法转换到另一个v-if语句。由于布尔值在第一个v-if完成之前不会改变,一旦前者完成,转换到第二个v-if的最佳方法是什么?我无法将代码直接添加到那里,因为转换取决于输入。

我考虑过重定向到另一个页面,但我想将变量放在一个地方。

<template>
  <transition name="fade">
    <div class="home" v-if="!secondplayer">
      <div class="inner">
        <img class="responsive" src="/static/mask.png" alt="Responsive image" />
        <h1>How well do you understand the lyrics in Hamilton?</h1>
        <br />
        <p>Invite a second player by sending them this link: <u>{{url}}</u>.</p>
      </div>
    </div>
    <div class="play" v-if="secondplayer">
      <div v-if="!waiting">
        <div class="container hamilton--header--text">
          <h1><i>Test your understanding of Hamilton lyrics</i></h1>

          <div class="columns hamilton--inner">
            <div class="column is-half left">
              <p class="title">Player 1</p>
              <p class="subtitle">Score: {{playerdata.one.score}}</p>
            </div>
            <div v-if="secondplayer" class="column is-half right">
              <p class="title">Player 2</p>
              <p class="subtitle">Score: {{playerdata.two.score}}</p>
            </div>
          </div>

          <div class="hamilton--lyrics--text">
            <p>{{question.lyric}}
            </p>
            <div class="hamilton--answers">
              <a v-bind:class="{ 'wronganswer': hasAnswered && !item.correct, 'correctanswer': hasAnswered && item.correct}" @click="checkAnswer(item)" v-for="(item, index) in options">{{item.name}}</a>
            </div>
          </div>
        </div>
      </div>
      <div v-if="waiting">
        <div class="container">
          <h1>Waiting for the other player to finish...</h1>
          <img src="/static/Spinner.gif">
        </div>
      </div>
    </div> 
    <div class="gameOver" v-if="winMessage">
      <div>
        <div class="container">
          <h1>{{winMessage}}</h1>
          <vue-simple-spinner size="big" message="Loading..."></vue-simple-spinner>
        </div>
      </div>
    </div>
  </transition>
</template>

1 个答案:

答案 0 :(得分:0)

您需要应用转换css:

示例:

.fade-enter-active { /* <<transition name>>-<<transition class>> */
  transition: all .3s ease;
}
.fade-leave-active {
  transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}

查看transition classes了解更多详情。