【问题标题】:Vue-Router Passing Data with PropsVue-Router 使用 Props 传递数据
【发布时间】:2017-12-13 13:34:33
【问题描述】:

我很难使用 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'],
   }
 }
}

【问题讨论】:

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


    【解决方案1】:

    当您以编程方式导航到新路线时,您应该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 {
       }
     }
    }
    

    【讨论】:

    • 欣赏!这行得通。谢谢...我是 Vue 的超级新手。
    • 谢谢它确实有效..浪费了这么多时间..终于得到了
    • 这是一个非常好的答案 - 简洁明了,并提供清晰的解决方案代码。
    • 值必须是标量吗?我可以传递一个对象吗?
    • @Ayyash 你可以传递一个对象。
    猜你喜欢
    • 2020-04-17
    • 2020-02-09
    • 1970-01-01
    • 2019-07-02
    • 2021-03-21
    • 2016-10-22
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多