実現したいこと
TextInput.vuewに初期値を渡して表示させたいのですが、表示されません。
前提
Laravel11+vue.jsです。
ここに質問の内容を詳しく書いてください。
前述の通り、単にTextInput.vueに初期値を与えて表示したいです。
発生している問題・エラーメッセージ
コンソールに以下のメッセージが出ています。 app.ts:18 [Vue warn]: Missing required prop: "modelValue" at <TextInput defineModel="初期値が入ります" > at <GuestLayout> at <ComponentTest errors= {} auth= {user: null} ziggy= {url: 'http://127.0.0.1:8000', port: 8000, defaults: Array(0), routes: {…}, location: 'http://127.0.0.1:8000/component-test'} ... > at <Inertia initialPage= {component: 'ComponentTest', props: {…}, url: '/component-test', version: '4bdca66a1356aeb97458fd51c170b614'} initialComponent= {__name: 'ComponentTest', __hmrId: 'e7bed9c1', __file: '/Users/yasuo/Herd/rose/resources/js/Pages/ComponentTest.vue', setup: ƒ, render: ƒ, …} resolveComponent=fn<r> ... > at <App>
該当のソースコード
web.php
1Route::get('/component-test', function () { 2 return Inertia::render('ComponentTest'); 3});
ComponentTest.vue
1<script setup> 2import GuestLayout from '@/Layouts/GuestLayout.vue' 3import Label from '@/Components/InputLabel.vue' 4import Input from '@/Components/TextInput.vue' 5</script> 6 7<template> 8<GuestLayout> 9 <Label value="件名">タイトル</Label> 10 <Input defineModel="初期値が入ります"></Input> 11</GuestLayout> 12</template>
TextInput.vlue
1<script setup lang="ts"> 2import { onMounted, ref } from 'vue' 3 4const model = defineModel<string>({ required: true }); 5 6const input = ref<HTMLInputElement | null>(null); 7 8onMounted(() => { 9 if (input.value?.hasAttribute('autofocus')) { 10 input.value?.focus(); 11 } 12}); 13 14defineExpose({ focus: () => input.value?.focus() }); 15 16</script> 17 18<template> 19 <input 20 class="border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm" 21 v-model="model" 22 ref="input" 23 /> 24</template>
試したこと
chatGPTで聞いてみましたが、要領を得ませんでした。
ちなみに、TextInput.vueは初期状態で、'vue'のところと、defineModelと、defineExposeにVSCode上で赤波線がついています。
補足情報(FW/ツールのバージョンなど)
特にありません。
何卒、ご回答のほどよろしくお願いいたします。
defineModel の使い方勘違いしていませんか?
https://ja.vuejs.org/api/sfc-script-setup#definemodel
`const model = defineModel<string>({ required: true });` で宣言される props は modelValue です。
v-modle="" で bind するものです。
コメントありがとうございます。教えて頂いた通り試したところ問題が解決しました!
ベストアンサーに選びたいので同じ内容を回答欄に投稿いただけますでしょうか?
咀嚼して待ってます。
マニュアルには「それ以外の場合、props 名はデフォルトで "modelValue" になります。」
ということは、そういうもので、それを知らない人はマニュアルを見なさい。見ない人には分かりませんってことでしょうか?
コードを見て分からないのは手間がかかりますね・・・。
マニュアルを見るクセをつけるとします。
解決おめでとうございます!
解決方法をまとめて、「自己解決」として回答してください!
同じように困っている人の貴重な情報になると思います。
マニュアルを読まなくても 一応 defineModel のソースにはその記述はありますよ……?
https://github.com/vuejs/core/blob/c9c9dff805e346a88b02a9c5edb386242e49b8f1/packages/runtime-core/src/apiSetupHelpers.ts#L226-L258
おはようございます。
この注釈部分なんですかね;・・。
* // default model (consumed via `v-model`)
* const modelValue = defineModel<string>()
* modelValue.value = "hello"
*
* // default model with options
* const modelValue = defineModel<string>({ required: true })
ここまでたどり着くのは私の身の丈にあってないようです。
こういうものもあるんだなと覚えておいて、いずれつかえるようになったら嬉しいです。
ともかく、コメントありがとうございました。
回答1件
あなたの回答
tips
プレビュー