国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Vue-router編程式導(dǎo)航的兩種實(shí)現(xiàn)代碼

瀏覽:66日期:2022-10-03 16:05:01
頁面導(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)到注冊頁面</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)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 达尔| 锡林浩特市| 邵阳县| 宁波市| 龙井市| 张家港市| 常州市| 江达县| 乌苏市| 娄底市| 西和县| 福清市| 东方市| 靖江市| 柘荣县| 砀山县| 会昌县| 基隆市| 漠河县| 崇明县| 山丹县| 盐源县| 登封市| 江源县| 贡觉县| 辽中县| 望城县| 大同县| 收藏| 庆安县| 浠水县| 阿图什市| 海南省| 洛川县| 吉木萨尔县| 土默特左旗| 樟树市| 米脂县| 镇赉县| 成安县| 宝兴县|