2

I am working on my first Web App using vue.js and I want to have a button that will create a new textarea when clicked. It seems to work fine when I checked it at jsfiddle, but when I load it in visual studio and run it, I do not get any results and the textarea and button do not display.

<!-- HTML -->
<div id="column1" style="float:left; margin:0; width:25%;">
            <h4>Column Header</h4>
            <div id="tile">
                <input type="button" value="+" @click="addInput">
                <div class="inputArea" v-for="input in inputs" :key="input.id">
                    <textarea placeholder="Enter text... " :id="input.id" v-model="input.value"></textarea>
                </div>
            </div>
        </div>


/* Vue.js on my main.js */
var tile = new Vue({
    el: '#tile',
    data: {
        counter: 0,
        inputs: [{
            id: 'text0',
            value: '',
        }],
    },
    methods: {
        addInput() {
            this.inputs.push({
                id: 'text${++this.counter}',
                value: '',
            });
        }
    }
});

I expected a textbox to be created each time the button is pressed. The textbox should appear beneath the previous one. What is happening though is I don't see either the button or any textboxes. Thanks in advance for taking the time to consider this!

3
  • do you see any errors in console? Commented Jun 13, 2019 at 11:43
  • 1
    did you install, import vue? Commented Jun 13, 2019 at 11:44
  • There aren't any errors in the console that I can see and I do have import vue installed. When I run it from VS 2017, it opens in IE version 11.1146.16299.0 (incase that helps) Commented Jun 13, 2019 at 12:00

1 Answer 1

1

data property must be a function (returning object). Otherwise it's initialised only once on first render.

Change it to this and it should work:

data() {
  return {
    counter: 0,
    inputs: [{
      id: 'text0',
      value: '',
    }],
  }
}

And also, when using template literals, you have to use back-ticks ( ` ), so the method should be:

addInput() {
  this.inputs.push({
    id: `text${++this.counter}`,
    value: '',
  });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have made both of these changes and while they work at jsfiddle.net/chrisvfritz/50wL7mdz (replacing the HTML and JS with my code), I still don't see any change when running out of VS. And thank you very much for the quick reply to my question.
To update, it now works in my app when I add <script src="cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> to the <head> and place the javascript at the bottom of the body in my index.html. But it doesn't seem to work if it is on the main.js page. I had also included src="main.js" but that didn't help.

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.