Default Values Not Displaying in Custom Vue 3 Input Component

I’m using a custom Vue 3 input component, TheInput2, in a project, but the default values are not displaying. The streamData object contains the necessary data, but the input fields remain empty.

Here’s the TheInput2 (custom input) component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><template>
<div class="the-input-wrapper">
<label v-if="label" :for="genId">
<span>{{ label }}</span>
</label>
<div class="input-container" :class="[ verification && 'input-container-verification' ]">
<slot name="before"></slot>
<input :id="genId"
v-maska
ref="inputElement"
v-model="inputModel"
:class="[ errorMessage && 'error', icon && 'ml-[30px]' ]"
:type="inputTypeResult"
:disabled="disabled"
:placeholder="placeholder"
@maska="setInputValue"
v-bind="{
...(mask && { 'data-maska': mask }),
...(name && { name })
}"
/>
<component v-if="icon" :is="icon"/>
<div v-if="type === 'password'" class="password-toggle">
<component :is="isShowPassword ? PasswordShowedIcon : PasswordHiddenIcon"
@click="isShowPassword = !isShowPassword" />
</div>
</div>
<div v-if="errorMessage" class="errors mb-2">{{ $t(errorMessage) || errorMessage }}</div>
<div v-else class="hint"><slot name="hint"></slot></div>
</div>
</template>
<script setup lang="ts">
import { defineProps, defineEmits, computed, ref, onMounted, watch } from 'vue'
import lodash from 'lodash'
import { vMaska, MaskaDetail } from "maska"
import { useField } from 'vee-validate';
import PasswordHiddenIcon from '@/assets/icons/eye-slash.svg?component'
import PasswordShowedIcon from '@/assets/icons/eye.svg?component'
const isShowPassword = ref<boolean>(false)
const genId = ref<string>('input-' + lodash.random(1, 100000))
const inputElement = ref<HTMLInputElement>('')
const props = defineProps<{
value: string | number
label?: string
disabled?: boolean
placeholder?: string
mask?: string
unmaskedValue?: boolean
type?: 'email' | 'password' | 'string' | 'number'
name: string
verification?: boolean
icon?: object
}>()
const emits = defineEmits(['success-mask', 'update:value'])
const { value: validateValueModel, errorMessage, resetField } = useField(() => props.name)
const inputTypeResult = computed<'email' | 'password' | 'text'>(() => {
if (props.type === 'password') return isShowPassword.value ? 'text' : 'password'
return props.type || 'text'
})
const inputModel = computed({
get: () => props.value,
set: (newValue: string | number) => {
emits('update:value', newValue)
validateValueModel.value = newValue
}
})
const setInputValue = (maskEvent: CustomEvent<MaskaDetail>) => {
inputModel.value = props.unmaskedValue || !props.mask ? maskEvent.detail.unmasked : maskEvent.detail.masked
if (maskEvent.detail.completed) emits('success-mask')
}
watch(
() => inputModel.value,
(newValue) => {
if (!newValue) {
inputElement.value.value = props.mask ? ' ' : ''
resetField()
}
}
)
onMounted(() => {
inputElement.value.value = props.mask ? ' ' + inputModel.value : inputModel.value
})
</script>
<style scoped lang="scss">
.input-container {
/* styles... */
}
.input-container-verification {
/* styles... */
}
@keyframes pulse-border {
/* styles... */
}
.the-input-wrapper {
/* styles... */
}
</style>
</code>
<code><template> <div class="the-input-wrapper"> <label v-if="label" :for="genId"> <span>{{ label }}</span> </label> <div class="input-container" :class="[ verification && 'input-container-verification' ]"> <slot name="before"></slot> <input :id="genId" v-maska ref="inputElement" v-model="inputModel" :class="[ errorMessage && 'error', icon && 'ml-[30px]' ]" :type="inputTypeResult" :disabled="disabled" :placeholder="placeholder" @maska="setInputValue" v-bind="{ ...(mask && { 'data-maska': mask }), ...(name && { name }) }" /> <component v-if="icon" :is="icon"/> <div v-if="type === 'password'" class="password-toggle"> <component :is="isShowPassword ? PasswordShowedIcon : PasswordHiddenIcon" @click="isShowPassword = !isShowPassword" /> </div> </div> <div v-if="errorMessage" class="errors mb-2">{{ $t(errorMessage) || errorMessage }}</div> <div v-else class="hint"><slot name="hint"></slot></div> </div> </template> <script setup lang="ts"> import { defineProps, defineEmits, computed, ref, onMounted, watch } from 'vue' import lodash from 'lodash' import { vMaska, MaskaDetail } from "maska" import { useField } from 'vee-validate'; import PasswordHiddenIcon from '@/assets/icons/eye-slash.svg?component' import PasswordShowedIcon from '@/assets/icons/eye.svg?component' const isShowPassword = ref<boolean>(false) const genId = ref<string>('input-' + lodash.random(1, 100000)) const inputElement = ref<HTMLInputElement>('') const props = defineProps<{ value: string | number label?: string disabled?: boolean placeholder?: string mask?: string unmaskedValue?: boolean type?: 'email' | 'password' | 'string' | 'number' name: string verification?: boolean icon?: object }>() const emits = defineEmits(['success-mask', 'update:value']) const { value: validateValueModel, errorMessage, resetField } = useField(() => props.name) const inputTypeResult = computed<'email' | 'password' | 'text'>(() => { if (props.type === 'password') return isShowPassword.value ? 'text' : 'password' return props.type || 'text' }) const inputModel = computed({ get: () => props.value, set: (newValue: string | number) => { emits('update:value', newValue) validateValueModel.value = newValue } }) const setInputValue = (maskEvent: CustomEvent<MaskaDetail>) => { inputModel.value = props.unmaskedValue || !props.mask ? maskEvent.detail.unmasked : maskEvent.detail.masked if (maskEvent.detail.completed) emits('success-mask') } watch( () => inputModel.value, (newValue) => { if (!newValue) { inputElement.value.value = props.mask ? ' ' : '' resetField() } } ) onMounted(() => { inputElement.value.value = props.mask ? ' ' + inputModel.value : inputModel.value }) </script> <style scoped lang="scss"> .input-container { /* styles... */ } .input-container-verification { /* styles... */ } @keyframes pulse-border { /* styles... */ } .the-input-wrapper { /* styles... */ } </style> </code>
<template>
  <div class="the-input-wrapper">
    <label v-if="label" :for="genId">
      <span>{{ label }}</span>
    </label>
    <div class="input-container" :class="[ verification && 'input-container-verification' ]">
      <slot name="before"></slot>
      <input :id="genId"
             v-maska
             ref="inputElement"
             v-model="inputModel"
             :class="[ errorMessage && 'error', icon && 'ml-[30px]' ]"
             :type="inputTypeResult"
             :disabled="disabled"
             :placeholder="placeholder"
             @maska="setInputValue"
             v-bind="{
             ...(mask && { 'data-maska': mask }),
             ...(name && { name })
           }"
      />
      <component v-if="icon" :is="icon"/>
      <div v-if="type === 'password'" class="password-toggle">
        <component :is="isShowPassword ? PasswordShowedIcon : PasswordHiddenIcon"
                   @click="isShowPassword = !isShowPassword" />
      </div>
    </div>
    <div v-if="errorMessage" class="errors mb-2">{{ $t(errorMessage) || errorMessage }}</div>
    <div v-else class="hint"><slot name="hint"></slot></div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits, computed, ref, onMounted, watch } from 'vue'
import lodash from 'lodash'
import { vMaska, MaskaDetail } from "maska"
import { useField } from 'vee-validate';
import PasswordHiddenIcon from '@/assets/icons/eye-slash.svg?component'
import PasswordShowedIcon from '@/assets/icons/eye.svg?component'

const isShowPassword = ref<boolean>(false)
const genId = ref<string>('input-' + lodash.random(1, 100000))
const inputElement = ref<HTMLInputElement>('')

const props = defineProps<{
  value: string | number
  label?: string
  disabled?: boolean
  placeholder?: string
  mask?: string
  unmaskedValue?: boolean
  type?: 'email' | 'password' | 'string' | 'number'
  name: string
  verification?: boolean
  icon?: object
}>()

const emits = defineEmits(['success-mask', 'update:value'])

const { value: validateValueModel, errorMessage, resetField } = useField(() => props.name)

const inputTypeResult = computed<'email' | 'password' | 'text'>(() => {
  if (props.type === 'password') return isShowPassword.value ? 'text' : 'password'
  return props.type || 'text'
})

const inputModel = computed({
  get: () => props.value,
  set: (newValue: string | number) => {
    emits('update:value', newValue)
    validateValueModel.value = newValue
  }
})

const setInputValue = (maskEvent: CustomEvent<MaskaDetail>) => {
  inputModel.value = props.unmaskedValue || !props.mask ? maskEvent.detail.unmasked : maskEvent.detail.masked
  if (maskEvent.detail.completed) emits('success-mask')
}

watch(
  () => inputModel.value,
  (newValue) => {
    if (!newValue) {
      inputElement.value.value = props.mask ? ' ' : ''
      resetField()
    }
  }
)

onMounted(() => {
  inputElement.value.value = props.mask ? ' ' + inputModel.value : inputModel.value
})
</script>

<style scoped lang="scss">
.input-container {
  /* styles... */
}
.input-container-verification {
  /* styles... */
}
@keyframes pulse-border {
  /* styles... */
}
.the-input-wrapper {
  /* styles... */
}
</style>

