Vue-Router實(shí)現(xiàn)組件間跳轉(zhuǎn)的三種方法
通過VueRouter來實(shí)現(xiàn)組件之間的跳轉(zhuǎn),供大家參考,具體內(nèi)容如下
提供了3種方式實(shí)現(xiàn)跳轉(zhuǎn):
①直接修改地址欄中的路由地址
<!doctype html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/vue.js"></script> <!-- 引入文件 --> <script src="js/vue-router.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <!--通過router-view指定盛放組件的容器 --> <router-view></router-view> </div> <script> var testLogin = Vue.component("login",{ template:` <div> <h1>這是我的登錄頁面</h1> </div> ` }) var testRegister = Vue.component("register",{ template:` <div> <h1>這是我的注冊頁面</h1> </div> ` }) //配置路由詞典 //對象數(shù)組 const myRoutes =[ //當(dāng)路由地址:地址欄中的那個路徑是myLogin訪問組件 //組件是作為標(biāo)簽來用的所以不能直接在component后面使用 //要用返回值 //path:''指定地址欄為空:默認(rèn)為Login頁面 {path:'',component:testLogin}, {path:'/myLogin',component:testLogin}, {path:'/myRegister',component:testRegister} ] const myRouter = new VueRouter({ //myRoutes可以直接用上面的數(shù)組替換 routes:myRoutes }) new Vue({ router:myRouter, //或者: /* router:new VueRouter({ routes:[ {path:'/myLogin',component:testLogin}, {path:'/myRegister',component:testRegister} ] }) */ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
②通過router-link實(shí)現(xiàn)跳轉(zhuǎn)
<router-link to="/myRegister">注冊</router-link>
<!doctype html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/vue.js"></script> <!-- 引入文件 --> <script src="js/vue-router.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <!--通過router-view指定盛放組件的容器 --> <router-view></router-view> </div> <script> var testLogin = Vue.component("login",{ template:` <div> <h1>這是我的登錄頁面</h1> <router-link to="/myRegister">注冊</router-link> </div> ` /*to后面是路由地址*/ }) var testRegister = Vue.component("register",{ template:` <div> <h1>這是我的注冊頁面</h1> </div> ` }) //配置路由詞典 const myRoutes =[ {path:'',component:testLogin}, {path:'/myLogin',component:testLogin}, {path:'/myRegister',component:testRegister} ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
③通過js的編程的方式
jumpToLogin: function () { this.$router.push('/myLogin'); }
代碼
<!doctype html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/vue.js"></script> <!-- 引入文件 --> <script src="js/vue-router.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <!--通過router-view指定盛放組件的容器 --> <router-view></router-view> </div> <script> var testLogin = Vue.component("login",{ template:` <div> <h1>這是我的登錄頁面</h1> <router-link to="/myRegister">注冊</router-link> </div> ` /*to后面是路由地址*/ }) var testRegister = Vue.component("register",{ methods:{ jumpToLogin:function(){ this.$router.push('/myLogin'); } }, template:` <div> <h1>這是我的注冊頁面</h1> <button @click="jumpToLogin">注冊完成,去登錄</button> </div> ` }) //配置路由詞典 const myRoutes =[ {path:'',component:testLogin}, {path:'/myLogin',component:testLogin}, {path:'/myRegister',component:testRegister} ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Vue-router二級路由跳轉(zhuǎn)另一條路由下的子級
- vue-router嵌套路由方式(頁面路徑跳轉(zhuǎn)但頁面顯示空白)
- Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取
- vue-router跳轉(zhuǎn)時打開新頁面的兩種方法
- Vue-router路由判斷頁面未登錄跳轉(zhuǎn)到登錄頁面的實(shí)例
- Vue.js實(shí)戰(zhàn)之利用vue-router實(shí)現(xiàn)跳轉(zhuǎn)頁面
- vue-router跳轉(zhuǎn)頁面的方法
- vue-router跳轉(zhuǎn)方式的區(qū)別解析
相關(guān)文章
vue.js中window.onresize的超詳細(xì)使用方法
這篇文章主要給大家介紹了關(guān)于vue.js中window.onresize的超詳細(xì)使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12vue可視化大屏實(shí)現(xiàn)無線滾動列表飛入效果
本文主要介紹了vue可視化大屏實(shí)現(xiàn)無線滾動列表飛入效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04vue實(shí)現(xiàn)簡潔文件上傳進(jìn)度條功能
這篇文章主要介紹了vue實(shí)現(xiàn)簡潔文件上傳進(jìn)度條功能,實(shí)現(xiàn)原理是通過performance.now()獲取動畫的時間戳,用于創(chuàng)建流暢的動畫,結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06vue2.0 中使用transition實(shí)現(xiàn)動畫效果使用心得
這篇文章主要介紹了vue2.0 中使用transition實(shí)現(xiàn)動畫效果使用心得,本文通過案例知識給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-08-08vue使用require.context實(shí)現(xiàn)動態(tài)注冊路由
這篇文章主要介紹了vue使用require.context實(shí)現(xiàn)動態(tài)注冊路由的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12