0

Iam new in vuejs. So I have this array in my data and when I output the data in console the result is correct. I need to put the categories in an array because it can have multiple categories.

const app = new Vue({ 
   el:"#app",
   data:{
     market:{
        categories:[]
     }
   },
   methods: {
      init: function(){
         var app = this;
         var url = '/api/categories/0';
            axios.get(url).then(function(response){
               app.market.categories[0] = response.data;
               console.log(app.market.categories[0]);
        });
      }
   }
});

But when use v-for in the ui it doesn't show. Can you help? Thanks`

 <select class="form-control form-control-no-border" id="s-category1">
      <option value="">Please select a category</option>
      <option v-for="category in market.categories[0]" :value="category.external_code">@{{ category.name  }}</option>
  </select>

1 Answer 1

3

Vue cannot react to changes to array elements when you access them with square brackets. When you modify array elements, you need to access them with array methods, or use Vue.set, like this...

Vue.set( app.market.categories, 0, response.data );
Sign up to request clarification or add additional context in comments.

4 Comments

What do you see? What do you see in your categories array?
in the ui nothings happen, but when I output the categories in console it shows an array in categories. I need to put the data in categories[0] since categories[1] can have different set of array
Ah ok, you have a 2d array for categories? Then your problem is updating the array. You need to use Vue.set(). Hold on I'll update the answer.
No problem. There are very few gotchas in Vue, but this is one of them. It's a limitation of javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.