0

I am pretty new to the wonderful world of Vue3(or Vue), and I encountered a problem with "conditional" rendering with select changed. What I want to achieve is "different value of select will show different div for argument setting". However, my code do nothing when I changed the value of select, can anybody give me a hint? The following is my .vue file code:

  <div class="">

    <div class="">
      <p>Data integration type: 
        <select name="" id="" v-on:change="onchange" v-model="integration_type">
          <option value="">--Please choose one--</option>
          <option value="1+2">1+2</option>
          <option value="1+1">1+1</option>
        </select>
      </p>
    </div>

    <div class="" v-bind:style="display1">
      <p>Arguments for 1+2: 
        <input type="text">
      </p>
      <br>
      <button v-on:click="onclick">start integration</button>
    </div>

    <div class="" v-bind:style="display2">
          <p>Arguments for 1+1:
        <input type="text">
      </p>
      <br>
      <button v-on:click="onclick">start integration</button>
    </div>

  </div>
</template>

<script setup lang="ts">
let integration_type = ""
let display1 = "display:none;"
let display2 = "display:none;"

const onchange = function(){
  if(integration_type == "1+2"){
    display1 = "display:inline;"
    display2 = "display:none;"
  }else{
    display1 = "display:none;"
    display2 = "display:inline;"
  }
}

const onclick = function(){

}

</script>

<style scoped>

</style>

1 Answer 1

1

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    let integration_type = ref("")
    let display1 = ref("display:none;")
    let display2 = ref("display:none;")

    const onchange = function(){
      if (integration_type.value == "1+2"){
        display1.value = "display:inline;"
        display2.value = "display:none;"
      } else {
        display1.value = "display:none;"
        display2.value = "display:inline;"
      }
    }

    const onclick = function(){

    }
    
    return {
      display1, display2, integration_type, onchange
    }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>
<div id="demo" class="">

    <div class="">
      <p>Data integration type: 
        <select name="" id="" @change="onchange" v-model="integration_type">
          <option value="">--Please choose one--</option>
          <option value="1+2">1+2</option>
          <option value="1+1">1+1</option>
        </select>
      </p>
    </div>

    <div class="" :style="display1">
      <p>Arguments for 1+2: 
        <input type="text">
      </p>
      <br>
      <button @click="onclick">start integration</button>
    </div>

    <div class="" :style="display2">
          <p>Arguments for 1+1:
        <input type="text">
      </p>
      <br>
      <button @click="onclick">start integration</button>
    </div>

  </div>
</div>

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

1 Comment

Thanks. The code works in Composition API, and what should it look like in Script setup?

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.