Home Web Front-end Vue.js Introduction to the basics of VUE3 development: implementing basic functions

Introduction to the basics of VUE3 development: implementing basic functions

Jun 15, 2023 pm 09:10 PM
vue basic skills Development basics

In front-end development, VUE is undoubtedly a very popular framework. With the release of VUE3, more and more developers have begun to learn and use this new version of VUE. This article will introduce how to use VUE3 to develop a front-end application, and demonstrate how to implement some basic functions based on some simple examples.

  1. Preparing the environment

Before you begin, please make sure you have installed Node.js and npm. Then you can enter the following command on the command line interface to install VUE CLI:

npm install -g @vue/cli
Copy after login

After the installation is complete, you can use VUE CLI to create a new project. Enter the following command in the command line interface:

vue create my-project
Copy after login

where "my-project" is the name of your own project. Afterwards, you will be asked to select configuration options for VUE. You can choose default options or configure them manually. After the project is created, you can start using VUE3 for your own development.

  1. Create components

In VUE, components are an important concept. A typical component contains three parts: template, script and style. Template is the HTML content of the component, script is the logical code of the component, and style is the style sheet of the component. Next, let's create a simple component. First, create a file named "HelloWorld.vue" in the "src/components" directory. Add the following content to the file:

<template>
  <div>
    <h1>Hello World</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {}
  },
  methods: {},
  computed: {},
  created() {}
}
</script>

<style scoped>
h1 {
  font-size: 24px;
  color: #333;
}
</style>
Copy after login

Here, we define a component named "HelloWorld" and set an H1 tag as the content of the component. In addition, we also define some other options, including data, methods, computed and created.

  1. Using components

After creating the component, we need to use it in the application. Add the following content in the "src/App.vue" file:

<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;
}
</style>
Copy after login

Here, we reference the "HelloWorld" component in the component and register the component in "components". In the application, we only need to use the "" tag to reference the component. When we run the application, we will see the title "Hello World" displayed on the page.

  1. Event handler

In applications, we usually need to respond to user operations, such as clicking buttons, entering text, etc. At this time, we need to use event handlers. In VUE, event handlers can be implemented through the "v-on" directive. Next, let's create a simple button component. When the user clicks the button, an event will be triggered and a pop-up window will be displayed.

First, create a file named "Button.vue" in the "src/components" directory. Add the following content to the file:

<template>
  <button @click="showAlert">Click me!</button>
</template>

<script>
export default {
  name: 'Button',
  methods: {
    showAlert() {
      window.alert('Hello World!')
    }
  },
}
</script>

<style scoped>
button {
  background-color: #333;
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
}
</style>
Copy after login

Here, we have created a component called "Button" and defined a button. The "v-on:click" command is used on the button to bind a click event, and a "showAlert" method is called when clicked to display a pop-up window.

Finally, add the following content in the "src/App.vue" file:

<template>
  <div id="app">
    <Button />
  </div>
</template>

<script>
import Button from '@/components/Button.vue'

export default {
  components: {
    Button
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
}
</style>
Copy after login

Here, we reference the "Button" component in the application. When we run the application and click the button, we will see a pop-up window displayed.

  1. Computed properties

In applications, we sometimes need to calculate a new value based on one or more states. At this time, we can use calculated properties to deal with this problem. In VUE, computed properties can be implemented through the "computed" option. Next, let's create a computed property to calculate the square of a number.

First, create a file named "Number.vue" in the "src/components" directory. Add the following content to the file:

<template>
  <div>
    <label>Number: </label>
    <input type="number" v-model="number" />
    <p>Number squared: {{ numberSquared }}</p>
  </div>
</template>

<script>
export default {
  name: 'Number',
  data() {
    return {
      number: 0
    }
  },
  computed: {
    numberSquared() {
      return this.number * this.number
    }
  }
}
</script>

<style scoped>
label {
  font-weight: bold;
  margin-right: 10px;
}
input {
  padding: 5px;
  border-radius: 4px;
  border: 1px solid #ccc;
  width 100px;
  margin-right: 10px;
}
p {
  margin-top: 10px;
}
</style>
Copy after login

Here, we create a component named "Number" and define an input box for entering numbers. We also define a computed property "numberSquared" that is used to calculate the square of a number. When the number changes, the calculated property automatically updates the square of the number and displays it on the page.

Finally, add the following content in the "src/App.vue" file:

<template>
  <div id="app">
    <Number />
  </div>
</template>

<script>
import Number from '@/components/Number.vue'

export default {
  components: {
    Number
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
}
</style>
Copy after login

Here, we reference the "Number" component in the application and display a number input box and the square of that number.

Summary

In this article, we introduced how to use VUE3 to develop a front-end application and demonstrated how to create components, use event handlers and calculated properties and other basic functions. Through these examples, I believe you already have some basic understanding and skills. Of course, VUE3 still has many powerful functions and features waiting for developers to explore. I hope you can continue to learn and use VUE3, and continue to make progress and innovation in practice.

The above is the detailed content of Introduction to the basics of VUE3 development: implementing basic functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump a tag to vue How to jump a tag to vue Apr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles