Vue3's rendering responsiveness with the <select> element has been altered

I'm just starting to explore the exciting world of Vue3 (or Vue) and I've run into a challenge with conditional rendering based on select input changes. My goal is to display different div elements for argument setting based on the selected value in the dropdown. However, my code doesn't seem to be working when I change the select value. Can anyone offer me some guidance? Here's an excerpt from my .vue file:

  <div class="">

    <div class="">
      <p>Choose 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>

Answer №1

Check out the code snippet below:

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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a7a4b491e2ffe3ffe3e8">[email protected]</a>/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="">--Select 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a net::ERR_EMPTY_RESPONSE error while trying to use the DELETE Method in Angular 4

I have been using the following method to delete data: return this.http.delete(this.ApiURL, new RequestOptions({headers: headers,body: body })) .map((res: Response) => res.json()); However, I encountered the net::ERR_EMPTY_RESPONSE error. Interestingl ...

Exploring Angular 2.3 Component Inheritance and the Importanc of Dependency Injection

Is there a way to share dependency injection between child and parent components using the new Angular 2.3 Component Inheritance? For instance, I aim to transfer AlertService down into the parent component while keeping TraingCompanyService in the derived ...

Convert numeric month to its 3-letter abbreviation

Receiving the date time value from the server and storing it in a variable named a1: let a1 = (new Date(estimatedServerTimeMs)); console.log of a1 Sun Apr 05 2020 11:36:56 GMT+0530 (India Standard Time) The date is converted to a simpler format such as ...

Error encountered while working with SVG in a functional React component with typescript

I have a customized icon component that looks like this: import React from 'react'; type IconProps = { width?: number; height?: number; color?: string; styleName?:string; }; const MyIcon: React.FC<IconProps> = ({ width ...

Tips for managing the dimensions of the <label> element within a React component

I'm facing an issue where the element size is not matching the box size as expected. Additionally, the width property doesn't seem to work in React. Does anyone know how to solve this problem? const DragDrop = () => { ... return ( &l ...

I'm experiencing an issue with my website where it appears broken when I build it, but functions properly when I use npm run dev in Next

For my project, I have utilized NextJs, Tailwind, React, and Typescript. It is all set and ready to be hosted. After using "output: export" in next.config.js and running npm run build, the process was successful. However, when viewing my website locally, I ...

Issues arising with the proper functioning of the normalModuleReplacmentPlugin in Webpack on Windows

Recently, I began developing a library in TypeScript to fetch data from an API and using Webpack as a bundler. The library needs to interact with different APIs for development, testing, and production environments, so I created various environment files t ...

What are the best practices for integrating Qt with React in TSX?

While I've figured out how to communicate qt with JS successfully, the challenge arises when trying to use React in TSX for frontend development. The previous solution failed on this front. Backend code: #./main.py import os from PySide6.QtWidgets ...

The function threw an error: "TypeError: this.registeredUserArray.some is not a function"

I have been attempting to store a registered user object inside localstorage. However, when I try to retrieve or set those values from localstorage in an array and parse it back to an object, I encounter an error stating that ".push" or ".some" is not a ...

Typescript Regular Expression Issue: filter function is not returning any matches

Currently, I am developing an Ecommerce API and working on a class specifically for search queries. My approach involves using regex and typescript with node.js. Although I have based my project on a JavaScript node project, I am encountering an issue wher ...

Tips for maintaining type information while setting values in a class membership declaration

In my current class implementation, I have defined the following: export class HavenHandler implements IHavenHandler { opts: { auto: true handleGlobalErrors: true; revealStackTraces: true } // compared to: opts: { auto: boolean ...

Guide to removing selected value from a combobox in Angular

I am working on a simple HTML file that consists of one combobox and one clear button. I want the clear button to remove the selected value from the combobox when clicked. Below is my code: mat-card-content fxLayout="row wrap" fxLayoutAlign="left" fxLayou ...

How can different styles be seamlessly combined when customizing Fabric components?

Imagine I am enhancing a Fabric component by adding custom styles and wishing to combine any additional styles passed in through its props. Here's the solution I've devised: const { TextField, Fabric , IButtonProps, mergeStyleSets } = window.Fab ...

Two appearances of the record indicate that it has been updated

Whenever I update a record, it ends up appearing twice on the screen. This is how I am loading it: ngOnInit() { this.loadItems(); } loadItems(){ this.first_time_enter = true; console.log(this.first_time_enter); ...

Encountering issues with accessing properties of undefined while chaining methods

When comparing lists using an extension method that calls a comparer, I encountered an error. Here is the code snippet: type HasDiff<T> = (object: T, row: any) => boolean; export const isListEqualToRows = <T>(objects: T[], rows: any[], has ...

Troubleshooting the issue of Angular 2 error: Cannot access the 'getOptional' property

Currently, I am navigating my way through angular 2 and attempting to define a service named searchservice. I want to inject this service in the bootstap part of my project: import {SearchService} from 'src/service'; Here is the code for the Se ...

Guide on importing JavaScript into TypeScript without using noImplicityAny and while not allowing Js

In my development setup, I am utilizing Webpack along with @babel/typescript to compile a project that consists of a mix of TypeScript and JavaScript. To ensure better typing and take advantage of the benefits it offers, I have enabled the --noImplicitAny ...

Can we handle optional properties that are contingent on a boolean in the type?

My current scenario involves a server response containing a boolean indicating success and optional data or error information. type ServerResponse = { success: boolean; data?: { [key: string]: string }; err?: { code: number, message: string }; } Dea ...

Global variable's value cannot be accessed outside the function in which it was inserted

I am facing an issue where I cannot access the value of var Login inside submit() function. This could possibly be due to a race condition. What could be causing this blockage in accessing the value of var Login? Here is the code snippet: class ABC { ...

You cannot utilize Lesson as a JSX Component in Next JS TypeScript

Below is my updated page.tsx code: import Aspects from '@/components/Aspects'; import FreeForm from '@/components/FreeForm'; import Lesson from '@/components/Lesson'; import React from 'react'; import { Route, Route ...