当前位置:   article > 正文

Vue2聊天组件vue-beautiful-chat_vue聊天组件

vue聊天组件

1.安装

npm i vue-beautiful-chat

2.main.js中引入

  1. import Chat from 'vue-beautiful-chat'
  2. Vue.use(Chat)

 3.对接接口的完整代码(复制过去就可以用)

  1. <template>
  2. <div>
  3. <beautiful-chat
  4. :participants="participants"
  5. :titleImageUrl="titleImageUrl"
  6. :onMessageWasSent="onMessageWasSent"
  7. :messageList="messageList"
  8. :newMessagesCount="newMessagesCount"
  9. :isOpen="isChatOpen"
  10. :close="closeChat"
  11. :icons="icons"
  12. :open="openChat"
  13. :showEmoji="true"
  14. :showFile="true"
  15. :showEdition="true"
  16. :showDeletion="true"
  17. :showTypingIndicator="showTypingIndicator"
  18. :showLauncher="true"
  19. :showCloseButton="true"
  20. :colors="colors"
  21. :alwaysScrollToBottom="alwaysScrollToBottom"
  22. :messageStyling="messageStyling"
  23. @onType="handleOnType"
  24. @edit="editMessage"
  25. />
  26. </div>
  27. </template>
  28. <script>
  29. import CloseIcon from "@/assets/logo.png";
  30. import OpenIcon from "@/assets/logo.png";
  31. import FileIcon from "@/assets/logo.png";
  32. import CloseIconSvg from "@/assets/logo.png";
  33. import io from "socket.io-client";
  34. export default {
  35. name: "app",
  36. data() {
  37. return {
  38. // 创建一个 socket 对象
  39. socket: null,
  40. icons: {
  41. open: {
  42. img: OpenIcon,
  43. name: "default",
  44. },
  45. close: {
  46. img: CloseIcon,
  47. name: "default",
  48. },
  49. file: {
  50. img: FileIcon,
  51. name: "default",
  52. },
  53. closeSvg: {
  54. img: CloseIconSvg,
  55. name: "default",
  56. },
  57. },
  58. participants: [
  59. {
  60. id: "user1",
  61. name: "Matteo",
  62. imageUrl:
  63. "https://avatars3.githubusercontent.com/u/1915989?s=230&v=4",
  64. },
  65. {
  66. id: "user2",
  67. name: "Support",
  68. imageUrl:
  69. "https://avatars3.githubusercontent.com/u/37018832?s=200&v=4",
  70. },
  71. ], // the list of all the participant of the conversation. `name` is the user name, `id` is used to establish the author of a message, `imageUrl` is supposed to be the user avatar.
  72. titleImageUrl:
  73. "https://a.slack-edge.com/66f9/img/avatars-teams/ava_0001-34.png",
  74. messageList: [
  75. { type: "text", author: `me`, data: { text: `Say yes!` } },
  76. { type: "text", author: `user1`, data: { text: `No.` } },
  77. ], // the list of the messages to show, can be paginated and adjusted dynamically
  78. newMessagesCount: 0,
  79. isChatOpen: false, // to determine whether the chat window should be open or closed
  80. showTypingIndicator: "", // when set to a value matching the participant.id it shows the typing indicator for the specific user
  81. colors: {
  82. header: {
  83. bg: "#4e8cff",
  84. text: "#ffffff",
  85. },
  86. launcher: {
  87. bg: "#4e8cff",
  88. },
  89. messageList: {
  90. bg: "#ffffff",
  91. },
  92. sentMessage: {
  93. bg: "#4e8cff",
  94. text: "#ffffff",
  95. },
  96. receivedMessage: {
  97. bg: "#eaeaea",
  98. text: "#222222",
  99. },
  100. userInput: {
  101. bg: "#f4f7f9",
  102. text: "#565867",
  103. },
  104. }, // specifies the color scheme for the component
  105. alwaysScrollToBottom: false, // when set to true always scrolls the chat to the bottom when new events are in (new message, user starts typing...)
  106. messageStyling: true, // enables *bold* /emph/ _underline_ and such (more info at github.com/mattezza/msgdown)
  107. };
  108. },
  109. created() {
  110. // 建立服务器的连接
  111. console.log("连接");
  112. this.socket = io("http://toutiao.itheima.net", {
  113. query: {
  114. token: "8d6b045d-a15f-4d4a-9841-340ddf27a283",
  115. },
  116. transports: ["websocket"],
  117. });
  118. console.log("socket", this.socket);
  119. // 收消息
  120. this.socket.on("message", (data) => {
  121. console.log("收到消息", data);
  122. // 将内容添加到聊天列表中
  123. const item = {
  124. type: "text",
  125. author: `user1`,
  126. data: { text: `${data.msg}` },
  127. };
  128. this.messageList.push(item);
  129. // this.chatList.push({
  130. // isme: false,
  131. // t: data.msg,
  132. // });
  133. // 自动滚动到底部
  134. // this.$nextTick(() => {
  135. // // 滚动到最后面
  136. // this.$refs.chat.scrollTop = this.$refs.chat.scrollHeight;
  137. // });
  138. });
  139. },
  140. methods: {
  141. sendMessage(text) {
  142. if (text.length > 0) {
  143. this.newMessagesCount = this.isChatOpen
  144. ? this.newMessagesCount
  145. : this.newMessagesCount + 1;
  146. this.onMessageWasSent({
  147. author: "support",
  148. type: "text",
  149. data: { text },
  150. });
  151. }
  152. },
  153. // 发送消息
  154. onMessageWasSent(message) {
  155. // called when the user sends a message
  156. console.log("限制性这个", message);
  157. this.messageList = [...this.messageList, message];
  158. // 将消息发送到服务器
  159. this.socket.emit("message", {
  160. msg: message.data.text,
  161. timestamp: Date.now(),
  162. });
  163. console.log("发送到服务器");
  164. },
  165. openChat() {
  166. // called when the user clicks on the fab button to open the chat
  167. this.isChatOpen = true;
  168. this.newMessagesCount = 0;
  169. },
  170. closeChat() {
  171. // called when the user clicks on the botton to close the chat
  172. this.isChatOpen = false;
  173. },
  174. handleScrollToTop() {
  175. // called when the user scrolls message list to top
  176. // leverage pagination for loading another page of messages
  177. },
  178. handleOnType() {
  179. console.log("Emit typing event");
  180. },
  181. editMessage(message) {
  182. const m = this.messageList.find((m) => m.id === message.id);
  183. m.isEdited = true;
  184. m.data.text = message.data.text;
  185. },
  186. },
  187. };
  188. </script>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/900297
推荐阅读
相关标签
  

闽ICP备14008679号