1. 程式人生 > 其它 >Vue實現單頁面路由攔截

Vue實現單頁面路由攔截

技術標籤:vue

單頁面路由攔截

//需要你寫在需要攔截的頁面 例如你在首頁進入到我的頁面時,寫在我的頁面程式碼塊裡面
<template>
  <div>
    歡迎:xxx(通this.$stort.state.vuex儲存使用者的變數)
  </div>
</template>

<script>
export default {
  name: "",
  beforeRouteEnter(to, from, next) {
    next((vm) => {
  //判斷vux裡面有沒有使用者名稱存的值 前提是您要存進去再判斷
if (vm.$store.state.username == "") { alert("請先登入"); vm.$router.push("/");//攔截成功後需要跳轉的頁面 } }); }, data() { return {}; }, mounted() {}, methods: {}, };
</script> <style scoped></style>