📜  vue 3 - Javascript (1)

📅  最后修改于: 2023-12-03 14:48:22.535000             🧑  作者: Mango

Vue 3 - JavaScript

Vue is a progressive framework for building user interfaces. It is written in JavaScript and designed to be incrementally adoptable. Vue 3 is the latest version of the framework, with significant updates and improvements over the previous version.

Installation

To use Vue 3 in your project, you can install it with npm or yarn. Here's an example of how to install it with npm:

npm install vue@next
Getting Started

To get started with Vue 3, you'll need to create a new Vue instance. You can do this by importing the Vue module and calling the createApp method:

import { createApp } from 'vue';

const app = createApp({
  // options
});

app.mount('#app');

In the above example, we create a new Vue instance with an empty options object, then mount it to an element with the ID app. You can replace the empty object with your own configuration options for your application.

Template Syntax

Vue's template syntax is inspired by Angular's syntax, and allows you to declaratively render your UI. Here's an example of a basic template:

<template>
  <div>
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
  </div>
</template>

In this template, we define a title and message variable in our Vue instance, and then display them using the double curly brace syntax.

Directives

Vue 3 includes a number of built-in directives that you can use to manipulate your UI. Some of these directives include v-if, v-for, v-bind, and v-on. Here's an example of using a directive to render a list of items:

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

In this example, we use the v-for directive to loop through the items array in our Vue instance and render a list item for each one. We also use the :key directive to provide a unique key for each item, which is a best practice for performance optimization.

Component Composition

Vue 3 allows you to create reusable components for your UI. Here's an example of defining a custom component:

import { defineComponent } from 'vue';

export default defineComponent({
  name: 'MyComponent',
  props: {
    message: {
      type: String,
      required: true,
    },
  },
  template: `
    <div>
      <p>{{ message }}</p>
    </div>
  `,
});

In this example, we use the defineComponent method to define a new component named MyComponent. We define a props object to allow our component to receive data from its parent, and a template string to define the component's UI. We can then use this component in other templates like this:

<template>
  <div>
    <my-component :message="'Hello, world!'"></my-component>
  </div>
</template>
Conclusion

Vue 3 is a powerful and flexible framework for building user interfaces with JavaScript. It includes a rich set of features and a simple API, making it easy to learn and use in your projects. If you're looking for a modern and performant front-end framework, Vue 3 is definitely worth considering.