【发布时间】:2017-05-28 01:47:57
【问题描述】:
我有一个使用子组件的表单组件。我想在父组件中使用来自子组件的数据。
我在 html 中的组件:
<candidates-form endpoint='/candidates/create' buttontext='Add Candidate'></candidates-form>
那么这里是 Vue 实例:
CandidatesForm.vue
<template>
<div class='row'>
<div class='form-group'>
<label>Name:</label>
<input type='text' class='form-control' v-model='name'>
</div>
<div class='form-group'>
<location-input></location-input>
</div>
<button class='btn btn-primary'>{{buttontext}}</button>
</div>
</template>
<script>
export default {
data() {
return {}
},
props: ['endpoint', 'buttontext'],
ready() {}
}
</script>
我在那里使用了 locationInput 组件,它可以很好地呈现到屏幕上。该组件为输入字段实现了 Google Maps 预输入功能,如下所示:
LocationInput.vue
<template>
<place-input
:place.sync="placeInput.place"
:types.sync="placeInput.types"
:component-restrictions.sync="placeInput.restrictions"
class='form-control'
label='Location: '
name='location'
></place-input>
<pre>{{ placeInput.place | json }}</pre>
</template>
<script>
import { PlaceInput, Map } from 'vue-google-maps'
export default {
data() {
return {
placeInput: {
place: {
name: ''
},
types: [],
restrictions: {'country': 'usa'}
}
}
},
props: ['location'],
components: {
PlaceInput
},
ready() {
}
}
</script>
<style>
label { display: block; }
</style>
我想将name 的值和来自placeInput.place 的信息提交到服务器。
我在我的主应用程序文件中注册这两个组件,如下所示:
Vue.component('locationInput', require('./components/LocationInput.vue'));
Vue.component('candidatesForm', require('./components/CandidatesForm.vue'));
const app = new Vue({
el: 'body'
});
如何将placeInput.place 数据从位置输入组件传递到候选表单组件?
我想将 placeInput.place 和 name 数据从 Candidate-form 组件发送到服务器,很可能使用 vue-resource。
【问题讨论】:
标签: javascript vue.js vue-component