我只是在尝试使用路由器参数的基本功能,而对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>
现在我的网址看起来像这样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')
要访问路由器参数,您需要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 
    },
  }
}
这是工作的小提琴。
| 归档时间: | 
 | 
| 查看次数: | 6213 次 | 
| 最近记录: |