Contents
- Adding message
- Edit messages
- Scroll to bottom
Having trouble getting help?
Contact Support
Contact Support
Methods in Vue Chat UI component
18 Dec 202418 minutes to read
Adding message
You can use the addMessage public method to add the messages in the Chat UI. You can add it either as a string or MessageModel collection. It programmatically adds a new message to the chat.
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="addMessageModel" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Add Message as Object</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete."></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today."></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script setup>
import { ChatUIComponent as EjsChatui, MessagesDirective as EMessages, MessageDirective as EMessage } from "@syncfusion/ej2-vue-interactive-chat";
let chatInstance = ref(null);
const currentUser = {
id: "user1",
user: "Albert"
};
const michaleUser = {
id: "user2",
user: "Michale Suyama"
};
const buttonClick = () => {
chatInstance.value.addMessage(
{
author: michaleUser,
text: "Great! Let me know if there’s anything that needs adjustment."
});
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="addMessageModel" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Add Message as Object</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete."></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today."></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script>
import { ChatUIComponent, MessagesDirective, MessageDirective } from "@syncfusion/ej2-vue-interactive-chat";
export default {
components: {
'ejs-chatui': ChatUIComponent,
'e-messages': MessagesDirective,
'e-message': MessageDirective
},
data() {
return {
currentUser: {
id: "user1",
user: "Albert",
},
michaleUser: {
id: "user2",
user: "Michale Suyama",
}
};
},
methods: {
buttonClick: function () {
let defaultChat = this.$refs.chatInstance.ej2Instances;
defaultChat.addMessage(
{
author: this.michaleUser,
text: "Great! Let me know if there’s anything that needs adjustment."
});
}
}
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="addMessageString" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Add Message as string</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete."></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today."></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script setup>
import { ChatUIComponent as EjsChatui, MessagesDirective as EMessages, MessageDirective as EMessage } from "@syncfusion/ej2-vue-interactive-chat";
let chatInstance = ref(null);
const currentUser = {
id: "user1",
user: "Albert"
};
const michaleUser = {
id: "user2",
user: "Michale Suyama"
};
const buttonClick = () => {
chatInstance.value.addMessage('Also, let me know if there are any blockers we should address before the next phase.');
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="addMessageString" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Add Message as string</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete."></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today."></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script>
import { ChatUIComponent, MessagesDirective, MessageDirective } from "@syncfusion/ej2-vue-interactive-chat";
export default {
components: {
'ejs-chatui': ChatUIComponent,
'e-messages': MessagesDirective,
'e-message': MessageDirective
},
data() {
return {
currentUser: {
id: "user1",
user: "Albert",
},
michaleUser: {
id: "user2",
user: "Michale Suyama",
}
};
},
methods: {
buttonClick: function () {
let defaultChat = this.$refs.chatInstance.ej2Instances;
defaultChat.addMessage('Also, let me know if there are any blockers we should address before the next phase.');
}
}
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
Edit messages
You can use the updateMessage public method to update the messages in the ChatUI to modify an existing message within the chat, useful for editing or correcting sent messages.
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="updateMessage" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Update Message</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?" id="msg1"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete." id="msg2"></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today." id="msg3"></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script setup>
import { ChatUIComponent as EjsChatui, MessagesDirective as EMessages, MessageDirective as EMessage } from "@syncfusion/ej2-vue-interactive-chat";
let chatInstance = ref(null);
const currentUser = {
id: "user1",
user: "Albert"
};
const michaleUser = {
id: "user2",
user: "Michale Suyama"
};
const buttonClick = () => {
chatInstance.value.updateMessage({text: "Hi Michael, are we still on schedule to meet the deadline?", author: currentUser},'msg1');
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="updateMessage" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Update Message</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Hi Michale, are we on track for the deadline?" id="msg1"></e-message>
<e-message :author="michaleUser" text="Yes, the design phase is complete." id="msg2"></e-message>
<e-message :author="currentUser" text="I’ll review it and send feedback by today." id="msg3"></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script>
import { ChatUIComponent, MessagesDirective, MessageDirective } from "@syncfusion/ej2-vue-interactive-chat";
export default {
components: {
'ejs-chatui': ChatUIComponent,
'e-messages': MessagesDirective,
'e-message': MessageDirective
},
data() {
return {
currentUser: {
id: "user1",
user: "Albert",
},
michaleUser: {
id: "user2",
user: "Michale Suyama",
}
};
},
methods: {
buttonClick: function () {
let defaultChat = this.$refs.chatInstance.ej2Instances;
defaultChat.updateMessage({text: "Hi Michael, are we still on schedule to meet the deadline?", author: this.currentUser},'msg1');
}
}
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
Scroll to bottom
You can use the scrollToBottom public method to scroll the chat view to the latest message, ensuring users see the new content updated.
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="scrollToBottom" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Scroll To Bottom</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Want to get coffee tomorrow?"></e-message>
<e-message :author="michaleUser" text="Sure! What time?"></e-message>
<e-message :author="currentUser" text="How about 10 AM?"></e-message>
<e-message :author="michaleUser" text="Perfect"></e-message>
<e-message :author="currentUser" text="See you!"></e-message>
<e-message :author="michaleUser" text="Bye!"></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script setup>
import { ChatUIComponent as EjsChatui, MessagesDirective as EMessages, MessageDirective as EMessage } from "@syncfusion/ej2-vue-interactive-chat";
let chatInstance = ref(null);
const currentUser = {
id: "user1",
user: "Albert"
};
const michaleUser = {
id: "user2",
user: "Michale Suyama"
};
const buttonClick = () => {
chatInstance.value.scrollToBottom();
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>
<template>
<div id='container' style="height: 380px; width: 450px; margin: 0 auto;">
<button id="scrollToBottom" style="margin-bottom: 10px;" @click="buttonClick" class="e-btn e-primary">Scroll To Bottom</button>
<ejs-chatui height="360px" ref="chatInstance" :user="currentUser">
<e-messages>
<e-message :author="currentUser" text="Want to get coffee tomorrow?"></e-message>
<e-message :author="michaleUser" text="Sure! What time?"></e-message>
<e-message :author="currentUser" text="How about 10 AM?"></e-message>
<e-message :author="michaleUser" text="Perfect"></e-message>
<e-message :author="currentUser" text="See you!"></e-message>
<e-message :author="michaleUser" text="Bye!"></e-message>
</e-messages>
</ejs-chatui>
</div>
</template>
<script>
import { ChatUIComponent, MessagesDirective, MessageDirective } from "@syncfusion/ej2-vue-interactive-chat";
export default {
components: {
'ejs-chatui': ChatUIComponent,
'e-messages': MessagesDirective,
'e-message': MessageDirective
},
data() {
return {
currentUser: {
id: "user1",
user: "Albert",
},
michaleUser: {
id: "user2",
user: "Michale Suyama",
}
};
},
methods: {
buttonClick: function () {
let defaultChat = this.$refs.chatInstance.ej2Instances;
defaultChat.scrollToBottom();
}
}
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-interactive-chat/styles/material.css";
</style>