Vue.js can be installed and used in different ways. Below are the two common methods for installing Vue.js:
1. CDN (Content Delivery Network):
If you want to quickly include Vue.js in your project without any build setup, you can use the CDN version. Include the following script tag in the HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js CDN Example</title>
</head>
<body>
<div id="app">
{{ message }}
</div>
<!-- Include Vue.js from CDN -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello, Vue!'
}
});
</script>
</body>
</html>
2. NPM (Node Package Manager):
For larger projects or when you want to take advantage of a more sophisticated development environment, you can use npm. This method assumes you have Node.js installed.
Install Vue.js:
Open your terminal or command prompt and navigate to your project directory. Then, run the following command to install Vue.js:
bashCopy code
npm install vue
Create a Vue Instance:
Once installed, you can create a Vue instance in your JavaScript file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js NPM Example</title>
</head>
<body>
<div id="app">
{{ message }}
</div>
<!-- Include your JavaScript file after Vue is installed -->
<script src="path/to/your/app.js"></script>
</body>
</html>
In your app.js
:
// Import Vue
import Vue from 'vue';
// Create a Vue instance
new Vue({
el: '#app',
data: {
message: 'Hello, Vue!'
}
});
Remember to replace 'path/to/your/app.js'
with the actual path to your JavaScript file.
Comments