vue --- [全家桶]vue-router

1. Vue - router

  • Vue Router是 Vue.js 官方的路由管理器
  • 它和Vue.js的核心深度集成,可以非常方便的用于SPA應用程序的開發

Vue Router包含的功能有:

  • 支持HTML5歷史模式或hash模式
  • 支持嵌套路由
  • 支持路由參數
  • 支持編程式路由
  • 支持命名路由
<div id="app"><router-link to='/user'>User</router-link><router-link to='/register'>Register</router-link><!-- 將來通過路由規則匹配到的組件,將會被渲染到router-view所在的位置--><router-view></router-view>
</div>
<script>const User={template: `<h1>User</h1>`}const Register={template: `<h1>Register</h1>`}const router = new VueRouter({routes:[{path: '/user',component: User},{path: '/register',component: Register}]})const vm = new Vue({el:'#app',data: {},router})
</script>

1.1 路由重定向

var router = new VueRouter({routes:[{ path: '/', redirect: '/user'},{ path: '/user', component: User},{ path: '/register', component: Register}]
})

1.2 嵌套路由

const router = new VueRouter({routes:[{ path: '/user', component: User },{ path: '/register',component: Register,children: [{ path: '/register/tab1', component: Tab1 },{ path: '/register/tab2', component: Tab2}]}]
})

1.3 動態路由匹配

  • 以下路由規則該如何匹配
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
  • 進行動態路由匹配
var router = new VueRouter({routes: [{path: '/user/:id', component: User}]
})
// 匹配得到的參數存在 $route.params 中
const User = {template: '<div>User {{ $route.params.id }}</div>'
}

1.4 路由組件傳遞參數

  1. props的值為布爾類型
const router = new VueRouter({routes:[{ path: '/user/:id', component: User, props: true}]
})
const User = {props: ['id'],template: `<div>用戶ID: {{ id }}</div>`
}
  1. props的值為對象類型
const router = new VueRouter({routes: [{ path: '/user/:id', component: User, props: { uname: 'lisi', age: 12}}]
})
const User = {props: ['uname', 'age'],template: '<div>用戶信息: {{ uname + '---' + age}}</div>'
}
  1. props的值為函數類型
const router = new VueRouter({routes:[{ path: '/user/:id', component: User, props: route => ({ uname: 'zs', age:20, id: route.params.id  })}]
})
const User = {props: ['uname', 'age', 'id'],template: '<div>用戶信息: {{ uname + '---' + age + '---' +id}}</div>'
}

1.5 命名路由

  • 未來更加方便的表示路由的路徑,可以給路由規則起一個別名,即為"命名路由"
<router-link :to="{ name: 'user', params: {id: 123}}">User</router-link>
<script>const router = new VueRouter({routes:[{ path: '/user/:id', name: 'user', component: User}]})const User = {template: `<div><h1> User -- {{$route.parmas.id}} </h1></div>`}
</script>

1.6 編程式導航

  • 聲明式導航: 通過點擊鏈接實現導航的方式,叫做聲明式導航
    • 普通網頁: 點擊<a></a>
    • vue: <router-link></router-link>
  • 編程式導航: 通過JavaScript形式中的API實現導航的方式
    • 普通網頁中: location.href
    • Vue:
      • this.$router.push(‘hash地址’)
      • this.$router.go(n) - 前進后退
// this.$router.push
const User = {template: `<div><h1>User {{$route.params.id}} 組件</h1><button @click="goRegister">跳轉到注冊頁面</button></div>`,methods:{goRegister(){this.$router.push('/register')}}
}// this.$router.go
const Register = {template: `<div><h1>Register</h1><button @click="goBack">后退</button></div>`,methods: {goBack() {this.$router.go(-1)}}
}
  • router.push()方法的參數規則
// 字符串(路徑名稱)
router.push('/home')// 對象
router.push({ path: '/home'})// 命名的路由(傳遞參數)
router.push({ name: '/user', params: { userId: 123}})// 帶查詢參數,變成 /register?uname=lisi
router.push({ path: '/register', query: { uname: 'lisi'}})

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

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

