0

I am working on a UI Builder project using Vue JS and Pinia. This app has an editor section and an output section side by side. The editor section contains several input fields like name, image, and text colour which are bonded to the output section to show the reactive data.

On a certain button click user can add dynamic input fields as well. When the button is clicked the following method from pinia data store is triggered to add another HTML div with an input field in the editor section and a respective one in the output section.

My problem is that the Vue js curly brace is not working properly when rendering in the Vue component as I am setting it as a Javascript string. `{{ dataStore.fieldsData.link }}

triggered method:

addLink() {
    console.log("add link");
    const editor = document.getElementById("editor");
    const newEditorDiv = document.createElement('div');
    newEditorDiv.className="w-full";
    newEditorDiv.classList.add("mb-5");
    newEditorDiv.innerHTML=`
        <label class="tracking-wide text-gray-700 text-md font-bold mb-7" for="">
            Link
        </label>
        <input
            class="bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500"
            id="" type="text" v-model="dataStore.fieldsData.link" />
    `;
    editor.append(newEditorDiv);
        
    // --------------------------------
    const output = document.getElementById("output");
    const newOutputDiv = document.createElement('div');
    newOutputDiv.className="w-full mb-5";
    newOutputDiv.innerHTML=`
        <div class="w-full text-center mt-10 px-5 py-3 shadow bg-gray-300 rounded">
            {{  dataStore.fieldsData.link  }}
        </div>
    `;
    output.append(newOutputDiv);
}

1 Answer 1

2

You can't add a Vue code as innerHTML to an element and expect it to work, it will be inserted as it is.

I recommend you to use Render Functions to dynamically add Vue template.

Sign up to request clarification or add additional context in comments.

Comments

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.