vue.js创建引入组件_用Vue.js创建的简单聊天组件

vue.js创建引入组件

快速聊天 (vue-quick-chat)

This vue component is a simple chat that can be easily imported and used in your project.

该vue组件是一个简单的聊天程序,可以轻松导入并在项目中使用。

特征 (Features)

  • Custom style

    定制风格

  • Handle on type event and on message submit

    处理类型事件和消息提交

  • Chat with multiple participants

    与多位参与者聊天

安装方式 (Instalation)

yarn add vue-quick-chat

or with npm

或使用npm

npm install vue-quick-chat --save

用法 (Usage)

import { Chat } from 'vue-quick-chat'
export default {
  components: {
    Chat
  },
}
<template>
  <div>
      <Chat 
       :participants="participants"
       :myself="myself"
       :messages="messages"
       :onType="onType"
       :onMessageSubmit="onMessageSubmit"
       :chatTitle="chatTitle"
       :placeholder="placeholder"
       :colors="colors"
       :borderStyle="borderStyle"
       :hideCloseButton="hideCloseButton"
       :closeButtonIconSize="closeButtonIconSize"
       :submitIconSize="submitIconSize"/>
   </div>
</template>
import { Chat } from 'vue-quick-chat'
export default {
  components: {
    Chat
  },
  data(){
    return {
      participants: [
        {
          name: 'Arnaldo',
          id: 1
        },
        {
          name: 'José',
          id: 2
        }
      ],
      myself: {
        name: 'Matheus S.',
        id: 3
      },
      messages: [
        {
          content: 'received messages', 
          myself: false,
          participantId: 1,
          timestamp: { year: 2019, month: 3, day: 5, hour: 20, minute: 10, second: 3, millisecond: 123 }
        },
        {
          content: 'sent messages', 
          myself: true,
          participantId: 3,
          timestamp: { year: 2019, month: 4, day: 5, hour: 19, minute: 10, second: 3, millisecond:123 }
        },
        {
          content: 'other received messages', 
          myself: false,
          participantId: 2,
          timestamp: { year: 2019, month: 5, day: 5, hour: 10, minute: 10, second: 3, millisecond: 123 }
        }
      ],
      chatTitle: 'My chat title',
      placeholder: 'send your message',
      colors:{
        header:{
          bg: '#d30303',
          text: '#fff'
        },
        message:{
          myself: {
            bg: '#fff',
            text: '#bdb8b8'
          },
          others: {
            bg: '#fb4141',
            text: '#fff'
          }
        },
        submitIcon: '#b91010'
      },
      borderStyle: {
        topLeft: "10px",
        topRight: "10px",
        bottomLeft: "10px",
        bottomRight: "10px",
      },
      hideCloseButton: false,
      submitIconSize: "20px",
      closeButtonIconSize: "20px"
    }
  },
  methods: {
    onType: function (event){
      //here you can set any behavior
    },
    onMessageSubmit: function(message){
      //here you can set any behavior
    }
  }

组件道具 (Component Props)

nametyperequireddefaultdescription
participantsArraytrueAn array of participants. Each participant should be an Object with name and id
myselfObjecttrueObject of my participant. "myself" should be an Object with name and id
messagesArraytrueAn array of messages. Each message should be an Object with content, myself, participantId and timestamp
onTypeFunctionfalse() => falseEvent called when the user is typing
onMessageSubmitFunctionfalse() => falseEvent called when the user sends a new message
chatTitleStringfalseEmpty StringThe title on chat header
placeholderStringfalse'type your message here'The placeholder of message text input
colorsObjecttrueObject with the color's description of style properties
borderStyleObjectfalse{ topLeft: "10px", topRight: "10px", bottomLeft: "10px", bottomRight: "10px"}Object with the description of border style properties
hideCloseButtonBooleanfalsefalseIf true, the Close button will be hidden
submitIconSizeStringfalse"15px"The submit icon size in pixels.
closeButtonIconSizeStringfalse"15px"The close button icon size in pixels.
名称 类型 需要 默认 描述
参加者 数组 真正 一系列参与者。 每个参与者都应该是一个具有名称和ID的对象
目的 真正 我的参与者的对象。 “我自己”应该是具有名称和ID的对象
讯息 数组 真正 消息数组。 每条消息都应该是一个包含内容,本人,参与者ID和时间戳的对象
onType 功能 ()=>错误 用户键入时调用的事件
onMessageSubmit 功能 ()=>错误 用户发送新消息时调用的事件
chatTitle 空字符串 聊天标题上的标题
占位符 '在这里输入你的消息' 消息文本输入的占位符
颜色 目的 真正 具有颜色的样式属性描述的对象
borderStyle 目的 {topLeft:“ 10px”,topRight:“ 10px”,bottomLeft:“ 10px”,bottomRight:“ 10px”} 具有边框样式属性描述的对象
hideCloseButton 布尔型 如果为true,则“关闭”按钮将被隐藏
SubmitIconSize “ 15px” 提交图标大小(以像素为单位)。
closeButtonIconSize “ 15px” 关闭按钮图标大小(以像素为单位)。

参加者 (participant)

nametypedescription
idintThe user id should be an unique value
nameStringThe user name that will be displayed
名称 类型 描述
ID 整型 用户标识应为唯一值
名称 将显示的用户名

Example

{
  name:  'Username',
  id: 1
},

信息 (message)

nametypedescription
contentStringThe message text content
myselfbooleanWether the message was sent by myself participant or by other participant
participantIdintThe participant's id who sent the message
timestampObjectObject describing the year, month, day, hour, minute, second and millisecond that the message was sent
名称 类型 描述
内容 消息文字内容
布尔值 消息是由我自己的参与者还是其他参与者发送的
参与者编号 整型 发送消息的参与者的ID
时间戳记 目的 描述消息发送的年,月,日,时,分,秒和毫秒的对象

Example

{
  content: 'received messages', 
  myself: false,
  participantId: 1,
  timestamp: { year: 2019, month: 3, day: 5, hour: 20, minute: 10, second: 3, millisecond: 123 }
}

颜色 (color)

nametypedescription
headerObjectObject containing the header background and text color
messageObjectObject containing the message background and text color. The Object should contains the style for 'myself' and 'others'
submitIconStringThe color applied to the send message button icon
名称 类型 描述
标头 目的 包含标题背景和文本颜色的对象
信息 目的 包含消息背景和文本颜色的对象。 对象应包含“我自己”和“其他”的样式
SubmitIcon 应用于发送消息按钮图标的颜色

Example

{
  header:{
    bg: '#d30303',
    text: '#fff'
  },
  message:{
    myself: {
      bg: '#fff',
      text: '#bdb8b8'
    },
    others: {
      bg: '#fb4141',
      text: '#fff'
    }
  },
  submitIcon: '#b91010'
}

项目设置 (Project setup)

npm install

编译和热重装以进行开发 (Compiles and hot-reloads for development)

npm run serve

编译并最小化生产 (Compiles and minifies for production)

npm run build

运行测试 (Run your tests)

npm run test

整理和修复文件 (Lints and fixes files)

npm run lint

翻译自: https://vuejsexamples.com/a-simple-chat-component-created-with-vue-js/

vue.js创建引入组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值