vue.js - Show child component when promise data is exists and also render the data in child omponent -


i trying implement search component application, parent component have search text box , button. when user provide value want send data api , show result in child component. bit confused call api , how populate data in child component. also, child component should not render in parent component, when search result can render. please me how implement search functionality in vue js 2.

parent component

<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 './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> 

search result component(child component)

<template>   <div>    <div class="row"><h3> search results</h3></div>   </div> </template> <script>   export default {    props: ['searchdatashow']   } </script> 

create boolean variable keeps track of ajax request, call loading, or fetcheddata, depending on context. before ajax call, set true, after call, set false.

once have variable working, can conditionally render result component v-if. show loading icon corresponding v-else.

also template doesn't seem have root element, required.

<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 v-if="!loading" :searchdatashow='searchdata'></result>   <div v-else>loading!!</div> </template> <script>  import resultview './result'  export default {   components: {     'result': resultview   },  data () {     return {       loading: false,       searchkey: null,       searchdata: null     }   },   methods: {    getinputvalue: function(e) {      console.log(this.searchkey)      this.loading = true;      if(this.searchkey && this.searchkey != null) {        this.$http.get('url').then((response) => {          console.log(response.data)          this.loading = false;          this.searchdata = response.data        })      }    } } </script> 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -