Home  >  Article  >  Web Front-end  >  How to build vue

How to build vue

王林
王林Original
2023-05-24 09:04:07758browse

Vue is a popular front-end framework that provides a wealth of tools and resources to help developers build single-page applications. In this article, we'll cover the basics of building and using Vue.

Vue installation and configuration:

  1. Before starting the Vue build, you need to install Node.js on your local machine. It provides a simple command line tool to manage various development dependencies and tasks.
  2. Install Vue.js
    You can install Vue.js on your local machine through the following command:

    npm install vue
  3. About Vue-cli
    Vue-cli is a scaffolding tool officially provided by Vue.js for building large-scale projects. It provides a fast, zero-configuration scaffolding generation tool to facilitate developers to quickly build Vue projects. The installation command is as follows:

    npm install -g @vue/cli

Basic use of Vue:

  1. Vue instance
    The most basic unit of Vue construction is the Vue instance, which is the core of Vue , encapsulates the basic logic module.

The following is a simple Vue instance bound to a DOM element with the id of app.

<div id="app">
  {{ message }}
</div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello, Vue!'
  }
})
  1. Directives
    Vue's directives are special attributes specified with the v- prefix on HTML tags that are used to bind expressions to specified HTML elements.

The following is a simple case using v-if to switch to different text paragraphs:

<div id="app">
  <p v-if="seen">你看到了我!</p>
</div>
var app = new Vue({
  el: '#app',
  data: {
    seen: true
  }
})
  1. Component
    In Vue, a component is a An abstraction that can be viewed as a custom element.

The following is a simple component used to display a message:

Vue.component('message', {
  props: ['text'],
  template: '<div>{{ text }}</div>'
})

var app = new Vue({
  el: '#app',
  data: {
    message: '你好呀!'
  }
})
<div id="app">
  <message :text="message"></message>
</div>
  1. Routing
    In Vue, routing is treated as a separate component, using It enables navigation between pages.

The following is a simple routing example:

var Foo = { template: '<div>我是Foo!</div>' }
var Bar = { template: '<div>我是Bar!</div>' }

var router = new VueRouter({
  routes: [
    { path: '/foo', component: Foo },
    { path: '/bar', component: Bar }
  ]
})

var app = new Vue({
  router
}).$mount('#app')
<div id="app">
  <router-view></router-view>
</div>

Summary:

This article introduces the basic construction and use of Vue, including installation and configuration, examples, Instructions, components, and routing. Vue provides a wealth of functions and tools that can easily complete front-end development tasks. If you haven’t used Vue yet, you can give it a try.

The above is the detailed content of How to build vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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