Vue2 skill tree (1)-introduction, import and use, responsive data binding, component development

Directory

  • Vue2 skill tree
  • What is Vue.js
    • Reactive data binding
    • Component development
    • Simple template syntax
    • ecosystem
    • gradually strengthen
    • community support
  • Install, import and use Vue.js
    • Step 1: Install Node.js
    • Step 2: Create a new project or add to an existing project
      • Create new project
      • Add to existing project
    • Step 3: Import Vue.js
    • Step 4: Create Vue instance
    • Step 5: Create HTML template
    • Step 6: Start the development server
  • Detailed explanation of Vue 2 responsive data binding
    • data attributes
    • Data binding in templates
    • instruction
      • `v-bind`
      • `v-model`
      • `v-if` and `v-else`
      • `v-for`
    • Computed properties
    • Listening properties
    • Vue.set and this.$set
  • Detailed explanation of Vue 2 component development
    • Create component
    • Component communication
      • Parent-child component communication
      • Sibling component communication
      • Vuex
    • life cycle hooks
    • slot
    • dynamic components

Like, your approval is the motivation for my creation!

Collection, your favor is the direction of my efforts!

Comments, your opinions are the wealth of my progress!

Vue2 skill tree

Vue2 skill tree (1) – introduction, import and use, responsive data binding, component development
vue2 skill tree (2)-template syntax, vue tool chain, progressive framework
Vue2 skill tree (3)-declarative rendering, instruction collection, life cycle function
Vue2 skill tree (4)-interpolation, dynamic parameters, instruction modifiers, calculated properties, listeners

What is Vue.js

Vue.js, commonly known as Vue, is a popular JavaScript front-end framework for building user interfaces. Its goal is to help developers easily build interactive, responsive single-page applications (SPA) or complex front-end applications. The design philosophy of Vue.js includes the following key features:

Responsive data binding

Vue.js offers reactive data binding, which means that when you modify data in your app, the associated views automatically update to reflect those changes. This greatly simplifies DOM manipulation, allowing you to focus on processing the data instead of having to manually manage the synchronization of views and data.

Component development

Vue.js encourages splitting your application into reusable components. Each component has its own state, templates, and behaviors, making the code easier to maintain and extend. Components can be combined with each other to build complex interfaces.

Simple template syntax

Vue.js uses a template syntax that lets you build views declaratively. The template syntax is similar to HTML, but also contains Vue directives for data binding and DOM manipulation.

Ecosystem

The Vue.js ecosystem includes a wealth of plug-ins, tools and supporting libraries, such as Vue Router for routing, Vuex for state management, Vue CLI for project construction, etc. These tools make Vue.js suitable for various application scenarios.

Gradually increasing

The core library of Vue.js only focuses on the view layer. It can be seamlessly integrated with other libraries and existing projects, and can also gradually enhance the functionality of existing projects without rewriting the entire application.

Community Support

Vue.js has a large developer community that provides extensive learning resources, documentation, and social discussions. This makes it a popular front-end framework that is widely supported and used.

In short, Vue.js is a flexible, easy-to-learn, and powerful tool for building modern front-end applications, suitable for a variety of project sizes, from small web applications to large enterprise-level applications.

Installing, importing and using Vue.js

There are several steps involved in installing, importing, and using Vue.js, and here’s a closer look at how to perform them.

Step 1: Install Node.js

Vue.js requires Node.js running environment. If you have not installed Node.js, please go to the Node.js official website to download and install the latest version.

Step 2: Create a new project or add to an existing project

Create new project

If you want to create a new Vue.js project, you can use the Vue CLI tool to quickly build a project skeleton. Open a terminal and execute the following command:

# Install Vue CLI (if not installed)
npm install -g @vue/cli

# Create a new project
vue create my-vue-app

Follow the prompts to select configuration options and wait for the project to be created.

Add to existing project

If you want to add Vue.js to an existing project, you can install it via:

# In the project directory, use npm to install Vue.js
npm install vue

Step 3: Import Vue.js

Once you have created your project or installed Vue.js, you need to import it in your code. Typically, you need to add the following import statement to your JavaScript file:

import Vue from 'vue';

This will allow you to use Vue.js in your application.

Step 4: Create Vue instance

To use Vue.js, you need to create a Vue instance. Typically, you will write your application logic in a JavaScript file. Here’s a simple example:

//Import Vue
import Vue from 'vue';

//Create a Vue instance
const app = new Vue({<!-- -->
  el: '#app', // Mount the Vue instance to the specified HTML element
  data: {<!-- -->
    message: 'Hello, Vue!'
  }
});

In this example, we create a Vue instance and mount it on the HTML element with the id “app”. We also define a data object, which contains a property named message.

Step 5: Create HTML template

Finally, you need to create a template in the HTML file so that the Vue instance can render it. Add the following code in the HTML file:

<div id="app">
  {<!-- -->{ message }}
</div>

This template includes a placeholder containing the message data attribute.

Step 6: Start the development server

If you used the Vue CLI to create your project, you can use the following command to start the development server:

npm run serve

If you created the project manually, simply open your HTML file in a browser, or use a local development server to preview your app.

The above are the basic steps to install, import and use Vue.js. You can now start building your Vue.js application, defining data, components, methods, etc. to create interactive front-end applications.

Detailed explanation of Vue 2 responsive data binding

Vue.js 2 provides powerful reactive data binding, which is one of its core features. This allows you to easily synchronize data with the view without having to manually manipulate the DOM. The following is a detailed introduction to Vue 2’s reactive data binding.

Data attributes

In Vue 2, you need to define your data properties in the data attribute of the Vue instance. These data properties will automatically become responsive so that related views can be updated when the data changes.

const app = new Vue({<!-- -->
  data: {<!-- -->
    message: 'Hello, Vue!',
    count: 0
  }
});

In the above example, message and count are two reactive data properties.

Data binding in templates

You can use double curly braces {{ }} in the template to bind data to DOM elements, and Vue will automatically update the view to reflect the changes in the data.

<div>
  {<!-- -->{ message }}
  <button @click="count + + ">Increment Count</button>
  {<!-- -->{ count }}
</div>

In this example, message and count are bound to two

elements, respectively, and to a button click event.

Directives

Vue 2 provides various directives for controlling and manipulating DOM elements in templates. Here are some commonly used instructions:

v-bind

The v-bind directive is used to dynamically bind the value of HTML attributes. This allows you to dynamically set the value of a data attribute to an element’s attribute.

<img v-bind:src="imageUrl">

v-model

The v-model directive implements two-way data binding and is typically used for form elements. It bidirectionally binds form element values to data attributes, allowing form inputs to affect data and vice versa.

<input v-model="username">

v-if and v-else

The v-if and v-else directives are used for conditional rendering to control the display or hiding of elements based on given conditions.

<div v-if="showMessage">This is a message.</div>
<div v-else>Message is hidden.</div>

v-for

The v-for directive is used to loop through rendering elements, typically used with arrays.

<ul>
  <li v-for="item in items">{<!-- -->{ item }}</li>
</ul>

Computed properties

Vue 2 also allows you to define computed properties, which are data properties derived from a Vue instance. Computed properties automatically calculate and return a value based on the data properties they depend on.

const app = new Vue({<!-- -->
  data: {<!-- -->
    radius: 5
  },
  computed: {<!-- -->
    area() {<!-- -->
      return Math.PI * Math.pow(this.radius, 2);
    }
  }
});

In this example, area is a computed property that depends on the radius data property.

Listening attributes

You can use the watch option to listen for changes in specific data properties and execute custom logic when the data changes.

const app = new Vue({<!-- -->
  data: {<!-- -->
    message: 'Hello, Vue!'
  },
  watch: {<!-- -->
    message(newValue, oldValue) {<!-- -->
      console.log('Message changed from', oldValue, 'to', newValue);
    }
  }
});

