【问题标题】:Vuejs router.params undefinedVuejs router.params 未定义
【发布时间】:2016-12-04 07:13:54
【问题描述】:

我只是在尝试使用路由器参数的基本功能,而我的 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')

【问题讨论】:

    标签: vue-router vue.js


    【解决方案1】:

    要访问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

    【讨论】:

    • 哈..非常感谢...我正在尝试“this.route”..当 $route 未定义时,从来没有知道 this.$route..
    • 谢谢,我试图访问 this.$router,它返回一个不同的对象。
    猜你喜欢
    • 2019-03-09
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2018-08-20
    • 2020-08-29
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多