Note: how to transform a div into an input box and register it as a vue component

Original address: https://blog.csdn.net/qq_34629352/article/details/81708024
How to transform DIV into input box in chat application!
Effect realization:

This component was written by a big guy. It works well. It's ready to use,
The component code is as follows:

<template>
    <div class="edit-div"
         v-html="innerText"
         :contenteditable="canEdit"
         @focus="isLocked = true"
         @blur="isLocked = false"
         @input="changeText">
    </div>
</template>
<script>
    export default{
        name: 'editDiv',
        props: {
            value: {
                type: String,
                default: ''
            },
            canEdit: {
                type: Boolean,
                default: true
            }
        },
        data(){
            return {
                innerText: this.value,
                isLocked: false
            }
        },
        watch: {
            'value'(){
                if (!this.isLocked || !this.innerText) {
                    this.innerText = this.value;
                }
            }
        },
        methods: {
            changeText(){
                this.$emit('input', this.$el.innerHTML);
            }
        }
    }
</script>
<style>
    .edit-div {
        width: 100%;
        height: 100%;
        overflow: auto;
        word-break: break-all;
        outline: none;
        user-select: text;
        white-space: pre-wrap;
        text-align: left;
        &[contenteditable=true]{
            user-modify: read-write-plaintext-only;
            &:empty:before {
                content: attr(placeholder);
                display: block;
                color: #ccc;
            }
        }
    }
</style>

Here, you can save it to any location in the project. The name is named by yourself. Next, when you reference it, you will bring the user-defined Name:

import editor from '@/components/view/editor'

Then again

components:{editor},

Finally, use components in html:

 <editor  id="editor-" v-model="input_content" class="editor-" ></editor>

Thanks to Bao Lei for this article:
https://blog.csdn.net/qq_34629352/article/details/81708024
https://blog.csdn.net/qq_34629352

Posted on Fri, 08 Nov 2019 15:06:12 -0500 by New Coder