vue.js - 如何编辑作为属性传递的 vue 数据?
问题描述
我有一个 Vuex 数据存储,其中包含contacts
. 我在 中显示该数组ContactList.vue
,其中有一个导航到 CreateEditContact.vue 的“编辑”按钮(我将contact
作为名为 的道具传递contactProp
)。所有这些都有效,但众所周知,您不能直接编辑道具。我尝试检查模板为 时是否填充了道具created
,并将其复制到contact
indata
但它不起作用。这样做的正确方法是什么?
联系人列表.vue
<template>
<!-- ...redacted for brevity -->
<!-- I'm reusing the same .vue for creating new and editing contacts -->
<!-- edit contact button -->
<v-btn text
:to="{name: 'createEditContact',
params: {
action: 'edit',
id: contact.id,
contactProp: contact
}}">Edit</v-btn>
<!-- new contact button -->
<v-btn
:to="{ name: 'createEditContact', params: { action: 'create' } }"
outlined
rounded>New</v-btn>
</template>
CreateEditContact.vue
<template>
<div>
<h3>Create or Edit a Contact</h3>
<v-form @submit.prevent="saveContact">
<v-container>
<v-row>
<v-col>
<v-text-field label="Name" v-model="contact.name" />
</v-col>
<v-col>
<v-text-field label="Phone Number" v-model="contact.phone" />
</v-col>
<v-col>
<v-text-field label="Extension" v-model="contact.extension" />
</v-col>
<v-col>
<v-btn type="submit">Save</v-btn>
</v-col>
</v-row>
</v-container>
</v-form>
</div>
</template>
<script>
import axios from "axios";
const options = {
headers: { "Content-Type": "application/json" }
};
export default {
name: "CreateEditContact",
props: {
action: {
type: String,
required: true,
validator: value => ["create", "edit"].indexOf(value) !== -1
},
id: {
required: false
},
// contactProp gets set correctly when editing...
contactProp: {}
},
data() {
return {
// the empty contact (when creating a new contact)
contact: {
id: 0,
name: "",
phone: "",
extension: "",
fullString: "",
telephoneUrl: ""
}
};
},
methods: {
saveContact() {
// redacted for brevity
},
created() {
// when contactProp is populated, I need to use that...
// but contact is always empty when I try to edit one.
// this next line isn't doing what I think it should.
if (this.contactProp) this.contact = this.contactProp;
}
}
};
</script>
解决方案
推荐阅读
- c# - 将参数发布到 api 正文返回错误 403
- linux - 无法在 Kali Linux 中打开 Visual Studio Code
- android - 用新的 APK 替换分阶段发布
- oracle - SQL:如果满足条件,有没有办法排除重复结果
- python - 绘制 Pandas DataFrame 并并排绘制
- javascript - 使用接受语言标头来猜测用户的国家(如果有)
- python - 从强盗扫描中删除 venv 文件夹
- reactjs - @fluentui\react-northstar React 测试库尝试单击时出现下拉错误
- python - 在具有特定刺痛的某行之后,如何使用 Pandas 的 read_csv 在 Python 中读取 csv?
- python - kivyMD 数据表在登录时加载;当我按回然后再次加载数据表时,它会在几次尝试后在数据表周围产生一个光环