相關文章

HashMap的四種訪問方式

第一種&#xff1a;通過Map.entrySet使用iterator遍歷key和value 1 public void visit_1(HashMap<String,Integer> hm){ 2 Iterator<Map.Entry<String,Integer>> it hm.entrySet().iterator(); 3 while(it.hasNext()){ 4 Map.Entry<String ,Integer> …

16.unix網絡編程一卷 unp.h

unix網絡編程 --ubuntu下建立編譯環境 1.安裝編譯器&#xff0c;安裝build-essential sudo apt-get install build-essential 2.下載本書的頭文件 下載unp13e&#xff1a; http://pix.cs.olemiss.edu/csci561/prg561.1.html 3.進入unp13e 查看readme&#xff0c;照下列提示操作…

webpack --- [讀書筆記] webpack中常用的一些配置項

1. Webpack 當前Web開發面臨的困境 文件依賴關系錯綜復雜靜態資源請求效率低模塊化支持不友好瀏覽器對高級JavaScript特性兼容程度低 1.1 webpack概述 webpack是一個流行的前端項目構建工具,可以解決當前web開發中所面臨的困境. webpack提供了友好的模塊化支持,以及代碼壓…

spring中bean的作用域屬性single與prototype的區別

https://blog.csdn.net/linwei_1029/article/details/18408363 轉載于:https://www.cnblogs.com/stanljj/p/9907444.html

windows程序設計.第一個windos程序

Windows程序設計&#xff08;第5版&#xff09; windows程序需要調用API。 第一個Windows程序 1 /*HelloMsg.c -- Displays "Hello World!" in a message box*/ 2 #include <Windows.h> 3 4 int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE…

java接口練習2

1、編寫2個接口&#xff1a;InterfaceA和InterfaceB&#xff1b;在接口InterfaceA中有個方法voidprintCapitalLetter()&#xff1b;在接口InterfaceB中有個方法void printLowercaseLetter()&#xff1b;然后寫一個類Print實現接口InterfaceA和InterfaceB&#xff0c;要求printC…

vue --- [全家桶] Vuex

1. Vuex 概述 1.1 組件之間共享數據的方式 父向子傳值: v-bind 屬性綁定子向父傳值: v-on 事件綁定兄弟組件之間共享數據: EventBus$on: 接收數據的那個組件$emit: 發送數據的那個組件 1.2 Vuex是什么 Vuex: 是實現組件全局狀態(數據)管理的一種機制,可以方便的實現組件之間…

C#/WPF程序開機自動啟動

最近一個C/S項目客戶要求開機自啟的功能&#xff0c;網上找了一些方法&#xff0c;不頂用&#xff1b;最后自己去翻書&#xff0c;找到了這段代碼&#xff0c;親測可用&#xff0c;Wpf環境下需要改下獲取程序目錄的方式即可&#xff0c;Winform直接可用。 1 #regio…

github --- 多個項目的管理方式

1. 多個項目管理方式 進入項目根目錄: git init 將當前的項目添加到暫存區中: git add . (注意: 最后有一個點) 將暫存區的內容放到本地倉庫: git commit -m 初始化項目 登錄github , 新建遠程倉庫 在本地添加遠程倉庫的源: git remote origin https://github.com/Lizhhhh/…

記錄一個坑

導入項目后運行控制臺打印異常,pom都已檢查,沒有任何問題 解決辦法: 項目右擊---properties---deployment assembly---add---java build path entries---maven deoendencies 保存并關閉 解決... 這個問題第一次遇到 檢查了很多遍maven的依賴,明明都已經配置好了 ,仍然產生了…

洛谷 P4011 孤島營救問題【最短路+分層圖】

題外話&#xff1a;昨夜腦子昏沉&#xff0c;今早一調試就過了...錯誤有&#xff1a;我忘記還有墻直接穿墻過...memset初始化INF用錯了數...然后手殘敲錯一個狀態一直過不了樣例...要是這狀態去比賽我簡直完了......orz 題目鏈接&#xff1a;https://www.luogu.org/problemnew/…

輸出控制臺信息到日志 并 通過cronolog對tomcat進行日志切分

windows下tomcat默認并不會把控制臺輸出的信息都記錄進日志文件。但是在生產環境中&#xff0c;出現問題時&#xff0c;控制臺的日志輸出是無法查據的&#xff0c;因此需要將日志記錄下來。 解決方法&#xff1a; 輸出日志到文件 修改tomcat的bin目錄下的startup.bat文件&#…

微信小程序 --- [筆記小結] 環境搭建,基礎學習

說明 源代碼拷貝源代碼 git clone https://github.com/Lizhhhh/miniProgram.git進入目錄cd miniProgram查看tag: git tag選擇需要查看的知識點,如: git checkout 02_Text 學習的視頻失效了…后續還會找資源學習… 小程序 地址 一種不需要下載安裝即可使用的應用,它實現了應…

監聽發現局域網dropbox客戶端broadcast-dropbox-listener

監聽發現局域網dropbox客戶端broadcast-dropbox-listenerDropbox是一款網盤文件同步工具。為了實現局域網內同步&#xff0c;該工具會通過UDP 17500端口發送廣播包。Nmap的broadcast-dropbox-listener腳本可以監聽局域網內dropbox客戶端發送的廣播包&#xff0c;并顯示客戶端的…

tornada-數據庫

數據庫 torndb安裝連接初始化執行語句 executeexecute_rowcount查詢語句 getquery與Django框架相比&#xff0c;Tornado沒有自帶ORM&#xff0c;對于數據庫需要自己去適配。我們使用MySQL數據庫。 在Tornado3.0版本以前提供tornado.database模塊用來操作MySQL數據庫&#xff0c…

使用Puppeteer進行數據抓取(一)——安裝和使用

Puppeteer 是 Google Chrome 團隊官方的Chrome 自動化工具。它本身是基于Chrome Dev Protocol協議實現的&#xff0c;但它提供了更高層次API封裝&#xff0c;使用起來更加方便快捷。加上google這個大咖加官方的背景&#xff0c;更使得其地位更是提升了不少。 我之前在文章使用C…

讀書筆記 --- [基礎知識點] 小結1

1. TCP,UDP區別 TCPUDP基于有連接基于無連接對系統資源要求較多對系統資源要求少程序比較復雜程序結構比較簡單流模式數據報模式保證數據的準確性不保證數據的準確性保證數據的順序不保證數據的順序 2. OSI七層模型以及tcp/ip四層模型 OSI七層模型tcp/ip四層模型常用的5層模型…

連讀

一、輔音元音的連讀 單詞的音標以輔音結尾&#xff0c;下一個單詞以元音開頭。 1、/n/ /?/ 連讀后就餓會發出“呢” 這個音&#xff1b; 2、/v/ /?/ son of a bitch 3、/t/ // 4、/t/ /?:/ 差不多是 tall 這個音 not at all 5、/l/ /?/ call it a day // 今天就到…

讀書筆記 --- [基礎知識點] 小結2

1. TCP和UDP的區別 \TCPUDP是否連接面向連接無連接是否可靠可靠不可靠連接對象個數1對11對1 或1 對多傳輸方式面向字節面向報文首部開銷20字節8字節使用場景可靠傳輸,如: 文件傳輸實時應用(IP電話、視頻會議、直播等) 2. WebSocket (1)什么是WebSocket? WebSocket是HTML5中的…

Spring差缺補漏

Spring差缺補漏 Spring4.0新特性 1&#xff1a;全面支持java1.8 2&#xff1a;空指針 RequestMapping("/user") public User getUser(String id,Option<String> userName){} 3&#xff1a;泛型依賴注入 public abstract class BaseService<M extends Serial…