javascript - Vue Js:传递数据
问题描述
我正在学习 vue js 并尝试将表单数据提交到联系人卡片中。我遇到了这些错误,需要一些帮助来解决 - 在渲染期间访问了属性“电话”,但未在实例上定义。在 <Contact onAddContact=fn > 在 .
感谢任何可以提供帮助的人!
这是我的代码:Contact.Vue
<template>
<div class="contact">
<li>
<h2>{{ name }}</h2>
</li>
<li>
{{ phone }}
</li>
<li>
{{ email }}
</li>
</div>
</template>
<script>
export default {
}
</script>
CreateAForm.Vue
<template>
<div class="container">
<form id="contact" action="" method="post" @submit.prevent="submitContact">
<h3>Form</h3>
<fieldset>
<input type="text" v-model="enteredName" />
</fieldset>
<fieldset>
<input type="tel" v-model="enteredPhone" />
</fieldset>
<fieldset>
<input type="email" v-model="enteredEmail" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
</form>
</div>
</template>
<script>
export default {
emits: ['add-contact'],
data() {
return {
enteredName: '',
enteredPhone: '',
enteredEmail: '',
};
},
methods: {
submitContact() {
this.$emit('add-contact',
this.enteredName,
this.enteredPhone,
this.enteredEmail
);
},
},
};
</script>
应用程序.Vue
<template>
<Nav></Nav>
<CreateAForm></CreateAForm>
<contact @add-contact="addContact"></contact>
<new-contact
v-for="contact in contacts"
:key="contact.id"
:id="contact.id"
:name="contact.name"
:phone-number="contact.name"
:email-address="contact.email">
</new-contact>
</template>
<script>
import Nav from './components/Nav.vue';
import CreateAForm from './components/CreateAForm.vue';
import Contact from './components/Contact.vue';
export default {
name: 'App',
components: {
Nav,
CreateAForm,
Contact,
},
methods:{
addContact(name, phone, email) {
const newContact = {
id: new Date().toISOString(),
name: name,
phone: phone,
email: email,
};
this.contacts.push(newContact);
}
}
}
</script>
解决方案
首先,出现这个错误是因为你没有在Contact.vue里面定义姓名、电话、邮箱。您应该将它们定义为props
. 还有一些其他的问题:
- 在
$emit
你应该提供一个论点。 contacts
应该在 App.vue 数据中定义。- 你没有
NewContact
组件。你的意思是使用那个Contact
。 @add-contact
应该CreateAForm
放在.
工作代码:
<template>
<div class="contact">
<li>
<h2>{{ name }}</h2>
</li>
<li>
{{ phoneNumber }}
</li>
<li>
{{ emailAddress }}
</li>
</div>
</template>
<script>
export default {
props: ['name', 'phoneNumber', 'emailAddress'],
}
</script>
<template>
<div class="container">
<form id="contact" action="" method="post" @submit.prevent="submitContact">
<h3>Form</h3>
<fieldset>
<input type="text" v-model="enteredName" />
</fieldset>
<fieldset>
<input type="tel" v-model="enteredPhone" />
</fieldset>
<fieldset>
<input type="email" v-model="enteredEmail" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
</form>
</div>
</template>
<script>
export default {
emits: ['add-contact'],
data() {
return {
enteredName: '',
enteredPhone: '',
enteredEmail: '',
};
},
methods: {
submitContact() {
this.$emit('add-contact',
{
name: this.enteredName,
phone: this.enteredPhone,
email: this.enteredEmail
});
},
},
};
</script>
<template>
<Nav></Nav>
<CreateAForm @add-contact="addContact"></CreateAForm>
<contact
v-for="contact in contacts"
:key="contact.id"
:id="contact.id"
:name="contact.name"
:phone-number="contact.phone"
:email-address="contact.email">
</contact>
</template>
<script>
import Nav from './components/Nav.vue';
import CreateAForm from './components/CreateAForm.vue';
import Contact from './components/Contact.vue';
export default {
name: 'App',
components: {
Nav,
CreateAForm,
Contact,
},
data() {
return {
contacts: [],
}
},
methods:{
addContact({ name, phone, email }) {
const newContact = {
id: new Date().toISOString(),
name,
phone,
email,
};
this.contacts.push(newContact);
}
}
}
</script>
推荐阅读
- php - 这是我在 laravel 数据库中插入多条记录的代码。那么,它不起作用吗?有人帮我吗?
- linux - 如何从文件名中删除日期linux
- mongodb - 如何在 Mongo DB 中合并文档数组
- jquery - 元属性 og:图像在 whatsapp 中不起作用
- javascript - React 和 PropTypes
- apache-zookeeper - Zookeeper 不为节点的数据更改引发事件,但为删除该节点引发事件
- c++ - 何时包含内置类型和运算符的标头?
- reactjs - 如何在本机反应中测试 ram 内存使用情况?
- php - 相关表的 PhpDOC 语法
- android - 用 FFmpeg 解码的原始 PCM 音频文件或 mp3 文件哪个更快?