vue-router2.0 組件之間傳參及獲取動態參數


<li v-for=" el in hotLins" >
    <router-link :to="{path:'details',query: {id:el.tog_line_id}}">
        <img :src="el.image_list[0]">
        <h3>{{el.tourism_name}} {{el.tog_line_id}}</h3>
        <p>{{el.address}}</p>
    </router-link>
</li>

在組件中,需要傳動態參數時,可以如上例子 

<router-link :to="{path:'details',query: {id:el.tog_line_id}}">

query中的參數id就是要傳的參數,在組件中獲取的方法為:

created: function() {
    var id = this.$route.query.id;
    this.getData(id);
 },

如上述,this.$route.query.id就可以獲取該參數,也可以通過,this.$root.id = id;傳給父組件,父組件中通過

   // 根組件構造器
    var vm = Vue.extend({
      router: router,
      data: function() {
        return {
          id: '11484' //城會玩明細id
        }
      }
    });

定義data中的id,然后子組件中用props傳遞參數

props: {
        id: {
          type: String,
          required: true
        }
      },

router-view中,傳遞該參數:

<router-view :id="id" :order-info="orderInfo">
</router-view>

注意orderInfo時,在這邊用的是:order-info。


免責聲明!

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



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