vue.js - Vue JS prop in component always undefined -


i have <select> component. once select category need id , boolean checks if category has subcategory, if does, make api call fetch subcategories.

parent template:

<material-selectcat v-model="catid" name="category" id="selcat">   <option      v-for="cat in cats"      :value="cat.cat_id"      :subcat="cat.has_subcat"      v-text="cat.category_name"   ></option> </material-selectcat> 

child component:

<template>   <select><slot></slot></select> </template>  <script>      export default {        props: ['value', 'subcat'],       watch: {         watcher: function() {}       },       computed: {         watcher: function() {           this.relaod(this.value);           this.fetchsubcategories(this.value, this.subcat);         }       },       methods: {         relaod: function(value) {            var select = $(this.$el);            select.val(value || this.value);           select.material_select('destroy');           select.material_select();         },         fetchsubcategories: function(val, sub) {           var mdl = this;           var catid = val || this.value;           console.log("sub: " + sub);            mdl.$emit("reset-subcats");            if (catid) {             if (sub == 1) {               if ($('.subdropdown').is(":visible") == true) {                 $('.subdropdown').fadeout();               }             } else {               axios.get(url.api + '/subcategories/' + catid)                 .then(function(response) {                   response = response.data.subcatdata;                   response.unshift({                     subcat_id: '0',                     subcategory_name: 'all subcategories'                   });                   mdl.$emit("update-subcats", response);                    $('.subdropdown').fadein();                 })                 .catch(function(error) {                   if (error.response.data) {                      swal({                       title: "something went wrong",                       text: "please try again",                       type: "error",                       html: false                     });                    }                 });             }           } else {             if ($('.subdropdown').is(":visible") == true) {               $('.subdropdown').fadeout();             }           }         }       },       mounted: function() {          var vm = this;         var select = $(this.$el);          select           .val(this.value)           .on('change', function() {             vm.$emit('input', this.value);           });          select.material_select();       },       updated: function() {          this.relaod();       },       destroyed: function() {          $(this.$el).material_select('destroy');       }     } </script> 

but inside fetchsubcategories() function line returns undefined:

console.log("sub: " + sub); 

if check vue devtools tab in chrome inspector, can see of data exists:

cat_id:0 category_name:"all subcategories" has_subcat:0 

but why doesnt has_subcat passed prop?

the subcat prop undefined because not passing component. but, subcat prop doesn't make sense anyway, since want check value each option in select different.

if compose options inside of component definition:

// child component script props: ['value', 'options'], 
// child component template <template>   <select>     <slot>       <option          v-for="option in options"          :key="option.id"         :value="option.id"          :subcat="option.subcat"          v-text="option.text"       ></option>     </slot>   </select> </template> 

and pass correctly formatted options prop component:

// parent component script computed: {   options() {     return this.cats.map((cat) => {       return {         id: cat.cat_id,         subcat: cat.has_subcat,         text: cat.category_name       }     });   } } 
// parent component template <material-selectcat v-model="catid" :options="options" name="category" id="selcat"> </material-selectcat> 

then, correct subcat value option's corresponding value referencing options prop in fetchsubcategories method:

fetchsubcategories: function(val) {   var mdl = this;   var catid = val || this.value;   var sub = (this.options.find(o => o.id === val) || {}).subcat;   ... 

the reason value prop defined in code using v-model directive on component tag. the v-model directive syntactic sugar :value="something" @input="something = $event.target.value". since you've specified catid v-model, passed component value prop.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -