【问题标题】:How to pass parameters from one component to another in router.push() (Vue.js)?如何在 router.push() (Vue.js) 中将参数从一个组件传递到另一个组件?
【发布时间】:2020-05-25 11:18:45
【问题描述】:

我正在实现一个功能,用户单击编辑按钮(存在于一行内),该行中的所有详细信息都应该传递给表单组件,我应该从参数中获取这些字段的默认值。我不确定如何实现这一点...有人可以提出解决方案吗?

----------inside table component------------------

edit_row(id){
      // alert("deleting row " + id);
      var d_fname = this.users[id].fname;
      var d_lname = this.users[id].lname;
      var d_tech = this.users[id].tech;
      this.$router.push({name:  'form/', params: {d_fname, d_lname, d_tech}});
      // this.$router.push({name:'/form1',  params:{d_fname,  d_lname,  d_tech}});
    }


----------------inside form component------------------------

<template>
  <form id="form">
    <h4>Personal Information</h4>
    <br />
    <input type="text" placeholder="First Name" v-model="fname" />
    <p>{{user_info}}</p>
    <br />
    <input type="text" placeholder="Last Name" v-model="lname" />
    <br />
    <input type="text" placeholder="Technologies" v-model="tech" />
    <br />
    <button type="button" value="submit" @click="submit_info()">Submit</button>
  </form>
</template>

<script>
export default {
  name: "form1",
  props:["d_fname", "d_lname", "d_tech"],
  data() {
    return {
        fname: "",
        lname: "",
        tech: ""
    };
  }
}

【问题讨论】:

    标签: javascript vue.js parameter-passing vue-router


    【解决方案1】:

    您需要在 params 对象中将参数作为键值传递,还需要在路由声明时将 props 设置为 true。

    {path: "form/", name:"form", component: FormComponent,  props: true}
    

    你应该参考这个。

    Passing props with programmatic navigation Vue.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      相关资源
      最近更新 更多