vue學習記錄④(路由傳參)


通過上篇文章對路由的工作原理有了基本的了解,現在我們一起來學習路由是如何傳遞參數的,也就是帶參數的跳轉。

帶參數的跳轉,一般是兩種方式:

①.a標簽直接跳轉。

②點擊按鈕,觸發函數跳轉。

上篇文章中我們已經有兩個頁面(Helloworld.vue&Hello.vue),現在我准備往Hello.vue里面添加3個鏈接,分別對應兩種情況的跳轉。

 第一步:在原來的Hello.vue里添加路由鏈接跳轉的代碼(見第38-44行代碼),添加后的Hello.vue代碼如下:

 1 <template>
 2   <div class="hello">
 3     <h1>{{ msg }}</h1>
 4     <h2>Essential Links</h2>
 5     <ul>
 6       <li>
 7         <a href="https://vuejs.org" target="_blank">Core Docs</a>
 8       </li>
 9       <li>
10         <a href="https://forum.vuejs.org" target="_blank">Forum</a>
11       </li>
12       <li>
13         <a href="https://chat.vuejs.org" target="_blank">Community Chat</a>
14       </li>
15       <li>
16         <a href="https://twitter.com/vuejs" target="_blank">Twitter</a>
17       </li>
18       <br>
19       <li>
20         <a href="http://vuejs-templates.github.io/webpack/" target="_blank">Docs for This Template</a>
21       </li>
22     </ul>
23     <h2>Ecosystem</h2>
24     <ul>
25       <li>
26         <a href="http://router.vuejs.org/" target="_blank">vue-router</a>
27       </li>
28       <li>
29         <a href="http://vuex.vuejs.org/" target="_blank">vuex</a>
30       </li>
31       <li>
32         <a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a>
33       </li>
34       <li>
35         <a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a>
36       </li>
37     </ul>
38     <div>
39       <router-link :to="{path:'/helloworld/123'}">參數傳遞1</router-link>
40       <br>
41       <router-link :to="{path:'/helloworld/789',query:{userid:9527,name:'Tom_Lo'}}">參數傳遞2</router-link>
42       <br>
43       <button @click="toNewpage">點擊跳轉</button>
44     </div>
45   </div>
46 </template>
47 
48 <script>
49 export default {
50   name: 'hello',
51   data() {
52     return {
53       msg: 'Welcome to Your Vue.js App'
54     }
55   },
56    methods: {
57     toNewpage: function() {
58       
59       this.$router.push({ path: '/helloworld/999', query: { userid: 128, name: 'Tom' } });
60     }
61   }
62 }
63 </script>
64 
65 <!-- Add "scoped" attribute to limit CSS to this component only -->
66 <style scoped>
67 h1,
68 h2 {
69   font-weight: normal;
70 }
71 
72 ul {
73   list-style-type: none;
74   padding: 0;
75 }
76 
77 li {
78   display: inline-block;
79   margin: 0 10px;
80 }
81 
82 a {
83   color: #42b983;
84 }
85 </style>

第38-44行代碼的路由鏈接跳轉寫法是固定的,記住會用就好了。<router-link>默認會被渲染成一個 `<a>` 標簽 ,to指令跳轉到指定路徑 。

第二步:Hello.vue傳遞了參數,那么我們就用Helloworld.vue接收參數。見更新后的Helloworld.vue代碼:

 1 <!--模板部分-->
 2 <template>
 3   <div class="container">
 4     <h1>hello,world!</h1>
 5     <p>{{test}}</p>
 6     <p>接收的參數id:
 7       <span class="hint">{{id}}</span>
 8     </p>
 9     <p>接收的參數userid:
10       <span class="hint">{{userid}}</span>
11     </p>
12     <p>接收的參數name:
13       <span class="hint">{{username}}</span>
14     </p>
15   </div>
16 </template>
17 <!--js部分-->
18 <script>
19 export default {
20   name: 'helloworld',
21   data() {
22     return {
23       test: 'this is a test',
24       id: this.$route.params.id,//接收參數
25       userid: this.$route.query.userid,
26       username: this.$route.query.name
27     }
28   }
29 }
30 </script>
31 <!--樣式部分-->
32 <style>
33 .container {
34   background: #aaa;
35   color: blue;
36 }
37 </style>

上面的第6-14行就是接收參數的容器。

注意:Hello.vue中的$router是用來傳遞參數的,而Helloworld.vue中的$route是用來接收參數的。

第三步:路由變化了,當然還得在index.js里面體現(見第16行),見更新后的index.js代碼:

 1 import Vue from 'vue'
 2 import Router from 'vue-router'
 3 import Hello from '@/components/Hello'
 4 import HelloWorld from '@/components/Helloworld'//我們新定義的組件
 5 
 6 Vue.use(Router)
 7 
 8 export default new Router({
 9   routes: [
10     {
11       path: '/',
12       name: 'Hello',
13       component: Hello
14     },
15     {//新路由
16       path: '/helloworld/:id',
17       name: 'HelloWorld',
18       component: HelloWorld
19     }
20   ]
21 })

 第四步:入口文件App.vue不用動。路由定義、發送參數、接收參數都具備了,下面咱們就跑一下試試吧~~

運行跳轉成功后如下圖:

 

 

通過圖示我們看到,參數都可以正常拿到了。

同時注意url的變化。首先url是有個#號,這個就代表是單頁面的路由(hash模式);然后我們的參數實際都是放在url上傳輸的,要注意別超出url的長度范圍。

下面我們再來看下子路由。

子路由也就是在父頁面下,單獨划出一個div容器,里面再塞進一個路由。

我們把Helloworld設定為父路由,再設定兩個兩個子路由。分別是info和content。

 更新后的Helloworld.vue代碼如下:

 1 <!--模板部分-->
 2 <template>
 3   <div class="container">
 4     <h1>hello,world!</h1>
 5     <p>{{test}}</p>
 6     <p>接收的參數id:
 7       <span class="hint">{{id}}</span>
 8     </p>
 9     <p>接收的參數userid:
10       <span class="hint">{{userid}}</span>
11     </p>
12     <p>接收的參數name:
13       <span class="hint">{{username}}</span>
14     </p>
15     <div class="subrouter">
16       <h2 class="hint">子路由:</h2>
17       <router-view></router-view>
18     </div>
19   </div>
20 </template>
21 <!--js部分-->
22 <script>
23 export default {
24   name: 'helloworld',
25   data() {
26     console.log(this.$route);
27     return {
28       test: '這是一個測試',
29       id: this.$route.params.id,//接收參數
30       userid: this.$route.query.userid,
31       username: this.$route.query.name
32     };
33   }
34 }
35 </script>
36 <!--樣式部分-->
37 <style>
38 .container {
39   background: #ccc;
40   color: greenyellow;
41   height: 500px;
42 }
43 
44 .hint {
45   color: darkred;
46   font-weight: bold;
47 }
48 
49 .subrouter {
50   background: #aaa;
51   width: 500px;
52   height: 100px;
53   margin: 0 auto;
54 }
55 </style>

第15-18行創建了子路由的容器。然后在components下,創建新目錄subpage,並在subpage下新建兩個子組件info.vue和content.vue。

info.vue代碼如下:

<template>
 <div class="info">info page--id:{{id}}</div>
</template>
<script>
export default {
 name: 'info',
 data () {
  return {
   id: this.$route.params.id
  }
 }
}
</script>
<style>
.info{
color:blue;
}
</style>

content.vue代碼如下:

<template>
 <div class="content">
  content page
  <div>ID:{{id}}</div>
 </div>

</template>
<!--js部分-->
<script>
export default {
 name:'content',
 data() {
  return {
   id:this.$route.params.id
  };
 }
}
</script>
<style>
.content{
color:blueviolet;
}
</style>

子路由建好了,那怎么把他們串起來呢?這里就要更新index.js了。index.js代碼如下:

import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import HelloWorld from '@/components/Helloworld' //我們新定義的組件
// 引入子頁面
import Info from '@/components/subpage/info.vue'
import Content from '@/components/subpage/content.vue'

Vue.use(Router)

export default new Router({
  routes: [{
      path: '/',
      name: 'Hello',
      component: Hello
    },
    { //新路由
      path: '/helloworld/:id',
      name: 'HelloWorld',
      component: HelloWorld,
      children: [{
          path: 'info/:id',
          component: Info
        },
        {
          path: 'content/:id',
          component: Content
        }
      ]
    }
  ]
})

首先要引入子頁面,然后在父路由下配置一下即可。

到了這里就算是配置好了,運行看看吧~~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM