【问题标题】:Vue 3 prop returned as a stringVue 3 道具以字符串形式返回
【发布时间】:2021-04-29 19:35:44
【问题描述】:

我有一个路由器链接:

  <router-link
    :to="{
    name: 'EditStaff',
    params: { id: key, staffInfo: staff }
    }"
  >
    <i class="fas fa-edit text-info"></i>
  </router-link>

以及带有以下属性的视图;

  props: {
    staffInfo: {
      type: Object,
      required: true
    }
  }

有路线;

  {
    path: "/staff/edit/:id",
    name: "EditStaff",
    component: EditStaff,
    props: true,
    beforeEnter: requireAuth
  },

当我在 EditStaff 视图中记录 props.staffInfo 时,我得到了 Object 的字符串版本 "[object Object]"

我需要更改什么来获取对象?

【问题讨论】:

    标签: vue-router vuejs3 vue-composition-api vue-props


    【解决方案1】:

    如果您使用的是console.log(props.staffInfo ),它可能会将您的对象就地转换为字符串

    改用console.log(JSON.stringify(props.staffInfo ,null, 2))

    const staffInfo = {gettingFired: 'false?'};
    console.log(staffInfo); // what you'd hope to get
    console.log(staffInfo.toString()); // but you might be doing this, especially if it happens in the template
    console.log(JSON.stringify(staffInfo).toString()); // so convert to string first

    如果您尝试在模板中输出它,{{ props.staffInfo }} 将被转换为使用 toString(或等效项)`

    您可以使用&lt;pre&gt;{{ JSON.stringify(props.staffInfo ,null, 2)) }}&lt;/pre&gt; 获得更好的输出。

    【讨论】:

    • 我得到了同样的结果。
    • 模板中有这个吗?
    • 在 setup() 中。
    • 在路由器链接中,我已将参数更改为参数:{ id: key, staffInfo: JSON.stringify(staff ,null, 2) } 我得到了预期的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2015-04-02
    • 2016-11-23
    • 2014-04-26
    • 1970-01-01
    相关资源
    最近更新 更多