Vue.js is an open-source, front-end JavaScript framework created by Evan You in 2014. Unlike other JavaScript frameworks like Angular and React, Vue is designed to be incrementally adoptable. This means you can use it for small parts of your project or scale it up to build large-scale applications.
v-if
, v-for
, v-bind
, v-model
, and more, to make the development process easier.To start using Vue.js in your project, follow these simple steps:
The Vue CLI helps you quickly create and manage Vue.js projects.
npm install -g @vue/cli
After the Vue CLI is installed, you can create a new Vue project:
This command will prompt you to choose a preset for your project. For now, you can choose the default settings.
Navigate into your project folder and start the development server:
cd my-vue-project
npm run serve
When you create a new Vue project, the default project structure looks like this:
my-vue-project/
├── node_modules/
├── public/
│ ├── index.html
├── src/
│ ├── assets/
│ ├── components/
│ ├── App.vue
│ ├── main.js
├── package.json
├── vue.config.js
public/index.html
: The main HTML file of your application.src/main.js
: The entry point for your Vue app, where Vue is initialized.src/App.vue
: The root component of your Vue application.src/components/
: Folder where all your Vue components reside.Vue applications are built using components. A component is a reusable building block that encapsulates HTML, JavaScript, and CSS.
src/components/
directory, create a new file called HelloWorld.vue
.<template>
<div class="hello">
<h1>{{ message }}</h1>
<p>Welcome to Vue.js!</p>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue.js!'
};
}
};
</script>
<style scoped>
h1 {
color: #42b983;
}
</style>
To use this component, you need to import it into App.vue
and include it in the template.
<template>
<div id="app">
<HelloWorld />
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
name: 'App',
components: {
HelloWorld
}
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
}
</style>
Vue.js provides special attributes called directives to make it easier to manipulate the DOM and interact with data.
v-bind
: Binds an attribute to an expression.v-if
: Conditionally renders elements.v-for
: Loops through an array and renders elements.v-model
: Creates two-way data binding for form inputs.v-if
and v-for
:<template>
<div>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
<button v-if="showButton" @click="toggleButton">Toggle Button</button>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, name: 'Vue.js' },
{ id: 2, name: 'React.js' },
{ id: 3, name: 'Angular.js' }
],
showButton: true
};
},
methods: {
toggleButton() {
this.showButton = !this.showButton;
}
}
};
</script>
In the example above:
v-for
is used to loop through an array of items
and render each item as a list element.v-if
conditionally renders a button depending on the value of showButton
.One of the core features of Vue.js is two-way data binding, which means the UI and the data are automatically synchronized. This is done using the v-model
directive.
v-model
:<template>
<div>
<input v-model="inputText" placeholder="Type something">
<p>You typed: {{ inputText }}</p>
</div>
</template>
<script>
export default {
data() {
return {
inputText: ''
};
}
};
</script>