Vue.js is an open-source JavaScript framework used for building user interfaces and single-page applications. It is easy to learn and use, making it an excellent choice for beginners who want to become front-end developers. In this tutorial, we will cover the basics of Vue.js and provide step-by-step instructions on how to get started with building web applications using this framework.
Step 1: Installing Vue.js
Before you can start using Vue.js, you need to install it on your system. There are several ways to do this, but the simplest method is to use the Vue CLI (Command Line Interface). To install the Vue CLI, open your terminal and run the following command:
npm install -g @vue/cli
This command will install the Vue CLI globally on your system, allowing you to create new Vue projects from the command line. Once the installation is complete, you can create a new Vue project by running the following command:
vue create my-app
Replace my-app
with the name of your project. The Vue CLI will then prompt you to select a preset for your project. For beginners, we recommend selecting the default preset, which includes all the basic features you need to get started with Vue.js.
Step 2: Creating Your First Vue Component
Now that you have created a new Vue project, it’s time to create your first Vue component. Components are the building blocks of Vue.js applications, allowing you to encapsulate reusable pieces of code and logic. To create a new Vue component, navigate to the src/components
directory in your project folder and create a new file called HelloWorld.vue
. You can do this using your favorite text editor or code editor.
In HelloWorld.vue
, write the following code:
<template>
<div>
<h1>Hello, World!</h1>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
h1 {
color: red;
}
</style>
In this code, we have defined a simple Vue component that displays the text "Hello, World!" in a red color. The <template>
section contains the HTML code for the component’s markup, the <script>
section contains the JavaScript code for the component’s logic, and the <style>
section contains the CSS code for the component’s styles.
Step 3: Registering Your Vue Component
To use your new Vue component in your application, you need to register it with the Vue instance. Open the App.vue
file in the src
directory of your project and add the following code to register the HelloWorld
component:
<template>
<div id="app">
<HelloWorld />
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
components: {
HelloWorld
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Now, when you run your Vue project using the npm run serve
command, you should see the text "Hello, World!" displayed on the browser window. Congratulations! You have successfully created and registered your first Vue component.
Step 4: Working with Data and Methods in Vue Components
One of the key features of Vue.js is its ability to work with data and methods in Vue components. You can define data properties in your components and use methods to manipulate this data. To demonstrate this, let’s modify the HelloWorld.vue
component to display a greeting message that changes based on a data property.
In HelloWorld.vue
, update the <script>
section to include a data
property and a method like this:
<script>
export default {
data() {
return {
greeting: 'Hello, World!'
}
},
methods: {
changeGreeting() {
this.greeting = 'Welcome to Vue.js!'
}
}
}
</script>
Next, update the <template>
section to display the greeting message and add a button that triggers the changeGreeting
method:
<template>
<div>
<h1>{{ greeting }}</h1>
<button @click="changeGreeting">Change Greeting</button>
</div>
</template>
Now, when you run your Vue project and click the "Change Greeting" button, the greeting message should update to "Welcome to Vue.js!". This demonstrates how you can work with data and methods in Vue components to create interactive and dynamic user interfaces.
Step 5: Routing in Vue.js
Another important aspect of building web applications is routing, which allows users to navigate between different pages or views within the application. Vue.js provides a built-in routing system called Vue Router that makes it easy to set up and manage routes in your application.
To add routing to your Vue project, install Vue Router by running the following command in your terminal:
npm install vue-router
Next, create a new file called router.js
in the src
directory of your project and add the following code to set up routes for your application:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: HelloWorld },
{ path: '/about', component: About }
]
const router = new VueRouter({
mode: 'history',
routes
})
export default router
In this code, we have defined two routes: one for the home page (/
) and one for an about page (/about
). Replace HelloWorld
and About
with the components you want to display for each route.
Finally, import the router in the main.js
file of your project and add it to the Vue instance like this:
import Vue from 'vue'
import App from './App.vue'
import router from './router'
new Vue({
router,
render: h => h(App),
}).$mount('#app')
Now, when you navigate to the home page (http://localhost:8080/
) and the about page (http://localhost:8080/about
), you should see the respective components displayed for each route. Congratulations! You have successfully added routing to your Vue project.
Conclusion
In this tutorial, we covered the basics of Vue.js and demonstrated how to create Vue components, work with data and methods, and add routing to a Vue project. By following these steps and experimenting with different features of Vue.js, you can become a proficient front-end developer and build responsive and interactive web applications. Vue.js is a powerful framework that is easy to learn and use, making it a great choice for beginners looking to advance their skills in front-end development.
Thanks for your effort.
If this teaches me Vue, i will love your channel forever
Awesome tutorial. But I can not understand which key you press for shortcuts. For example at time: 26:36. You make them all Pages children. How did you do? Can you explain it. Thank you
loved it
If we make pagesStore reactive(), then some events can be removed
Man I cannot thank you enough. This is the best Vue tutorial on YT! Your explanations and extensive commentary on reasoning and syntax breakdown is just gold. Really appreciate your effort!
New achievement unlocked: 🎉 The first long YouTube tutorial that I actually completed and didn't leave halfway through.
Thank you very much
Done watching, new subscriber here👋. I'm looking forward for vue + laravel tutorial.
what is theme ? Looks very nice.
1:21:30 chap3
One of the few courses on youtube that I have actually followed completely till the end. Thanks a lot🙇
may I know what vscode theme that you're using ?
This is an absolute treasure! Thank you very much!
This is kind of a noob question but can you please tell what font you're using on your vs code? It looks cool
but why is 2 nav bars generated at 1:48:41. Pls help
Found this precisely when I needed it. 30 minutes in and I know I’ll stay there until the end. Thanks so much
25:05
55:00
1:32:18
for my later use
Very good tutorial. Can you share the code?
Hi! Absolute NOOB here..
– got everything up and running
– my Index.html code is exactly the same as far as I can tell
– got stuck at around 9:30 of the video, where my page doesnt show the softcoded pageTitle and content..
– All I get is {{ pageTitle }} and {{ content }}, so it doesnt get the info from the Vue.createApp script.
Any pointers on what I've might have missed?
EDIT: Nevermind… fixed it.. was missing one ">" in my code..