And here is the OwnerStreamForm component where I use TheInput2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><template>
<div class="owner-stream-form-container">
<form @submit.prevent="handleSubmit" class="owner-stream-form">
<div class="form-group">
<div class="label-container">
<label for="streamTitle">{{ $t('form_labels.stream_title') }}</label>
<p class="field-description">{{ $t('form_descriptions.stream_title') }}</p>
</div>
<TheInput2
id="streamTitle"
v-model:value="streamData.title"
required
/>
</div>
<!-- other form groups -->
</form>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import TheButton from '@/components/settings/TheButton.vue';
import TheInput2 from '@/components/ui/form/TheInput2/index.vue';
import TheTextarea2 from '@/components/ui/form/TheTextarea2/index.vue';
import { useRoute } from 'vue-router';
import { useWowStream } from '@/composables/useWowStream';
const props = defineProps({
profile: {
type: Object,
required: true
}
});
const route = useRoute();
const { saveStreamData, isLoadingSubmitForm } = useWowStream();
const streamData = ref({
title: '',
description: '',
link: '',
lobbyNumber: '',
lobbyPassword: '',
prize: ''
});
const handleSubmit = async () => {
await saveStreamData(route.params.id, streamData.value);
};
onMounted(() => {
const stream = props.profile.wow_streamer?.wow_streams[0];
if (stream) {
streamData.value = {
title: stream.name,
description: stream.description,
link: stream.link,
lobbyNumber: stream.data.lobby_number,
lobbyPassword: stream.data.password,
prize: stream.data.prize
};
}
});
</script>
<style scoped lang="scss">
.owner-stream-form-container {
/* styles... */
}
.owner-stream-form {
/* styles... */
}
.stream-preview {
/* styles... */
}
</style>
</code>
<code><template> <div class="owner-stream-form-container"> <form @submit.prevent="handleSubmit" class="owner-stream-form"> <div class="form-group"> <div class="label-container"> <label for="streamTitle">{{ $t('form_labels.stream_title') }}</label> <p class="field-description">{{ $t('form_descriptions.stream_title') }}</p> </div> <TheInput2 id="streamTitle" v-model:value="streamData.title" required /> </div> <!-- other form groups --> </form> </div> </template> <script setup lang="ts"> import { ref, onMounted, computed } from 'vue'; import TheButton from '@/components/settings/TheButton.vue'; import TheInput2 from '@/components/ui/form/TheInput2/index.vue'; import TheTextarea2 from '@/components/ui/form/TheTextarea2/index.vue'; import { useRoute } from 'vue-router'; import { useWowStream } from '@/composables/useWowStream'; const props = defineProps({ profile: { type: Object, required: true } }); const route = useRoute(); const { saveStreamData, isLoadingSubmitForm } = useWowStream(); const streamData = ref({ title: '', description: '', link: '', lobbyNumber: '', lobbyPassword: '', prize: '' }); const handleSubmit = async () => { await saveStreamData(route.params.id, streamData.value); }; onMounted(() => { const stream = props.profile.wow_streamer?.wow_streams[0]; if (stream) { streamData.value = { title: stream.name, description: stream.description, link: stream.link, lobbyNumber: stream.data.lobby_number, lobbyPassword: stream.data.password, prize: stream.data.prize }; } }); </script> <style scoped lang="scss"> .owner-stream-form-container { /* styles... */ } .owner-stream-form { /* styles... */ } .stream-preview { /* styles... */ } </style> </code>
<template>
  <div class="owner-stream-form-container">
    <form @submit.prevent="handleSubmit" class="owner-stream-form">
      <div class="form-group">
        <div class="label-container">
          <label for="streamTitle">{{ $t('form_labels.stream_title') }}</label>
          <p class="field-description">{{ $t('form_descriptions.stream_title') }}</p>
        </div>
        <TheInput2
          id="streamTitle"
          v-model:value="streamData.title"
          required
        />
      </div>
      <!-- other form groups -->
    </form>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import TheButton from '@/components/settings/TheButton.vue';
import TheInput2 from '@/components/ui/form/TheInput2/index.vue';
import TheTextarea2 from '@/components/ui/form/TheTextarea2/index.vue';
import { useRoute } from 'vue-router';
import { useWowStream } from '@/composables/useWowStream';

const props = defineProps({
  profile: {
    type: Object,
    required: true
  }
});

const route = useRoute();
const { saveStreamData, isLoadingSubmitForm } = useWowStream();

const streamData = ref({
  title: '',
  description: '',
  link: '',
  lobbyNumber: '',
  lobbyPassword: '',
  prize: ''
});

const handleSubmit = async () => {
  await saveStreamData(route.params.id, streamData.value);
};

onMounted(() => {
  const stream = props.profile.wow_streamer?.wow_streams[0];
  if (stream) {
    streamData.value = {
      title: stream.name,
      description: stream.description,
      link: stream.link,
      lobbyNumber: stream.data.lobby_number,
      lobbyPassword: stream.data.password,
      prize: stream.data.prize
    };
  }
});
</script>

<style scoped lang="scss">
.owner-stream-form-container {
  /* styles... */
}
.owner-stream-form {
  /* styles... */
}
.stream-preview {
  /* styles... */
}
</style>

streamData has data, but it does not display in TheInput2 component. What am I missing here?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật