欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue-Router實現(xiàn)組件間跳轉的三種方法

 更新時間:2017年11月07日 10:49:41   作者:匿名的girl  
這篇文章主要為大家詳細介紹了Vue-Router來實現(xiàn)組件間跳轉的三種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

提供了3種方式實現(xià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 =[
  //當路由地址:地址欄中的那個路徑是myLogin訪問組件
  //組件是作為標簽來用的所以不能直接在component后面使用
  //要用返回值
   //path:''指定地址欄為空:默認為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實現(xià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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue.js中window.onresize的超詳細使用方法

    vue.js中window.onresize的超詳細使用方法

    這篇文章主要給大家介紹了關于vue.js中window.onresize的超詳細使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • vue使用canvas繪制圓環(huán)

    vue使用canvas繪制圓環(huán)

    這篇文章主要介紹了vue使用canvas繪制圓環(huán),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue可視化大屏實現(xiàn)無線滾動列表飛入效果

    vue可視化大屏實現(xiàn)無線滾動列表飛入效果

    本文主要介紹了vue可視化大屏實現(xiàn)無線滾動列表飛入效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • vue實現(xiàn)簡潔文件上傳進度條功能

    vue實現(xiàn)簡潔文件上傳進度條功能

    這篇文章主要介紹了vue實現(xiàn)簡潔文件上傳進度條功能,實現(xiàn)原理是通過performance.now()獲取動畫的時間戳,用于創(chuàng)建流暢的動畫,結合示例代碼介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • vue js格式化數(shù)字為金額格式代碼

    vue js格式化數(shù)字為金額格式代碼

    這篇文章主要介紹了vue js格式化數(shù)字為金額格式代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue2.0 中使用transition實現(xiàn)動畫效果使用心得

    vue2.0 中使用transition實現(xiàn)動畫效果使用心得

    這篇文章主要介紹了vue2.0 中使用transition實現(xiàn)動畫效果使用心得,本文通過案例知識給大家介紹的非常詳細,需要的朋友參考下吧
    2018-08-08
  • vue3中watch監(jiān)聽的四種寫法

    vue3中watch監(jiān)聽的四種寫法

    本文主要介紹了vue3中watch監(jiān)聽的四種寫法,包含了ref 定義的數(shù)據(jù),reactive定義的數(shù)據(jù),函數(shù)返回的值(getter函數(shù))和前面3個內容的數(shù)組,具有一定的參考價值,感興趣的可以了了解一下
    2024-02-02
  • vue使用require.context實現(xiàn)動態(tài)注冊路由

    vue使用require.context實現(xiàn)動態(tài)注冊路由

    這篇文章主要介紹了vue使用require.context實現(xiàn)動態(tài)注冊路由的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue2.0模擬錨點的實例

    vue2.0模擬錨點的實例

    下面小編就為大家分享一篇vue2.0模擬錨點的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue3的組合式API中使用ref()函數(shù)的例子

    Vue3的組合式API中使用ref()函數(shù)的例子

    你們是否聽說過Vue3的組合式API?它可是Vue3的新玩法,把以前的Vue2組件函數(shù)轉化為了函數(shù)組件。好了,今天我要和大家分享的是如何在組合式API中使用ref()函數(shù),感興趣的朋友跟隨小編一起看看吧
    2023-06-06

最新評論