You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic Vue3组合式API中v-model无法更新值的问题排查

问题原因及修复方案

错误1:ion-label属性拼写错误

你模板里的ion-label的position属性写成了flaoting,正确拼写是floating。这个拼写错误会导致Ionic的表单标签无法正确关联输入框,进而破坏v-model的双向绑定逻辑,输入的内容无法同步到响应式数据中。

错误2:未导入所需的Ionic组件

在Vue 3 + Ionic项目中,必须显式导入用到的UI组件(或全局注册),否则组件无法正常初始化,数据绑定也会失效。


修正后的完整代码

<template>
    <form class="ion-padding" @submit.prevent="submitForm">
      <ion-list>
        <ion-item>
          <ion-label position="floating">Title</ion-label>
          <ion-input type="text" v-model="enteredTitle" />
        </ion-item>
        <ion-item>
          <ion-label position="floating">Image URL</ion-label>
          <ion-input type="url" required v-model="enteredImageUrl" />
        </ion-item>
        <ion-item>
          <ion-label position="floating">Description</ion-label>
          <ion-textarea rows="5" v-model="enteredDescription"></ion-textarea>
        </ion-item>
      </ion-list>
      <ion-button type="submit" expand="full">Save</ion-button>
    </form>
  </template>
  
  <script lang="ts">
  import { reactive, toRefs } from 'vue';
  // 导入所需的Ionic组件
  import { IonList, IonItem, IonLabel, IonInput, IonTextarea, IonButton } from '@ionic/vue';

  export default {
      components: {
        IonList,
        IonItem,
        IonLabel,
        IonInput,
        IonTextarea,
        IonButton
      },
      setup() {
          const enteredMemoryData = reactive({ 
            enteredTitle: '',
            enteredImageUrl: '',
            enteredDescription: '',
          });
  
          function submitForm(){
              console.log('Form is getting submitted');
              const memoryData = {
                title: enteredMemoryData.enteredTitle,
                imageUrl: enteredMemoryData.enteredImageUrl,
                description: enteredMemoryData.enteredDescription,
              }
              console.log('Following data will be submitted', memoryData);
          }
          return {
              ...toRefs(enteredMemoryData),
              submitForm
          }
      },
  }
  </script>

修复这两个问题后,v-model的双向绑定就能正常工作,点击提交按钮时控制台会正确输出输入的内容。

内容的提问来源于stack exchange,提问作者Rotan075

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:45:17