Various content implementation methods of vue chat box

WBOY
Release: 2023-05-11 11:51:37
Original
1288 people have browsed it

Vue is a popular JavaScript framework for creating modern web applications. Chat boxes are a common component in many web applications. This article will introduce how to use Vue to implement multiple content types of chat boxes.

  1. Text messages

Text messages are the most common type of chat content. To implement a text message chat box using Vue, first create a chat room component. You can then allow the user to enter a message by adding a text input box, like this:

<template>
  <div>
    <div v-for="(message, index) in messages" :key="index">
      {{ message.text }}
    </div>
    <input type="text" v-model="newMessage" @keyup.enter="sendMessage">
  </div>
</template>

<script>
export default {
  data() {
    return {
      messages: [],
      newMessage: ''
    }
  },
  methods: {
    sendMessage() {
      this.messages.push({
        text: this.newMessage,
        type: 'text'
      })
      this.newMessage = ''
    }
  }
}
</script>
Copy after login

In the above code, we include text (text) and type (type) for each message object. When the user presses the Enter key, we add the new message to the message array and clear the contents of the input box.

  1. Picture Message

To implement the picture message chat box, you need to add a picture upload button and picture preview function to the text input box.

<template>
  <div>
    <div v-for="(message, index) in messages" :key="index">
      <template v-if="message.type === 'text'">
        {{ message.text }}
      </template>
      <template v-else-if="message.type === 'image'">
        <img :src="message.url">
      </template>
    </div>
    <input type="text" v-model="newMessage.text" @keyup.enter="sendMessage">
    <input type="file" ref="fileInput" @change="previewImage">
    <button @click="sendImage">发送图片</button>
    <img v-if="imageUrl" :src="imageUrl">
  </div>
</template>

<script>
export default {
  data() {
    return {
      messages: [],
      newMessage: {
        type: 'text',
        text: ''
      },
      imageUrl: ''
    }
  },
  methods: {
    previewImage() {
      const file = this.$refs.fileInput.files[0]
      const reader = new FileReader()
      reader.onload = event => {
        this.imageUrl = event.target.result
      }
      reader.readAsDataURL(file)
    },
    sendImage() {
      this.messages.push({
        type: 'image',
        url: this.imageUrl
      })
      this.imageUrl = ''
    }
  }
}
</script>
Copy after login

In the above code, we enable the image upload function through the <input type="file"> element, and use the FileReader object to render the uploaded image Thumbnail.

  1. Audio Message

To implement the audio message chat box, you need to create a player component, which can be simplified using the Vue-Audio plug-in.

<template>
  <div>
    <div v-for="(message, index) in messages" :key="index">
      <template v-if="message.type === 'text'">
        {{ message.text }}
      </template>
      <template v-else-if="message.type === 'image'">
        <img :src="message.url">
      </template>
      <template v-else-if="message.type === 'audio'">
        <vue-audio :src="message.url"/>
      </template>
    </div>
    <input type="text" v-model="newMessage.text" @keyup.enter="sendMessage">
    <input type="file" ref="fileInput" accept="audio/*" @change="previewAudio">
    <button @click="sendAudio">发送音频</button>
  </div>
</template>

<script>
import VueAudio from 'vue-audio'

export default {
  components: {
    VueAudio
  },
  data() {
    return {
      messages: [],
      newMessage: {
        type: 'text',
        text: ''
      },
      audioUrl: ''
    }
  },
  methods: {
    previewAudio() {
      const file = this.$refs.fileInput.files[0]
      const reader = new FileReader()
      reader.onload = event => {
        this.audioUrl = event.target.result
      }
      reader.readAsDataURL(file)
    },
    sendAudio() {
      this.messages.push({
        type: 'audio',
        url: this.audioUrl
      })
      this.audioUrl = ''
    }
  }
}
</script>
Copy after login

In the above code, we use the Vue-Audio component to play the uploaded audio file. We set messages of type audio inside the component so that audio messages are rendered correctly in the message list. When uploading audio files, we can restrict users to only upload audio files by setting the accept attribute of the <input> element to audio/*.

  1. Video Message

Implementing a video message chat box requires a similar approach to audio messaging. Likewise, we will create a component to play the video.

<template>
  <div>
    <div v-for="(message, index) in messages" :key="index">
      <template v-if="message.type === 'text'">
        {{ message.text }}
      </template>
      <template v-else-if="message.type === 'image'">
        <img :src="message.url">
      </template>
      <template v-else-if="message.type === 'audio'">
        <vue-audio :src="message.url"/>
      </template>
      <template v-else-if="message.type === 'video'">
        <video :src="message.url" controls></video>
      </template>
    </div>
    <input type="text" v-model="newMessage.text" @keyup.enter="sendMessage">
    <input type="file" ref="fileInput" accept="video/*" @change="previewVideo">
    <button @click="sendVideo">发送视频</button>
  </div>
</template>

<script>
import VueAudio from 'vue-audio'

export default {
  components: {
    VueAudio
  },
  data() {
    return {
      messages: [],
      newMessage: {
        type: 'text',
        text: ''
      },
      videoUrl: ''
    }
  },
  methods: {
    previewVideo() {
      const file = this.$refs.fileInput.files[0]
      const reader = new FileReader()
      reader.onload = event => {
        this.videoUrl = event.target.result
      }
      reader.readAsDataURL(file)
    },
    sendVideo() {
      this.messages.push({
        type: 'video',
        url: this.videoUrl
      })
      this.videoUrl = ''
    }
  }
}
</script>
Copy after login

In the above code, we use the <video> element to render the uploaded video file. We also set a video type message inside the component so that the video message is rendered correctly. Likewise, we also limit the file type that users can upload to videos.

Conclusion

This article introduces how to use Vue to implement multiple chat content types. Using Vue components and their lifecycle hooks, computed properties, and event handlers, we can easily implement various functions of the chat box. During practice, you can modify and customize it according to your needs to meet your project requirements.

The above is the detailed content of Various content implementation methods of vue chat box. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!