Vue-Router使用道具传递数据

时间:2017-07-09 22:08:20

标签: vue.js vuejs2 vue-component axios vue-router

我很难用Vue-Router传递道具。当我将它们带入下一个视图时,我似乎无法访问道具。这是我的方法对象:

methods: {
    submitForm() {
      let self = this;
      axios({
        method: 'post',
        url: url_here,
        data:{
          email: this.email,
          password: this.password
        },
        headers: {
          'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      }).then(function(response) {
        self.userInfo = response.data;
        self.$router.push({name: 'reading-comprehension', props: {GUID:self.userInfo.uniqueID }});
      })
   }
}

帖子请求正在运行,但当我尝试路由到新组件并传入道具以便在下一个组件中访问时,它说,

  

属性或方法“guid”未在实例上定义,但是   在渲染期间引用。确保声明反应数据   数据选项中的属性。

顺便说一下,我路由到的组件看起来像这样:

<template lang="html">
  <div class="grid-container" id="read-comp">
    <div class="row">
      <h1>Make a sentence:</h1>
      {{ GUID }}
    </div>
  </div>
</template>

<script>
export default {
 data(){
   return {
     props: ['GUID'],
   }
 }
}

1 个答案:

答案 0 :(得分:27)

以编程方式导航到新路线时,您应该use params, not props

self.$router.push({name: 'reading-comprehension', params: {guid:self.userInfo.uniqueID }});

其次,在路线定义中,您应将the props property设置为true。

{name: "reading-comprehension", component: SomeComponent, props: true }

最后,在您的组件中,您将propsdata分开定义,它应该全部为小写。

export default {
 props: ["guid"],
 data(){
   return {
   }
 }
}