2016-12-04 44 views
0

我只是想用路由器PARAMS一個基本功能,我得到未定義router.paramsVuejs router.params不確定

我的模板

<div id="app><template id="image-capture"> 
      <div class="row" > 
       <router-link :to="{ path: 'vc/'+item.id}" class="btn btn-primary"> ACCEPT</router-link> 
    </div> 
     </template></div> 

現在我的URL看起來像這樣http://localhost/cams-web/#/vc/3

const ic = { 
    template: '#image-capture' , 
} 

const vc = { 
    template: '#video-capture' , 

    mounted() { 
    this.init() 
    }, 

    methods: { 
    init() { 
    console.log(router); //returns object 
console.log(router.params); //undefined.. 
    }, 
    } 
} 


const routes = [ 
    { path: '/ic', component: ic}, 
    { path: '/vc/:id', component: vc} 
] 

const router = new VueRouter({ 
    routes 
}) 

new Vue({ 
    router, 
    }).$mount('#app') 

回答

2

要訪問router params,您需要在代碼中使用this.$route.params。你的代碼應該如下:

const vc = { 
    template: '#video-capture' , 

    mounted() { 
    this.init() 
    }, 

    methods: { 
    init() { 
     console.log(this.$route); //should return object 
     console.log(this.$route.params); //should return object 
     console.log(this.$route.params.id); //should return id of URL param 
    }, 
    } 
} 

這裏是工作fiddle

+0

哈..感謝這麼多...我試圖「this.route」..當$路線未定義時從來沒有得到這個線索$ route .. –

+0

謝謝,我試圖訪問此。$路由器它返回一個不同的對象。 – tomhughes