最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn)(參數(shù)傳遞)

 更新時(shí)間:2017年11月07日 10:56:33   作者:匿名的girl  
這篇文章主要為大家詳細(xì)介紹了vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn),參數(shù)傳遞方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

通過(guò)VueRouter來(lái)實(shí)現(xiàn)組件之間的跳轉(zhuǎn):參數(shù)的傳遞,具體內(nèi)容如下

login ---用戶(hù)名--->main

①明確發(fā)送方和接收方

②配置接收方的路由地址
{path:'/myTest',component:TestComponent}
-->
{path:'/myTest/:id',component:TestComponent}

③接收方獲取傳遞來(lái)的數(shù)據(jù)
this.$route.params.id

④跳轉(zhuǎn)的時(shí)候,發(fā)送參數(shù)
this.$router.push('/myTest/20')
<router-link :to="'/myTest'+id">跳轉(zhuǎn)</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>
 </div>
 <script>
 //創(chuàng)建主頁(yè)面組件
  var myMain = Vue.component("main-component",{
   //保存登錄傳遞過(guò)來(lái)的數(shù)據(jù)
   data:function(){
  return {
   uName:''
  }
  },
   template:`
    <div>
     <h1>主頁(yè)面用戶(hù)名:{{uName}}</h1>
    </div>
   `,
   //掛載該組件時(shí)自動(dòng)拿到數(shù)據(jù)
   beforeMount:function(){
    //接收參數(shù)
    console.log(this.$route.params);
    this.uName = this.$route.params.myName ;
   }
  })
  //創(chuàng)建登錄頁(yè)面組件
  var myLogin = Vue.component("login-component",{
   //保存用戶(hù)輸入的數(shù)據(jù)
   data:function(){
    return {
     userInput:""
    }
   },
   methods:{
    toMain:function(){
     //跳轉(zhuǎn)到主頁(yè)面,并將用戶(hù)輸入的名字發(fā)送過(guò)去
     this.$router.push("/main/"+this.userInput);
     console.log(this.userInput);
    }
   },
   template:`
    <div>
     <h1>登錄頁(yè)面</h1>
     <input type="text" v-model="userInput" placeholder="請(qǐng)輸入用戶(hù)名">
     <button @click="toMain">登錄到主頁(yè)面</button>
     <br>
     <router-link :to="'/main/'+userInput">登錄到主頁(yè)面</router-link>
    </div>
   `
  })
  var NotFound = Vue.component("not-found",{
   template:`
    <div>
     <h1>404 Page Not Found</h1>
     <router-link to="/login">返回登錄頁(yè)</router-link>
    </div>
   `
  })
  //配置路由詞典
  const myRoutes = [
   {path:"",component:myLogin},
   {path:"/login",component:myLogin},
    //注意冒號(hào),不用/否則會(huì)當(dāng)成地址
   {path:"/main/:myName",component:myMain},
   //沒(méi)有匹配到任何頁(yè)面則跳轉(zhuǎn)到notfound頁(yè)面
   {path:"*",component:NotFound}
  ]
  const myRouter = new VueRouter({
   routes:myRoutes
  })
  new Vue({
   router:myRouter,
   el:"#container",
   data:{
    msg:"Hello VueJs"
   }
  })
// 注意,路由地址
 </script>
 </body>
</html>
<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>傳參練習(xí)</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>
 </div>
 <script>
//創(chuàng)建產(chǎn)品列表組件
  var myList = Vue.component("product-list",{
   //保存產(chǎn)品列表的數(shù)據(jù)
   data:function(){
    return{
     productList:["蘋(píng)果","華為","三星","小米","vivo"]
    }
   },
   template:`
    <div>
     <h4>這是列表頁(yè)</h4>
     <ul>
      <li v-for="(tmp,index) in productList">
      //將index傳遞過(guò)去
       <router-link v-bind:to="'/detail/'+index">{{tmp}}</router-link>
      </li>
     </ul>
    </div>
   `
  })
//詳情頁(yè)組件 
  var myDetail = Vue.component("product-detail",{
   //保存?zhèn)鬟f過(guò)來(lái)的index
   data:function(){
    return{
     myIndex:""
    }
   },
   //在掛載完成后,將接收到的index賦值給myIndex
   mounted:function(){
     this.myIndex = this.$route.params.id;
   },
   template:`
    <div>
     <h4>這是詳情頁(yè)</h4>
     <p>這是id為:{{myIndex}}的產(chǎn)品</p>
    </div>
   `
  })
//頁(yè)面找不到的時(shí)候
  var NotFound = Vue.component("not-found",{
   template:`
    <div>
     <h1>404 Page Not Found</h1>
    </div>
   `
  })
// 配置路由詞典
  const myRoutes = [
   {path:"",component:myList},
   {path:"/list",component:myList},
   {path:"/detail/:id",component:myDetail},
   {path:"*",component:NotFound},
  ]
  const myRouter = new VueRouter({
   routes:myRoutes
  })
  new Vue({
   router:myRouter,
   el:"#container",
   data:{
    msg:"Hello VueJs"
   }
  })
 </script>
 </body>
</html>

關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

普安县| 承德县| 乌海市| 南昌县| 和林格尔县| 怀安县| 乌鲁木齐市| 博白县| 天等县| 新泰市| 宁安市| 台前县| 榆社县| 乌鲁木齐市| 四平市| 家居| 正宁县| 海晏县| 宕昌县| 英山县| 奉化市| 吉林省| 思茅市| 鹤壁市| 鲜城| 云浮市| 黑山县| 康马县| 常宁市| 北票市| 华蓥市| 肥乡县| 新建县| 昂仁县| 临猗县| 漯河市| 蒙自县| 磐石市| 郸城县| 龙里县| 岫岩|