Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data into a Vue template

Tags:

vue.js

I am fairly new to vue and can't figure out how to add data values within a template. I am trying to build a very basic form builder. If I click on a button it should add another array of data into a components variable. This is working. The I am doing a v-for to add input fields where some of the attributes are apart of the array for that component. I get it so it will add the input but no values are being passed into the input.

I have created a jsfiddle with where I am stuck at. https://jsfiddle.net/a9koj9gv/2/

<div id="app">
    <button @click="add_text_input">New Text Input Field</button>
    <my-component v-for="comp in components"></my-component>
    <pre>{{ $data | json }}</pre>
</div>

new Vue({
    el: "#app",

    data: function() {
        return {
            components: [{
                    name: "first_name",
                    showname: "First Name",
                    type: "text",
                    required: "false",
                    fee: "0"
                  }]
            }
    },

    components: {
        'my-component': {
            template: '<div>{{ showname }}: <input v-bind:name="name" v-bind:type="type"></div>',
            props: ['showname', 'type', 'name']
        }

    },

    methods: {
        add_text_input: function() {
            var array = {
                    name: "last_name",
                    showname: "Last Name",
                    type: "text",
                    required: "false",
                    fee: "0"
                  };
            this.components.push(array);
        }
    }
})

I appreciate any help as I know I am just missing something obvious.

Thanks

like image 849
Chris Avatar asked Jul 08 '16 20:07

Chris


People also ask

How do I transfer data to a Vue component?

Using Props To Share Data From Parent To Child. VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!

How do I transfer data to Vue props?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

How do I transfer data to child component Vue?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.


1 Answers

Use props to pass data into the component.

Currently you have <my-component v-for="comp in components"></my-component>, which doesn't bind any props to the component.

Instead, do:

<my-component :showname="comp.showname" 
              :type="comp.type" 
              :name="comp.name" 
              v-for="comp in components"
></my-component>

Here is a fork of your fiddle with the change.

like image 145
asemahle Avatar answered Oct 22 '22 14:10

asemahle