Vue 3 CDN

Step 1: Create an HTML file

Create an HTML file and add the necessary structure and tags. This file will serve as the entry point for your Vue application. You can name it whatever you like, but for this example, let's call it "index.html".

Step 2: Include the Vue 3 CDN

In the head section of your HTML file, include the Vue 3 CDN by adding the following script tag:

<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>

This script tag will fetch the Vue 3 library from the provided URL and make it available for use in your application.

Step 3: Create a Vue instance

In the body section of your HTML file, create a new Vue instance. This can be done by adding a script tag with the following code:

<script>
  const app = Vue.createApp({
    // Your Vue app configuration goes here
  })

  app.mount('#app')
</script>

The createApp() method is used to create a new Vue instance, and the configuration object passed to it is where you define your Vue app's behavior.

The mount() method is used to mount your Vue app to a specific element in your HTML. In this example, we're mounting it to an element with the id "app". Make sure you have an element with that id in your HTML file.

Step 4: Add Vue app configuration

Inside the configuration object passed to createApp(), you can define various options to configure your Vue app. Some common options include:

  • data: This option is used to define the initial data for your app.
  • methods: This option is used to define methods that can be called within your app.
  • computed: This option is used to define computed properties that depend on the app's data.
  • template: This option is used to define the HTML template for your app.

Here's an example of a basic Vue app configuration:

<script>
  const app = Vue.createApp({
    data() {
      return {
        message: 'Hello, Vue!'
      }
    },
    methods: {
      showMessage() {
        alert(this.message)
      }
    },
    template: `
      <div>
        <h1>{{ message }}</h1>
        <button @click="showMessage">Show Message</button>
      </div>
    `
  })

  app.mount('#app')
</script>

In this example, we have defined an initial data property message with the value "Hello, Vue!". We also have a method showMessage() that displays the value of message when a button is clicked. The HTML template uses double curly braces {{}} to bind the value of message and the @click directive to bind the showMessage() method to the button's click event.

Step 5: Test your Vue app

Save your HTML file and open it in a web browser. You should see the text "Hello, Vue!" and a button. Clicking the button should trigger an alert with the message "Hello, Vue!".

Congratulations! You have successfully set up a basic Vue 3 application using the CDN. You can now continue building your app by adding more components, using Vue directives, and exploring the many features and capabilities of Vue 3.