2016-12-04 43 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