In this tutorial, you will build a chat app using HTML CSS Vue js. In this article, a guide to creating a simple interface for chatting with your friends is given. Vue js is a JavaScript framework that allows you to build fast and efficient single-page applications. Vue was created by Evan You in 2011, and it has become one of the most popular frameworks today. In this tutorial, you will learn how to create a chat app using Vue js with HTML CSS js, and the vuetifyjs library.
First, you will have to create a folder for your project. Let’s call it a chat app and save it in the root of your project folder.
Next, you will add an index.html file which will be used as the template for all pages in this app. Add this file at the end of your directory (the last one). It should look like this:
“`html {
“`body {
“`div id=”chatbox” vue=true type=’container’ style=’font-weight:bold;px#f0c8d9′ font-size:1rem; color:#fff}“
The first step is to import Vue from ‘vue’.
You will create a new instance of the chat component by using the Vue instance you created in the previous step. You also need to create an instance of your new HtmlCssStyleSheet class so that you can configure its properties later on:
// Create an object that contains all necessary information for creating an HtmlCssStyleSheet object const styleSheets = {};
// Creates an object containing two properties: `from` and `to`.
It returns this value when called with no arguments or when passed as a second argument
const htmlcssStylesheet = new HtmlCssStyleSheet({ from: null, to: null });
// This function sets up default stylesheets based on whether they have been specified explicitly earlier or not (i.e., if there are no stylesheets specified then it assumes them being present). If there were any errors during execution then these would be reported here as well,
const notifyAll = function(error) { if (error instanceof Error) { throw error; } };
// Update your export object with new values… export default { name: “chat”, components: [], template : ` Message `
Add an input field for the user to type their messages.
Create a textarea, and add the name attribute to it. Set its value to your message:
Next, add an event listener for click events on this browser’s keyboard:$(document).on(“keydown”, function(event){});
Now you need to create a method for sending messages. You can find the Axios library in the Vue project, or you can install it separately through npm:
“`javascript
npm install –save axios@3.0.x
“`
“`javascript
Vue.use(axio)
import axios from ‘axios’;
// 1st line below imports `axios`. If you don’t have this installed, please follow step 1 above!
var source = new Axios({ baseURL : process.env[‘SEND_MESSAGE_API_URL’] });
// 2nd line imports `source`, which is your App instance (created in Step 2).
var message = {};
// 3rd line creates an empty object called `message`.
This will be used later when rendering UI components as well as being passed as input values during AJAX requests made by other scripts such as Vue Router! mutateData(newData);
sendMessage();
return promise;
// 4th line makes sure you return something from your callback function after making calls into external APIs using promises
Now that you have the app up and it’s running, let’s make it more interactive.
You can now receive messages from your friends by pressing on their name in the chat list. You can also send them a message by typing it in there too!
Now that you have a simple chat app, it’s time to add support for multiple users. The first step is to use the Vue.js v-for directive to iterate over an array of users and update their names in the view:
{{#each users as |userName|}}
{{ userName }}
You may also want this list of users to display differently depending on which user is viewing it—for example, you could show all of them or just those who’ve been active for more than a youek. To do this, you’ll use two new directives: v-on and v-model.
Now that you have a route, you can use the vue-router to show the list of online users.
To do this, import the router in main.js:
import Vue from ‘vue’
import Router from ‘vue-router’
import App from ‘./App’ new Vue({ el: ‘#app’,
components: { App } }) .$mount(‘/’) .$on(‘$routeChangeStart’,
function() { const currentState = getCurrentState();
if (this.$route)
{ if (!currentState[currentRoute]) { this.$store.dispatch(new Event(‘userSignUp’)); }
else if (currentState[currentRoute]) { setTimeout(function() { checkIfUserStillOnline(), }, 5000); }
else {} }); });
Now create a method for showing online users using router’s pushState property which will save us from manual rerendering whenever there is change in user state!
In this step, you will combine the Vue JS code with the HTML code.
The code for this is as follows:
new Vue({ el: ‘#app’, data:{ message: ‘Hello World!’, },
methods:{ // Add a button to be pressed onClick(e){ alert(‘You clicked on me!’); } } });
Inline CSS or CSS files are the easiest way to create styles for your app. There are many ways to use them, but you’ll go over two common approaches:
The code for the chat app is available here. You can use it to create your own chat app, or even share the code with others who want to make a similar app. If you’re interested in doing so, Contact us to get a build of a Vuejs-based chat room!
You’ll notice that we’ve used some of the same features as before:
We hope that this tutorial has helped you understand the basics of creating a chat app with Vue.js and HTML5. If you have any questions or want to get your own custom application, please feel free to contact us!
published: December 8, 2022
© Copyright 2023 Cognitionteam All Rights Reserved