0

I'm new to Vue JS and I'm trying to pass an HTML table using this array, I have a dropdown where I select the option I want and then it shows it, but I can't figure out how can I put HTML in there, when I do it prints code instead. A little help would be greatly appreciated.

This is the HTML file:

        <div id="app">
            <div>
              {{pickedValue}}
            </div>
            <picker v-model="pickedValue"></picker>
        </div>

This is the JS file and I want to put an HTML table inside list:["c","d","e"]

  console.clear()

Vue.component("picker",{
  props:["value"],
  data(){
    return {
      list:["c","d","e"],
      currentValue: this.value,
      selectedValue: ""
    }
  },
  template:`
    <div>
      <select @change="currentValue = $event.target.value" v-model="selectedValue">
          <option value="">Select</option>
          <option v-for="item in list" :value="item" :key="item">{{item}}</option>
      </select>
    </div>
  `,
  watch:{
    currentValue(newValue){
      if (!this.list.includes(newValue))
        this.selectedValue = "" 

      this.$emit('input', newValue)
    }
  }
})

new Vue({
  el:"#app",
  data:{
    pickedValue: null
  }
})
4
  • "but I can't figure out how can I put HTML in there" can you explain this with an example like what you want to show and how that suppose to happen using the dropdown? Commented Apr 12, 2020 at 8:58
  • When I select a value from the dropdown I want it to show an HTML table, this table should be inside list:["c","d","e"] instead of this letters, but when I do it shows the HTML code, not a table. Maybe this isn't possible or I'm doing something wrong. Commented Apr 12, 2020 at 9:00
  • 1
    may be you can keep the table in parent template only and using v-if show them based on select option selected Commented Apr 12, 2020 at 9:02
  • What HTML exactly and where does it originate from? It's possible with v-html but is unsafe. Please, explain your case. Commented Apr 12, 2020 at 9:20

1 Answer 1

1

You should use v-html instead. Be careful when using this because if "c", "d", "e" are user inputs, it could expose your application to XSS attacks:

    <div id="app">
        <div v-html="pickedValue"></div>
        <picker v-model="pickedValue"></picker>
    </div>

https://v2.vuejs.org/v2/api/#v-html

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.