Dynamically adding different components in Vue

前端 未结 3 1786
时光说笑
时光说笑 2021-02-02 01:22

I want to create a simple form builder with Vue where users click on buttons from a menu to add different form fields to a form. I know that if there was just one type of form f

3条回答
  •  迷失自我
    2021-02-02 01:54

    You can pass the field object as props of your form-input component and make the type dynamic:

    Vue.component('form-input', {
      template: '#form-input',
      props: ['field']
    })
    
    new Vue({
      el: '#app',
      data: {
        fields: [],
        inputType: '',
        count: 0
      },
      methods: {
        addFormElement(val) {
          this.fields.push({type: val, placeholder: 'Textbox ' + (++this.count)});
        }
      }
    })
    
    

    Add form element

提交回复
热议问题