前提
Vue2(Options API)の参考書に載っているアプリケーションを参考にしながら, Vue3の<script setup>で書き換えを行っています。
実現したいこと
Element-Plusを使用し, el-form, el-inputにてv-modelでリアクティブな入力ができるようにしたいと考えています。
発生している問題・エラーメッセージ
el-inputに, キーボード入力を行っても, 入力欄に何も表示されない。
エラーは特に発生していません trackingできない...
該当のソースコード
main.js
1import { createApp, provide, toDisplayString } from 'vue' 2import './style.css' 3import ElementPlus from 'element-plus' 4import 'element-plus/dist/index.css' 5import App from './App.vue' 6import router from "./router/router"; 7import store from "./store/store"; 8 9const app = createApp(App) 10app.use(store).use(router).use(ElementPlus).mount('#app') 11 12app.config.globalProperties.$http = (url, opts) => fetch(url, opts) 13 14app.provide('myFetch', app.config.globalProperties.$http) 15
BookForm.vue 本の読了記録を登録するFORMを生成
1<template> 2 <div id="form"> 3 // BookInfo...選択した本の詳細を表示 4 <BookInfo :book="book"></BookInfo> 5 <hr /> 6 <el-form ref="form" :model="form" :rules="rules" label-width="120px"> 7 <el-form-item label="読了日"> 8 <el-date-picker type="date" v-model="form.read"></el-date-picker> 9 </el-form-item> 10 <el-form-item label="感想" prop="memo"> 11 <el-input type="textarea" size="large" v-model="form.memo"></el-input> 12 </el-form-item> 13 <el-form-item> 14 <el-button type="primary" @click="onsubmit">登録</el-button> 15 </el-form-item> 16 </el-form> 17 </div> 18</template> 19 20<script setup> 21import { useStore } from "vuex"; 22import { ref, reactive, onMounted, inject } from "vue"; 23import BookInfo from '../components/BookInfo.vue' 24import { UPDATE_CURRENT, UPDATE_BOOK } from '../mutation-types' 25import router from "../router/router"; 26 27const store = useStore() 28// book...選択している本の情報を保持 29// form...el-formにて使用 30let book = ref({}) 31let form = reactive({ 32 read: new Date(), 33 memo: "" 34}) 35let valid = ref(true) 36 37const rules = reactive({ 38 memo: [{ require: true, message: 'メモが未入力です', trigger: 'change' }] 39}) 40 41// 選択している本が無ければ, HOME画面を表示 42if ( !store.getters.current ) { 43 router.push("/") 44} else { 45 book.value = Object.assign({}, store.getters.current) 46} 47 48onMounted( () => { 49 // すでに記録が登録されている場合, その記録をFormに反映 50 let b = store.getters.getBookById(book.value.id) 51 if (b) { 52 form.read = b.read 53 form.memo = b.memo 54 } 55}) 56 57const onsubmit = () => { 58// validは, テスト用に設けた変数なので意味は無し 59 if (valid) { 60 store.dispatch(UPDATE_BOOK, Object.assign({}, book.value, form)) 61 } 62 store.dispatch(UPDATE_CURRENT, null) 63 64 form.read = new Date() 65 form.memo = "" 66 router.push("/") 67} 68 69/* 以下は, 参考書に記載されているVue2(Options API)で書かれたもの 70export default { 71 name: 'book-form', 72 components: { 73 BookInfo 74 }, 75 data() { 76 return { 77 book: {}, 78 form: { 79 read: new Date(), 80 memo: '' 81 }, 82 rules: { 83 memo: [ 84 { required: true, message: 'メモが未入力です。', trigger: 'blur' } 85 ] 86 }, 87 rules: { 88 memo: [{ 89 validator: (rule, value, callback) => { 90 console.log(rule); 91 if (!value) { 92 return callback(new Error('メモが未入力です。')) 93 } 94 }, 95 trigger: 'change' 96 }] 97 } 98 } 99 }, 100 computed: mapGetters([ 'current', 'getBookById' ]), 101 created() { 102 if (!this.current) { 103 this.$router.push('/') 104 } 105 this.book = Object.assign({}, this.current) 106 }, 107 mounted() { 108 let b = this.getBookById(this.book.id) 109 if (b) { 110 this.form.read = b.read 111 this.form.memo = b.memo 112 } 113 }, 114 methods: { 115 ...mapActions([UPDATE_BOOK, UPDATE_CURRENT]), 116 onsubmit() { 117 this.$refs['form'].validate((valid) => { 118 if (valid) { 119 this[UPDATE_BOOK]( 120 Object.assign({}, this.book, this.form) 121 ) 122 this[UPDATE_CURRENT](null) 123 this.$notify({ 124 title: 'Reading Recorder', 125 message: this.$createElement('p', { style: 'color: #009' }, 126 '読書情報の登録/更新に成功しました。'), 127 duration: 2000 128 }) 129 this.form.read = new Date() 130 this.form.memo = '' 131 this.$router.push('/') 132 } 133 }) 134 } 135 } 136} 137*/ 138</script> 139 140<style scoped> 141#form { 142 margin-top: 15px; 143} 144</style> 145
試したこと
・Vueのデヴェロッパーツールを見ましたが, <el-input type="textarea">には, 一文字だけが認識されているみたいでした。
・refやreactiveにたいする参照の方法が間違っているかと思いましたが, 合っているようです。
・特段エラー文が出ていないので, 構文的エラーはないと思うのですが, onMountedなどの使い方が間違っているのかもしれません...
補足情報(FW/ツールのバージョンなど)
"node": v16.17.0
"npm": v8.15.0
"element-plus": "^2.2.16"
"vue": "^3.2.37"
"vue-router": "^4.1.5"
"vuex": "^4.0.2"
"vuex-persistedstate": "^4.1.0"

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。