Vue.set and this.$set

In Vue 2, if you need to add new properties to a reactive object at runtime, you can use the Vue.set or this.$set method to ensure this The new properties are also responsive.

Vue.set(app.data, 'newProperty', 'New Value');

This is a detailed, multi-faceted introduction to Vue 2 reactive data binding. The reactive system of Vue.js makes front-end development easier and more efficient because it can automatically handle the synchronization of data and views. Instead of manually manipulating the DOM, you can focus on your application’s logic and functionality.

Detailed explanation of Vue 2 component development

Vue.js 2 encourages component-based development, which means you can split your application into multiple reusable components, each with its own state, templates, and behaviors. This improves code maintainability and reusability. The following is a detailed and multi-faceted introduction to Vue 2 component development.

Create component

In Vue 2, creating a component typically involves the following steps:

  1. Define components: You can use the Vue.component function to define a global Vue component, or register a component locally in a Vue instance.

    //Globally register components
    Vue.component('my-component', {<!-- -->
      // Component options
    });
    
  2. Component options: Components usually include options such as data, templates, methods, etc. For example:

    Vue.component('my-component', {<!-- -->
      data() {<!-- -->
        return {<!-- -->
          message: 'Hello from my component'
        };
      },
      template: '<div>{<!-- -->{ message }}</div>'
    });
    
  3. Using components: You can use custom tags for components in your templates.

    <my-component></my-component>
    

Component communication

Components usually need to communicate with each other, and Vue 2 provides a variety of ways to implement component communication:

Parent-child component communication

  • Props: Parent components can pass data to child components through props.

    Vue.component('child-component', {<!-- -->
      props: ['message'],
      template: '<div>{<!-- -->{ message }}</div>'
    });
    
    <child-component :message="parentMessage"></child-component>
    
  • Custom events: Child components can trigger custom events through $emit, and parent components can listen to these events.

    Vue.component('child-component', {<!-- -->
      template: '<button @click="$emit('custom-event')">Click me</button>'
    });
    
    <child-component @custom-event="handleEvent"></child-component>
    

Brother component communication

  • Event Bus: You can use a global event bus to communicate between sibling components.

    //Create event bus
    const bus = new Vue();
    
    // send event
    bus.$emit('custom-event', data);
    
    //Receive events
    bus.$on('custom-event', data => {<!-- -->
      // Handle events
    });
    

Vuex

For large applications, you can use Vuex to manage global state so that data can be shared between different components.

Life cycle hook

Every Vue component has lifecycle hook functions that allow you to execute custom logic during different lifecycle stages of the component. Some commonly used lifecycle hooks include created, mounted, updated, and destroyed.

Vue.component('my-component', {<!-- -->
  created() {<!-- -->
    // Executed when the component instance is created
  },
  mounted() {<!-- -->
    // Executed after the component is mounted to the DOM
  },
  updated() {<!-- -->
    // Execute when component is updated
  },
  destroyed() {<!-- -->
    // Executed when the component is destroyed
  }
});

Slot

Vue 2 provides a slot mechanism that allows you to insert content inside components. This makes the component more flexible and can accept different content.

<my-component>
  <p>Custom content goes here.</p>
</my-component>
Vue.component('my-component', {<!-- -->
  template: '<div><slot></slot></div>'
});

Dynamic components

Vue 2 allows you to dynamically render different components, which is useful for loading different components based on conditions or user input.

<component :is="currentComponent"></component>
new Vue({<!-- -->
  data: {<!-- -->
    currentComponent: 'my-component'
  }
});

This is a detailed, multi-faceted introduction to component-based development in Vue 2. Component development is a key concept of Vue.js, which makes the structure of the application clearer and maintainable, and also improves the reusability of the code. Through components, you can better manage different parts of your application and improve development efficiency.