Vue-router編程式導(dǎo)航的兩種實(shí)現(xiàn)代碼
頁面導(dǎo)航的兩種方式
聲明式導(dǎo)航:通過點(diǎn)擊鏈接實(shí)現(xiàn)導(dǎo)航的方式,叫做聲明式導(dǎo)航
例如:普通網(wǎng)頁中的 <a></a> 鏈接 或 vue 中的 <router-link></router-link>
編程式導(dǎo)航:通過調(diào)用JavaScript形式的API實(shí)現(xiàn)導(dǎo)航的方式,叫做編程式導(dǎo)航
例如:普通網(wǎng)頁中的 location.href
編程式導(dǎo)航基本用法
常用的編程式導(dǎo)航 API 如下:
this.$router.push(‘hash地址')
this.$router.go(n)
const User = {
template: '<div><button @click="goRegister">跳轉(zhuǎn)到注冊(cè)頁面</button></div>',
methods: {
goRegister: function(){
// 用編程的方式控制路由跳轉(zhuǎn)
this.$router.push('/register');
}
}
}
具體嗎實(shí)現(xiàn):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 導(dǎo)入 vue 文件 -->
<!-- <script src="./lib/vue_2.5.22.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- <script src="./lib/vue-router_3.0.2.js"></script> -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<!-- 被 vm 實(shí)例所控制的區(qū)域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id', 'uname', 'age'],
template: `<div>
<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>
<button @click="goRegister">跳轉(zhuǎn)到注冊(cè)頁面</button>
</div>`,
methods: {
goRegister() {
this.$router.push('/register')//編程式導(dǎo)航
}
},
}
const Register = {
template: `<div>
<h1>Register 組件</h1>
<button @click="goBack">后退</button>
</div>`,
methods: {
goBack() {
this.$router.go(-1)
}
}
}
// 創(chuàng)建路由實(shí)例對(duì)象
const router = new VueRouter({
// 所有的路由規(guī)則
routes: [
{ path: '/', redirect: '/user' },
{
// 命名路由
name: 'user',
path: '/user/:id',
component: User,
props: route => ({ uname: 'zs', age: 20, id: route.params.id })
},
{ path: '/register', component: Register }
]
})
// 創(chuàng)建 vm 實(shí)例對(duì)象
const vm = new Vue({
// 指定控制的區(qū)域
el: '#app',
data: {},
// 掛載路由實(shí)例對(duì)象
// router: router
router
})
</script>
</body>
</html>
router.push() 方法的參數(shù)規(guī)則
// 字符串(路徑名稱)
router.push('/home')
// 對(duì)象
router.push({ path: '/home' })
// 命名的路由(傳遞參數(shù))
router.push({ name: '/user', params: { userId: 123 }})
// 帶查詢參數(shù),變成 /register?uname=lisi
router.push({ path: '/register', query: { uname: 'lisi' }})
到此這篇關(guān)于Vue-router編程式導(dǎo)航的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Vue router編程式導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
3分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧
最近在學(xué)習(xí)Vue,感覺methods還是有必有總結(jié)一下的,下面這篇文章主要給大家介紹了關(guān)于3分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
基于vue-cli 路由 實(shí)現(xiàn)類似tab切換效果(vue 2.0)
這篇文章主要介紹了基于vue-cli 路由 實(shí)現(xiàn)類似tab切換效果(vue 2.0),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
Vue3使用路由及配置vite.alias簡(jiǎn)化導(dǎo)入寫法的過程詳解
這篇文章主要介紹了Vue3使用路由及配置vite.alias簡(jiǎn)化導(dǎo)入寫法,本文通過實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源(最新推薦)
import.meta.glob() 其實(shí)不僅能接收一個(gè)字符串,還可以接收一個(gè)字符串?dāng)?shù)組,就是匹配多個(gè)位置,本文給大家介紹import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源,感興趣的朋友一起看看吧2023-11-11
Vue3+Vite實(shí)現(xiàn)項(xiàng)目搭建步驟
這篇文章主要介紹了Vue3+Vite實(shí)現(xiàn)項(xiàng)目搭建步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用)
這篇文章主要介紹了多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

