【发布时间】:2018-01-22 15:16:33
【问题描述】:
我正在尝试为我的应用程序实现搜索组件,父组件具有搜索文本框和按钮。当用户提供一些值时,我想将数据发送到 api 并在子组件中显示结果。我有点困惑在哪里调用 api 以及如何在子组件中填充数据。另外,最初我的子组件不应该在父组件中呈现,当搜索得到一些结果时它可以呈现。请帮助我如何在 vue js 2 中实现搜索功能。
父组件
<template>
<div><h3> Search </h3></div>
<div class="row">
<form role="search">
<div class="form-group col-lg-6 col-md-6">
<input type="text" v-model="searchKey" class="form-control">
</div>
<div class="col-lg-6 col-md-6">
<button type="button" id="btn2" class="btn btn-danger btn-md" v-on:click="getInputValue">Search</button>
</div>
</form>
</div>
<result :searchdataShow='searchData'></result>
</template>
<script>
import resultView from './result'
export default {
components: {
'result': resultView
},
data () {
return {
searchKey: null,
searchData: null
}
},
methods: {
getInputValue: function(e) {
console.log(this.searchKey)
if(this.searchKey && this.searchKey != null) {
this.$http.get('url').then((response) => {
console.log(response.data)
this.searchData = response.data
})
}
}
}
</script>
搜索结果组件(子组件)
<template>
<div>
<div class="row"><h3> Search Results</h3></div>
</div>
</template>
<script>
export default {
props: ['searchdataShow']
}
</script>
【问题讨论】: