Vue-router編程式導(dǎo)航的兩種實(shí)現(xiàn)代碼
聲明式導(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)到注冊頁面</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='http://www.intensediesel.com/bcjs/lib/vue_2.5.22.js'></script> --> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> <!-- <script src='http://www.intensediesel.com/bcjs/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)到注冊頁面</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í)例對象 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í)例對象 const vm = new Vue({ // 指定控制的區(qū)域 el: ’#app’, data: {}, // 掛載路由實(shí)例對象 // router: router router }) </script> </body></html>router.push() 方法的參數(shù)規(guī)則
// 字符串(路徑名稱) router.push(’/home’) // 對象 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP實(shí)現(xiàn)加法驗(yàn)證碼2. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?3. 小技巧處理div內(nèi)容溢出4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 存儲于xml中需要的HTML轉(zhuǎn)義代碼6. asp知識整理筆記4(問答模式)7. phpstudy apache開啟ssi使用詳解8. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁9. js的一些潛在規(guī)則使用分析10. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長日期的方法
