0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue3 での textareaの高さを自動で設定する方法

Last updated at Posted at 2021-04-09

結論

Vue
 props: {
        text: String,
        id: String,
    },
    data() {
        return {
            txt: this.text,
            i: this.id,
        }
    },
computed:{
        rows() {
            if(this.txt === null) {
                return 4
            }
            else{
                let num = (this.txt.match(/\n/g) || []).length + 1
                return (num > 4) ? num : 4
            }
        }
    }
HTML
<textarea class="editable-contents-textarea" v-on:blur="saveText" v-model="txt" :id="'textarea'+ i" :rows="rows"></textarea>
css
.editable-contents-textarea{
  height:auto;
}

補足

rows関数の4って言う数字は適当です.強いて言うなら最低の行数ですね

あと,if(this.txt === null)の分岐なんですけど,これはtexareaに文字がない場合のものです.

以上です

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?