Specification for writing vue components
This time I will bring you the vue component writing specification. What are the precautions of the vue component writing specification. The following is a practical case, let's take a look.
DataDriven and componentization are the two most important features of vue.js. Componentization is to facilitate code reuse and improve development efficiency. There are four common ways to write Vue components, each with its own characteristics and suitable for different scenarios.
1. Global component
Structure:
// 组件的注册 Vue.component( 'componentName', { template: // 组件的html结构, data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 }) // 组件的使用 new Vue({ el: '#app' })
Define a global component through Vue.component() in the script tag component, and apply the component to the tag with the id app in the html file through the new Vue() instance.
Features:
<1> Can be directly defined and used in the script tag in the html file;
<2> Pass The components defined by this method are global components and can be used in any Vue instance. They are suitable for relatively simple project scenarios;
<3> They must be reused every time defining a component Vue.component(), and the component names cannot be the same;
Example:
Welcome component
2. Local component
Structure:
// 构造组件对象 const componentName = { template: // 组件的html结构, data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 } // 组件的使用 new Vue({ el: '#app', components: { // 组件注册、调用 componentName } })
Define a component object in the script tag and register the component through the components attribute in the Vue instance.
Features:
<1>Similar to components defined globally, components can be written and used directly in the script tag in the html file;
<2>This component can only be used in a registered Vue instance;
Instance:
Welcome component
3. Use the template tag
Structure:
<template id="componnet"> // 组件的html结构 </template> // 全局组件的注册与使用 Vue.component( 'componentName', { template: '#component', data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 }) new Vue({ el: '#app' }) // 局部组件的注册与使用 const componentName = { template: '#component', data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 } new Vue({ el: '#app', components: { // 组件注册、调用 componentName } })
Use the template tag to write the html structure in the component inside the body tag and in the script tag It is registered and used in the way of global components and local components. The difference is that the template attribute in the component is referenced by id.
Features:
<1>The js file does not contain html structure content, realizing the separation of structure and logic;
Example:
Welcome component
4. Single file component
Structure:
<template lang="html"> // 组件中的html结构 </template> <script> //组件的逻辑 export default { // 组件的属性和方法 } </script> <style lang="css" scoped> // 组件的样式 </style>
Creation A file with the suffix vue, the file name is the component name. The component contains three parts: html structure, js logic, and css style, which correspond to different tags. When using components, you can use them by importing them.
Features:
<1>Components do not affect each other and have high reusability, and their html, css, and js can all be reused;
<2>The structure and logic of the components are clear;
<3>Suitable for large and complex projects, suitable for multi-person development;
Example:
Welcome component
! ! ! It should be noted that: all tags in the template tag must be wrapped with one tag, otherwise an error will be reported
Correct way of writing:
<template> <p> <p></p> ...... <p></p> </p> </template>
Wrong way of writing:
<template> <p></p> <p></p> ...... <p></p> </template>
Believe it After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of using vuex
##What are the methods to operate render execution
The above is the detailed content of Specification for writing vue components. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

Vue is one of the most popular front-end frameworks currently, and VUE3 is the latest version of the Vue framework. Compared with VUE2, VUE3 has higher performance and a better development experience, and has become the first choice of many developers. In VUE3, using extends to inherit components is a very practical development method. This article will introduce how to use extends to inherit components. What is extends? In Vue, extends is a very practical attribute, which can be used for child components to inherit from their parents.

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Introduction to the basic writing and usage of Java callback functions: In Java programming, the callback function is a common programming pattern. Through the callback function, a method can be passed as a parameter to another method, thereby achieving indirect call of the method. The use of callback functions is very common in scenarios such as event-driven, asynchronous programming and interface implementation. This article will introduce the basic writing and usage of Java callback functions, and provide specific code examples. 1. Definition of callback function A callback function is a special function that can be used as a parameter

Detailed explanation of how to write the less than sign in MyBatis MyBatis is an excellent persistence layer framework that is widely used in Java development. In the process of using MyBatis for database operations, we often use the less than sign (

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment
