Issue with Vue.js v-for not displaying component

Hello, I am facing an issue with the v-for feature as it is not rendering at all. You can view the fiddle by clicking on this link https://jsfiddle.net/tadeyemi/k6s4gv85/. I am puzzled as to why it's not working. Can anyone provide some insight?

<div id="app">
  <h1>Finds</h1>
  <div>
    <input ref="option">
  </div>
  <button v-if @click="addFind">
    New Find
  </button> 
  <p v-for="(option,idx) in options.slice(1)">
    <span @click="removeOption(idx+1)">Option{{idx+1}}: {{option}}</span>
  </p>
</div>

Here is the JavaScript code:

new Vue({
  el: '#app',
  data: {
    options: [],
    count:0
  },
  methods: {
    addFind: function () {
    var msg = this.$refs.option.value;
    console.log(this.options);
    if( msg.trim() != "" ){
        this.count++;
        var i = this.count;
      this.options[i]= this.$refs.option.value.trim();
    }
    },
    removeOption:function(index){

    this.options.splice(index,1);
    this.count--;

    }
  }
});

Answer №1

Your code has a few issues, but the main one is violating reactivity rules which are detailed here: https://v2.vuejs.org/v2/guide/reactivity.html#For-Arrays

Vue won't detect changes to an array in these cases:

- Directly setting an item with index like vm.items[indexOfItem] = newValue

- Modifying the length of the array like vm.items.length = newLength

In simpler terms, using this.options.push(msg.trim()); will work while using

this.options[i]= this.$refs.option.value.trim();
won't

I made some adjustments to the fiddle to fix the issue: https://jsfiddle.net/63jyw7gz/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

iOS Safari is failing to follow the CORS policy by neglecting to send the necessary preflight OPTIONS request

One issue I've encountered while using Axios and VUEJS is a persistent error message on Safari only stating xmlhttprequest cannot load XXX due to access control checks. Strangely, the application functions perfectly on Chrome and Brave, but not on Saf ...

Is it possible to pass props to a child component within router-view in Vue.js?

I have a dilemma with my three Views that rely on data from the parent - I only want one of these children to be displayed at a time. Parent The Parent component fetches API data and manipulates it, storing it as an array of objects in the data() object. ...

Encountering error code 2064 without any clear explanation in sight

Hey, I'm currently facing an issue while uploading values to a MySQL table from Node.js. The error 1064 keeps popping up, indicating that the query is badly formatted. However, I can't seem to pinpoint the exact problem. Here's the query in ...

By utilizing the power of JSONP, data transfer limitations can be eliminated

Is it possible to remove the limitation on data sent using JSONP? Here's my code. I'm attempting to send 3000 characters (an image converted to base64 data) at a time to a service (serviceCall.ashx). Since my data is quite large, up to 30,000-40, ...

Do we really need to use the eval function in this situation?

Just wondering, is it reasonable to exclude the eval() function from this code? Specifically how <script> ... ... function addGeoJson (geoJsonPath, iconPath = "leaflet-2/images/marker-icon.png", iconSize = [30,50], popUpContent, ...

Changing the color of the cursor in input fields in Ionic3

Is there a way to customize the color of the cursor in text input fields within my Ionic 3 app on Android? I am referring to the marker that indicates the current position within the text. In the screenshot below, you can see that the cursor is currently g ...

SlickGrid checkbox formatter/editor experiencing issues with data consistency

Exploring the functionalities of SlickGrid'seditors/formatters, I delved into how these features could potentially alter the data object utilized for constructing the grid (as modifications within the table are usually reflected in the original data o ...

The AJAX call for fetching logged in users is coming back as undefined

Hey there! I'm currently diving into the world of AJAX and am working on enhancing the user experience on my admin system. One thing I want to achieve is to update information like online users, messages, tasks, etc. every 30 seconds or so (for testin ...

Utilizing jQuery to establish various AJAX requests through functions on page load and button click

Utilizing a basic ajax call both on page load and click events, where I have defined separate functions for each. Despite using different URLs and parameters in the function, the JSON data from the previous call is still being displayed when clicked. Bel ...

Tips to prevent elements from overlapping in Angular applications

I am facing an issue with my Angular-based app where I dynamically populate the page with table rows. There is an app-console element below the page that has a fixed position. Whenever I click the button to add a new row, it overlaps with the app-console. ...

Footer placement not aligning at the bottom using Bootstrap

I'm having trouble getting my footer to stay at the bottom of my website without it sticking when I scroll. I want it to only appear at the bottom as you scroll down the webpage. Currently, the footer is positioned beneath the content on the webpage. ...

Unable to retrieve PHP data using AJAX

Index.html → <form> <div class="form-group"> <!-- <input type="text" id="name" class="form-control" placeholder="Enter Name"> --> </div> <div ...

Modifying Owlcarousel 2 settings post initialization

I've encountered an issue with the settings of a carrousel plugin I'm using. I've tried to change the loop and nav options but it doesn't seem to be working. Here is the code snippet: var owl = $(".owl-carousel").owlCarousel({loop: tru ...

Why does the API in Next Js get triggered multiple times instead of just once, even when the strict mode is set to false?

React Query Issue I am currently facing an issue with React Query where the API is being triggered multiple times instead of just once when the selectedAmc value changes. I have tried setting strict mode to false in next.config.js, but that didn't so ...

Unable to pass several parameters to a Component

I'm attempting to send three URL parameters to a React Component. This is my approach: App.js: <Route path="/details/:id(/:query)(/:type)" handler={DishDetails}/> DishDetails.js: class DishDetails extends Component { constructor(props) { ...

Uniform Height for Several Selectors

I came across a script on Codepen created by RogerHN and decided to customize it for a project I'm currently working on: https://codepen.io/RogerHN/pen/YNrpVa The modification I made involved changing the selector: var matchHeight = function ...

Utilizing regular expressions in Javascript to retrieve multiple instances

This paragraph contains a specific string txt = "Local residents o1__have called g__in o22__with reports..."; that requires extracting numbers between each occurrence of o and __ If we use the following regex: txt.match(/o([0-9]+)__/g); We will obtain ...

Having difficulty creating a file labeled as "undefined" within the grunt plugin

As a newcomer to writing grunt plugins, I've encountered an issue when attempting to run it: Running "inject_css:dev" (inject_css) task Warning: Unable to write "undefined" file (Error code: undefined). Use --force to continue. The structure of my p ...

Applying background-image in ngStyle results in an undefined value

I have been attempting to incorporate images (retrieved through an API) as the background-image of an element. However, I keep encountering the error message Cannot read property 'url' of undefined, even though the URL is actually being rendered. ...

What is the best way to create a CSS class for a list element in React?

I am facing an issue with styling buttons in my UI. These buttons represent different domains and are dynamically generated based on data fetched from the server using the componentDidMount() method. Since I do not know the quantity of buttons at the time ...