Tips for showcasing information from a composable to a Vue.js component

Having an issue with Vuejs 3 composables where I am trying to create a single composable for three inputs - email type and text type. I'm unable to display the email typed in the button component template even though I have imported the composable hook js file in the same way as for the input components. I am struggling to figure out how to display the input data and also how to send the collected input data to the parent component for form submission, as I am using the composable component itself. Any help would be appreciated.

useInputComposable

import { ref } from 'vue'

export function useInput() {
 const input = ref('')
 let passed = ref('')
 
 function updateInput(newInput) {
   input.value = newInput
 }
 return [ input, updateInput ]
}

Parent component

<template>
  <TextInput />
  <TextInput />
  <EmailInput />
  <Buttoncomponent />
</template>

Text type input

<script setup>
import { useInput } from '@/composables/useInput.js'
const [input, updateInput] = useInput()
</script>

<template>
  <div>
    <label for="input">{{ input }}</label>
    <input
      id="input"
      type="text"
      :value="input"
      @input="updateInput($event.target.value)"
    />
  </div>
</template>

Email type input

<script setup>
import { useInput } from '@/composables/useInput.js'
const [input, updateInput] = useInput()
</script>

<template>
  <div class="input">
    <input
      id="input"
      type="email"
      :value="input"
      @input="updateEmailInput($event.target.value)"
    />
  </div>
</template>

Button component

<script setup>
import { useInput } from '@/composables/useInput.js'
const [ input ] = useInput()
</script>

<template>
  <button>{{ input }} place the email here</button> <!-- the input is empty here ('') -->
</template>

Answer №1

You can simplify your code by switching from using a composable to v-model for two-way data binding between child components and the parent component. This approach is more suitable for your current needs.

Parent Component

<script setup>
import TextInput from './TextInput.vue';
import EmailInput from './EmailInput.vue';
import ButtonComponent from './ButtonComponent.vue';
import { ref } from 'vue'

const email = ref('')
const input1 = ref('')
const input2 = ref('')
</script>

<template>
  <TextInput v-model="input1" />
  <TextInput v-model="input2" />
  <EmailInput v-model="email" />
  <ButtonComponent :email="email" />
</template>

TextInput

<script setup>
defineProps(['modelValue'])
defineEmits(['update:modelValue'])
</script>

<template>
  <div>
    <label for="input">{{ modelValue }}</label>
    <input
      id="input"
      type="text"
      :value="modelValue"
      @input="$emit('update:modelValue', $event.target.value)"
    />
  </div>
</template>

EmailInput

<script setup>
defineProps(['modelValue'])
defineEmits(['update:modelValue'])
</script>

<template>
  <div class="input">
    <input
      id="input"
      type="email"
      :value="modelValue"
      @input="$emit('update:modelValue', $event.target.value)"
    />
  </div>
</template>

ButtonComponent

<script setup>
defineProps(['email'])
</script>

<template>
  <button>{{ email }}</button>
</template>

Vue Playground example

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

Should a React application perform a complete refresh when a file is reloaded?

Recently, I delved into the world of React and learned about one of its key benefits: updating only the necessary DOM elements. However, as I began building an app from scratch, I encountered a situation where every time I saved the .js file, it resulted ...

Uploading a three.js canvas to the server without saving it as a file

