vue 監聽路由變化

一.watch監聽$route($router的對象)

// 監聽,當路由發生變化的時候執行
watch:{$route(to,from){console.log(to.path);}
},
===========================================================
// 監聽,當路由發生變化的時候執行
watch: {$route: {handler: function(val, oldVal){console.log(val);},// 深度觀察監聽deep: true}
},
===========================================================
// 監聽,當路由發生變化的時候執行
watch: {'$route':'getPath'
},
methods: {getPath(){console.log(this.$route.path);}
}

二. vue-router 的鉤子函數(寫在與methods同級的地方或者router中)

beforeRouteEnter (to, from, next) {// 在渲染該組件的對應路由被 confirm 前調用// 不!能!獲取組件實例 `this`// 因為當鉤子執行前,組件實例還沒被創建
},
===========================================================
beforeRouteUpdate (to, from, next) {// 在當前路由改變,但是該組件被復用時調用// 舉例來說,對于一個帶有動態參數的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候,// 由于會渲染同樣的 Foo 組件,因此組件實例會被復用。而這個鉤子就會在這個情況下被調用。// 可以訪問組件實例 `this`
},
===========================================================
beforeRouteLeave (to, from, next) {// 導航離開該組件的對應路由時調用// 可以訪問組件實例 `this`
},

完整的導航解析流程

  1. 導航被觸發。
  2. 在失活的組件里調用 beforeRouteLeave 守衛。
  3. 調用全局的 beforeEach 守衛。
  4. 在重用的組件里調用 beforeRouteUpdate 守衛 (2.2+)。
  5. 在路由配置里調用 beforeEnter。
  6. 解析異步路由組件。
  7. 在被激活的組件里調用 beforeRouteEnter。
  8. 調用全局的 beforeResolve 守衛 (2.5+)。
  9. 導航被確認。
  10. 調用全局的 afterEach 鉤子。
  11. 觸發 DOM 更新。
  12. 調用 beforeRouteEnter 守衛中傳給 next 的回調函數,創建好的組件實例會作為回調函數的參數傳入。

三. 路由守衛

import Vue from "vue";
import VueRouter from "vue-router";
import store from '@/store'
Vue.use(VueRouter);const routes = [***]const router = new VueRouter({routes
});// 設置路由守衛,在進頁面之前,判斷有token,才進入頁面,否則返回登錄頁面
router.beforeEach((to, from, next) => {// 判斷要去的路由有沒有 noRequireToken// to.matched.some(r => r.meta.noRequireToken) or to.meta.noRequireTokenif (to.matched.some(r => !r.meta.noRequireToken)) {let token = store.getters.getTokenconsole.log("token:", token,to.fullPath)if (token) {next(); //有token,進行request請求,后臺還會驗證token} else {next({name: "Login", // 使用params參數不會消失,meta刷新后消失;params必須和query一起用?// path:"/login",// 將剛剛要去的路由path(卻無權限)作為參數,方便登錄成功后直接跳轉到該路由,這要進一步在登陸頁面判斷query: { redirect: to.fullPath },// params: { redirect: to.fullPath },// meta: { redirect: to.fullPath },});}} else {next(); //如果無需token,那么隨它去吧}
});export default router;

組件獨享守衛

const router = new VueRouter({routes: [{path: '/foo',component: Foo,beforeEnter: (to, from, next) => {// ...}}]
})

官網介紹 vue-router

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278658.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278658.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278658.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

音頻剪切_音頻編輯入門指南:剪切,修剪和排列

音頻剪切Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Knowing how to cut and trim tracks is basic audio editing and is a fundamental starting point for making more elaborate arrangements. 大膽的新手通常從崇高的項…

Mybatis自定義SQL攔截器

本博客介紹的是繼承Mybatis提供的Interface接口,自定義攔截器,然后將項目中的sql攔截一下,打印到控制臺。 先自定義一個攔截器 package com.muses.taoshop.common.core.database.config;import org.apache.commons.lang3.StringUtils; import…

搭建spring boot環境并測試一個controller

Idea搭建spring boot環境一、新建項目二、起步依賴三、編寫SpringBoot引導類四、編寫Controller五、熱部署一、新建項目 1.新建project 2.選擇SpringInitializr,選擇jdk,沒有則需要下載并配置(若選擇Maven工程則需要自己添加pom.xml所需依賴坐標和Java…

音頻噪聲抑制_音頻編輯入門指南:基本噪聲消除

音頻噪聲抑制Laying down some vocals? Starting your own podcast? Here’s how to remove noise from a messy audio track in Audacity quickly and easily. 放下人聲? 開始自己的播客? 這是在Audacity中快速輕松地消除雜亂音軌中噪聲的方法。 Th…

Dubbo集群容錯

轉自dubbo官網文檔http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在分布式系統中,集群某個某些節點出現問題是大概率事件,因此在設計分布式RPC框架的過程中,必須要把失敗作為設計的一等公民來對…

Linux基礎(day53)

2019獨角獸企業重金招聘Python工程師標準>>> 12.21 php-fpm的pool php-fpm的pool目錄概要 vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加include etc/php-fpm.d/*.confmkdir /usr/local/php/etc/php-fpm.d/cd /usr/local/php/etc/php-fpm.d/vim www.co…

Mysql+Navicat for Mysql

一、mysql 1.下載安裝 Mysql官網下載地址 下載后解壓 .zip (或安裝.msi) 2.可加入全局變量mysqld (可選) 我的電腦->屬性->高級->環境變量->Path(系統變量),添加mysql下的bin目錄,如 D:\Pr…

公鑰,私鑰和數字簽名

一、公鑰加密 假設一下,我找了兩個數字,一個是1,一個是2。我喜歡2這個數字,就保留起來,不告訴你們(私鑰),然后我告訴大家,1是我的公鑰。 我有一個文件,不能讓別人看&…

MySQL中的日志類型(二)-General query log

簡介 General query log記錄客戶端的連接和斷開,以及從客戶端發來的每一個SQL語句。 日志內容格式 General query log可以記錄在文件中,也可以記錄在表中,格式如下:在文件中會記錄時間、線程ID、命令類型以及執行的語句示例如下&a…

android wi-fi_如何在Android手機上查找3G或Wi-Fi速度

android wi-fiAre you curious about what kind of connection speed you are getting with your Android phone? Today we’ll take a look at how to easily check your Wi-Fi or 3G speeds with Speedtest.net’s Speed Test app. 您是否對Android手機的連接速度感到好奇&a…

vue引入全局less實現全局變量的控制

vue引入全局less1.設置全局樣式變量的好處:2.以less為例(sass等同原理)1.vue-cli2搭建的項目(1)2.vue-cli2搭建的項目(2)3.vue-cli3、vue-cli43.vue-cli2和vue-cli3的區別4.vue-cli3和vue-cli4的…

如何在eclipse中對項目進行重新編譯

有時由于eclipse異常關閉,當我們重啟Eclipse,在啟動項目時,會報錯,說:ClassNotFound類似的錯誤,引起這種問題的原因可能是由于,Eclipse異常關閉引起的。 解決:在一個項目中&#xff…

SQL 查詢數據庫中包含指定字符串的相關表和相關記錄

declare str varchar(100)set str我要找的 --要搜索的字符串declare s varchar(8000)declare tb cursor local forselect if exists(select 1 from [b.name] where [a.name] like %str%)print [b.name].[a.name]from syscolumns a join sysobjects b on a.idb.idwhere b.xtype…

如何在Gmail的圖片中插入超鏈接

Adding hyperlinks is an efficient way of getting your reader to the intended web page. Though it’s no secret that you can add hyperlinks to text, Gmail also lets you add hyperlinks to images in the body of the email. Here’s how to make it happen. 添加超鏈…

內聯元素居中

父元素&#xff1a; height:100px; line-height:100px; // 與高相同 text-align:center; 子元素: display:inline; vertical-align: middle; 適用圖片、文字 <div><div class"wrapper"><span>我是文字</span></div><div class&qu…

防止html標簽轉義

function htmlDecode ( str ) {var ele document.createElement(span);ele.innerHTML str;return ele.textContent;} 例如body下邊所有的p標簽都防止轉義&#xff1a; $.each($("body").find(p),function(){this.innerHTML htmlDecode(this.innerHTML);}); 轉載于…

新垣結衣自拍照_如何阻止自拍照出現在iPhone的自拍照專輯中

新垣結衣自拍照Khamosh PathakKhamosh PathakThe Photos app on your iPhone automatically populates all photos from the front-facing camera in the Selfies album. But what if you don’t want a photo to appear there? Here are a couple of solutions. iPhone上的“…

前端個人筆記

前端個人筆記1.vue項目安裝依賴/插件時忘記--save&#xff0c;再次install出問題并且沒有報錯。2.margin移動元素不顯示背景色3.新知識&#xff1a;media 條件樣式4.入坑&#xff1a;row和col不能分離&#xff0c;span24不能不寫5.聚焦實現滾動到指定元素1.vue項目安裝依賴/插件…

kernel中對文件的讀寫【學習筆記】【原創】

/*1. 頭文件 */ #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/fs.h> #include <linux/uaccess.h>MODULE_PARM_DESC(iva…

ssm項目快速搭建(注解)-依賴

父層jar包版本控制&#xff0c;管理配置 <!-- 集中定義依賴版本號 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> <pagehelper.version>4.0.0<…