2017-05-15 2 views
0

내 vue 템플릿에 구성 요소를 동적으로 추가하려고합니다. 상황이 예상대로 진행되지 않았습니다. 동적으로 생성 된 VueJS 구성 요소를 렌더링하는 방법

클릭이 발생

Vue.component('form-x-text', { 
 
    delimiters: ["[[", "]]"], 
 
    template: ` 
 
    <div v-if="_.indexOf(['text','email','tel','number','url','color'], fType) > 0" class="form-group" :class="{'has-error': errors.has(fName)}"> 
 
     <label :for="fId">[[fLabel]]</label> 
 
     <input v-validate="{ rules: parsedRule }" class="form-control" :type="fType" :name="fName" :id="fId" data-vv-delay="700"> 
 
     <span class="field-error" v-show="errors.has(fName)"> [[ errors.first(fName) ]]</span> 
 
    </div> 
 
    `, 
 
    props: { 
 
    fType: { 
 
     type: String, 
 
     validation: function(value){ 
 
     return _.indexOf(['text','email','tel','number','url','color'], value) > 0 
 
     }, 
 
     required: true 
 
    }, 
 
    fName: { 
 
     type: String, 
 
     required: true 
 
    }, 
 
    fId: null, 
 
    fLabel: null, 
 
    fRule: null 
 
    }, 
 
    computed: { 
 
    parsedRule: function(){ 
 
     console.log(this.fRule); 
 
     return JSON.parse(this.fRule); 
 
    } 
 
    } 
 
}) 
 

 

 
class FormX{ 
 
    constructor(){ 
 
    
 
    } 
 
    
 
    static newForm(){ 
 
    return JSON.parse(` 
 
    { 
 
     "node": "root", 
 
     "child": [] 
 
    } 
 
    `) 
 
    } 
 
    
 
    static textInput(ftype, name, label, id, required = false){ 
 
    
 
    let emailR = false; 
 
    
 
    if (ftype == 'email'){ 
 
     emailR = true; 
 
    } 
 
    
 
    return JSON.parse(
 
    ` 
 
     { 
 
     "node": "element", 
 
     "tag": "form-x-text", 
 
     "attr": { 
 
      "f-type": "${ftype}", 
 
      "f-name": "${name}", 
 
      "f-label": "${label}", 
 
      "f-id": "${id}" 
 
     } 
 
     } 
 
    ` 
 
    ) 
 
    } 
 
    
 
} 
 

 

 
var builder = new Vue({ 
 
    el: '#builder', 
 
    delimiters: ["[[", "]]"], 
 
    data: { 
 
    blankTemplate: { 
 
     node: 'root', 
 
     child: [ 
 
     { 
 
      node: 'element', 
 
      tag: 'div', 
 
      attr: { id: '1', class: 'foo'}, 
 
      child: [] 
 
    }, 
 
    workingTemplate: { 
 
     content: {} 
 
    }, 
 
    primaryButton: {}, 
 
    secondaryButton: {}, 
 
    bForm: null, 
 
    }, 
 
    methods: { 
 
    openFormDesigner: function(){ 
 
     
 
     $('#formDesignerModal').modal('show'); 
 
     this.primaryButton = {'show':false, 'text':'Save', 'spanstyle':'fa fa-floppy-o'}; 
 
     this.secondaryButton = {'show':true, 'text': 'Close'}; 
 
    }, 
 
    addToFormCanvas: function(ftype){ 
 
     if (!ftype){ 
 
     console.log("You need to provide the type of the Form element"); 
 
     }else if (ftype == 'email'){ 
 
     this.bForm.child.push(FormX.textInput('email', 'mail1','mail1','mail1')); 
 
     }else if (ftype == 'text'){ 
 
     this.bForm.child.push(FormX.textInput('text', 'mail1','mail1','mail1')); 
 
     }else if (ftype == 'number'){ 
 
     this.bForm.child.push(FormX.textInput('number', 'mail1','mail1','mail1')); 
 
     } 
 
     
 
    }, 
 
    jsonToHtml: function(jsono){ 
 
     return json2html(jsono); 
 
    }, 
 
    getAttr: function(aobj){ 
 
     return aobj 
 
    } 
 
    }, 
 
    mounted: function(){ 
 
    $('#vwr-panel > iframe').contents().find('body').css({"margin":"0 auto", "background-color":"#fff"}).html(
 
     json2html(this.blankTemplate)); 
 
    
 
    
 
    }, 
 
    created: function(){ 
 
    this.bForm = FormX.newForm(); 
 
    }, 
 
    computed: { 
 
    bFormHtml: function(){ 
 
     return json2html(this.bForm) 
 
    } 
 
    }, 
 
    filters: { 
 
    capitalize: function(v){ 
 
     return _.toUpper(v); 
 
    } 
 
    } 
 
})
, 나는 기본적으로 내 VUE 인스턴스의 데이터 객체에 추가 :

나는이 구성 요소를 가지고있다. 그런 다음 json2html을 사용하여이 데이터 객체를 실제로 "html"로 변환합니다.

구성 요소가 렌더링 될 것으로 예상되지만 관리자가 볼 수는 있지만 렌더링하지는 않습니다. 당신이 변경 가능한 <component> 태그를 사용하여 시도 해 봤나

나는 <div v-html="bFormHtml"></div>

답변

0

와 구성 요소를 삽입 무엇입니까?

HTML :

... 
<component :is="componentId"></component> 
... 

스크립트 :

... 
data: { 
    componentId: 'blank' 
}, 
components: { 
    'blank': { 
     'template': '<div></div>' 
    } 
}, 
methods: { 
    'updateComponent': function() { 
     this.componentId = 'form-x-text' 
    } 
} 
... 

JsFiddle 여러 구성 요소의 일례.

정확히 어떤 것이 있는지는 잘 모르겠지만 HTML을 삽입하는 것보다 더 잘할 수 있습니다.

관련 문제