Vue中$router與?$route的區(qū)別詳解
前言
- this.$route:當前激活的路由的信息對象。每個對象都是局部的,可以獲取當前路由的 path, name, params, query 等屬性。
- this.$router:全局的 router 實例。通過 vue 根實例中注入 router 實例,然后再注入到每個子組件,從而讓整個應用都有路由功能。其中包含了很多屬性和對象(比如 history 對象),任何頁面也都可以調(diào)用其 push(), replace(), go() 等方法。
路由跳轉分為編程式和聲明式
聲明式:
簡單來說,就是使用 router-link 組件來導航,通過傳入 to 屬性指定鏈接(router-link 默認會被渲染成一個a標簽)。
編程式:
采用這種方式就需要導入 VueRouter 并調(diào)用了。
src\router\index.js
import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter) // 1. 定義一些路由 // 每個路由都需要映射到一個組件。 const routes = [ { path: '/home', component: ()=> import('../views//home.vue') }, { path: '/about', component: ()=> import('../views/about.vue') }, ] const router = new VueRouter({ // mode: 'hash', //默認是hash模式,url是帶#號的 mode: 'history', //history模式url不帶#號 routes }) export default router
src\views\home.vue
<template> <div id="app"> <h1>home</h1> <button @click="handerHerf">跳轉</button> </div> </template> <script> export default { name: 'App', data(){ return { } }, mounted() { }, components:{ }, methods:{ handerHerf(){ console.log('this.$router',this.$router) this.$router.push('/about') } } } </script> <style scoped> </style>
src\views\about.vue
<template> <div> <h1>about</h1> </div> </template> <script> export default { name: 'about', data(){ return { } }, created(){ console.log('this.$route',this.$route) }, mounted() { }, computed:{ }, methods:{ } } </script> <style scoped> </style>
this.$router參數(shù)詳情
this.$route參數(shù)詳情
到此這篇關于Vue中$router與 $route的區(qū)別詳解的文章就介紹到這了,更多相關Vue $router 與 $route內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue elementui 實現(xiàn)搜索欄公共組件封裝的實例代碼
這篇文章主要介紹了vue elementui 搜索欄公共組件封裝,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01Vue3使用customRef封裝防抖函數(shù)的方法詳解
防抖函數(shù)的作用是高頻率觸發(fā)的事件,在指定的單位時間內(nèi),只響應最后一次,如果在指定的時間內(nèi)再次觸發(fā),則重新計算時間,本文將給大家詳細的介紹一下Vue3使用customRef封裝防抖函數(shù)的方法,需要的朋友可以參考下2023-09-09Vue?2源碼閱讀?Provide?Inject?依賴注入詳解
這篇文章主要為大家介紹了Vue?2源碼閱讀?Provide?Inject?依賴注入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08