国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue-Router來實現組件間跳轉的三種方法

2024-05-06 15:24:43
字體:
來源:轉載
供稿:網友

通過VueRouter來實現組件之間的跳轉,供大家參考,具體內容如下

提供了3種方式實現跳轉:

①直接修改地址欄中的路由地址

<!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>      `    })    //配置路由詞典    //對象數組    const  myRoutes =[    //當路由地址:地址欄中的那個路徑是myLogin訪問組件    //組件是作為標簽來用的所以不能直接在component后面使用    //要用返回值      //path:''指定地址欄為空:默認為Login頁面      {path:'',component:testLogin},      {path:'/myLogin',component:testLogin},      {path:'/myRegister',component:testRegister}    ]    const myRouter = new VueRouter({      //myRoutes可以直接用上面的數組替換      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實現跳轉

<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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 江安县| 鲁甸县| 元阳县| 双鸭山市| 岑溪市| 正阳县| 成安县| 安顺市| 卫辉市| 栖霞市| 徐汇区| 历史| 襄樊市| 大港区| 靖江市| 宜州市| 宜昌市| 浦北县| 鄢陵县| 平利县| 白河县| 团风县| 兴安盟| 清水河县| 博罗县| 叙永县| 苗栗县| 房产| 铜陵市| 仪陇县| 淳化县| 深州市| 苏尼特右旗| 海口市| 利辛县| 泽库县| 南城县| 红安县| 红安县| 沈丘县| 长垣县|