Currently, I am in the process of saving an image to the server using a three.js script that I have created. The script looks like the following: actualCode(THREE); function actualCode(THREE) { //Rendering variables const renderer = new THREE.WebG ...

Utilizing JFlex for efficient brace matching

Currently, I am in the process of developing an IntelliJ plugin. One of the key features that I am working on is a brace matcher. After completing the JetBrains plugin tutorial, I was able to get the brace matcher functioning using this regular expression ...

React Component for Toggling a Click Event on a Select Tag

I'm currently developing a react app where clicking on a specific text triggers the display of a select tag with several items. However, I've encountered two issues: Upon clicking the text, the select tag appears on ALL list items instead of ju ...

Instructions on accessing the subsequent li a starting from a designated location

My goal is to change the link color of the button with the id #idIMMUNOLOGY_9, which has the class .active_default, but I must start from the element with the id #idTERRITORIAL_8. I attempted this approach : $('#idTERRITORIAL_8').parent().find( ...

Adjust the Pivot Point of a GLTF Model in ThreeJS Manually

Hey there, I have a GLTF model that I successfully loaded into my ThreeJS scene by using the code snippet below: gltfLoader.load('assets/models/coin/scene.gltf', (gltf) => { const root = gltf.scene; gltf.scene.traverse(functio ...

Switching themes in Vue-Material (Error: Property 'material' is not recognized in type 'Vue')

Currently, I have a working copy to create and load themes in vue-typescript versus vue-material. Here is an example of the SCSS code: @import "~vue-material/dist/theme/engine"; // Import the theme engine @include md-register-theme("defaul ...

Utilizing MongoDB for data transfers: uploading and downloading

I successfully configured my localhost to upload files to Mongo DB using server.js //I have configured my ID and PW const mongoURI = "mongodb+srv://myID:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cfdbf2f5e2c1ced7d1 ...

Vue.js Applications tend to encounter Expected Errors originating from JavaScript

I've been diving into learning Vue.js using Visual Studio 2017. Currently, my goal is to create applications with multiple buttons that trigger the display of a message upon being clicked. However, when I attempt to run this code, I encounter the foll ...

Retrieving specific data from nested arrays in Vue.js

I am currently working on Vue.js template development and facing an issue with filtering nested data in a faq section. When I try to add a search bar, the nested data array returns all data without proper filtering. Dom <v-container> <v ...

Instance of a Component Available Worldwide

When working with Vue 2.x in our production applications, we utilize a toast component. This toast component is mounted once through a plugin (code provided below) and then added to the Vue prototype for easy access in every component instance. This setup ...

Importing an array of Vue components to be exported and utilized in the app.js file

I'm currently working on a Laravel 8 project with vue.js v2.6 and I want to clean up my app.js file by moving all of my Vue.component() declarations to a separate file. To achieve this, I created js/vueComponents.js where I placed all the vue componen ...

Ensure that input field is validated only upon clicking the save button in Angular framework

Can anyone suggest the most efficient method to validate required fields in an input form using AngularJS? I want to display an error message only after the user clicks the save button (submit). Thank you in advance. ...

A class with Three.js

I attempted to consolidate all the necessary functionalities into a single class to create a straightforward three.js scene with a cube. Despite not encountering any errors, the scene remains black when viewed in the browser. Here is the code I've wri ...

Can a TypeScript-typed wrapper for localStorage be created to handle mapped return values effectively?

Is it feasible to create a TypeScript wrapper for localStorage with a schema that outlines all the possible values stored in localStorage? Specifically, I am struggling to define the return type so that it corresponds to the appropriate type specified in t ...

Utilize fancybox to target and display related content through external links within tabs

My website features a stylish box with tabbed inline content, allowing users to navigate between different views by clicking on tab names. However, I want the tabbed content to be accessible when a user clicks on a navigation link to launch the box. For ex ...

When using Express.js for file uploading, it is important to first verify that a file has been sent, set a maximum file size limit, and ensure

After working with expressjs for a month, I've encountered some issues with file uploads. Despite researching on Google and various blogs, I haven't been able to find answers to the following three questions: What do I need to do or what setting ...

The jQuery window.on event is not functioning when trying to handle the same hash change

How to fix jQuery window.on hashchange issue when clicking on the same hash? See the code snippet below for a potential solution: function addMargin() { let header = $('.header__wrapper').outerHeight(); let headerHeight = $('body& ...

Issue with timebox filtering in customapp.js for Rally SDK 2

I am interested in creating a timebox filtered app and found an example at However, when attempting to run this on a custom HTML page, I encountered the following error: Failed to load resource: the server responded with a status of 404 (Not Found) on th ...

Exploring Interactive Designs with Vue.js

In order to dynamically construct a series of CSS style classes based on the toolName property in the JSON data using Vue 2, I tried to use a computed property to bind them to the existing span with a class of panel-icon. However, when attempting to save t ...