iT邦幫忙

0

如何才能達成完成某些條件,頁面會跳轉到其他頁面

接到一個指示說 要做隱藏按鈕依照特定的順序 按下 才會進入某個頁面 要在vue 底下做這件事情
我的想法是

坐四個按鈕 設為false

     left: false,
      right: false,
      bottomleft: false,
      bottomRight: false

點擊按鈕為true

<v-btn @click="left = true">left</v-btn>
<v-btn @click="right = true">right</v-btn>
<v-btn @click="bottomleft = true">bottomleft</v-btn>
<v-btn @click="bottomRight = true">bottomRight</v-btn>

然後在mounted 下設定只要四個buttn 都為真 就可以自動跳轉某頁面

  mounted () {
    if (this.left === true && this.right === true && this.bottomleft === true && this.bottomRight === true) {
      alert('123')
      this.$router.push({ name: 'Home' })
    } else {
      this.$router.push({ name: 'Costomer' })
    }
  }

但是沒有效果椰,是說寫錯了嗎? 還是不應該這樣設?希望能提供一個方向 解答

後來我改由watch 監聽
這樣寫對嗎?

<v-btn @click="left = !left" class="left">left</v-btn>
<v-btn @click="right = !right" class="right">right</v-btn>
<v-btn @click="bottomleft = !bottomleft" class="bottomleft">bottomleft</v-btn>
<v-btn @click="bottomRight = !bottomRight" class="bottomRight">bottomRight</v-btn>
methods: {
  mountedApi () {
    if (this.left === true && this.right === true && this.bottomleft === true && this.bottomRight === true) {
      this.$router.push({ name: 'Home' })
    } else {
      alert('123')
    }
    
  }
},

watch: {

  $route: 'mountedApi'
 
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
dragonH
iT邦超人 5 級 ‧ 2020-11-05 11:55:51

codepen

你沒有用 watch 呀

然後你看一下 mounted 在 vue lifecycle 是什麼角色

image

yolala iT邦新手 5 級 ‧ 2020-11-05 18:31:43 檢舉

謝謝你 我再努力把生命週期 搞懂 謝謝你

vue的watch有針對多個變數進行監聽的機制,你可以查看看!

try看看

created() {
this.$watch(function () {
return this.left + this.right + this.bottomleft + this.bottomRight
}, function (val) {
console.log(val)
})
},

0

你寫在 mounted 內是錯的。
因為那是在載入頁面完成後才運行的。

所以你的寫法是錯的。
你因該要在 methods 寫一個函數來處理。
將四個按鍵的觸發動作都跑這個函式。

yolala iT邦新手 5 級 ‧ 2020-11-05 18:32:03 檢舉

謝謝你 @@

我要發表回答

立即登入回答