首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuejs router.params未定义

Vuejs router.params未定义
EN

Stack Overflow用户
提问于 2016-12-04 15:13:55
回答 1查看 15.2K关注 0票数 5

我只是在尝试使用路由器参数的基本功能,而我还没有为router.params定义

我的模板

代码语言:javascript
复制
 <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

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-04 15:48:19

要访问router params,您需要在代码中使用this.$route.params。您的代码应该如下所示:

代码语言:javascript
复制
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

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40956347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档