Home > Web Front-end > JS Tutorial > body text

Vue component basics

不言
Release: 2018-05-05 15:52:14
Original
1322 people have browsed it

This article summarizes the relevant knowledge points and code examples of the basics of vue components. Friends in need can learn and refer to it.

What is a component

A component is a simple encapsulation of data and methods. The component in the web can actually be regarded as a component of the page. It is an interface with independent logic and functions. At the same time, it can be integrated with each other according to the specified interface rules, and finally becomes a complete application. The page is composed of a It is composed of similar components, such as navigation, lists, pop-up windows, drop-down menus, etc. The page is just a container for such components. The components are freely combined to form a fully functional interface. When a component is not needed or you want to replace a component, you can replace and delete it at any time without affecting the operation of the entire application. , The core idea of ​​front-end componentization is to split a huge and complex thing into small things with reasonable granularity.

Use to improve development efficiency, facilitate reuse, simplify debugging steps, improve the maintainability of the entire project, and facilitate collaborative development.

As a lightweight front-end framework, vue’s core is component development.

Components can extend HTML elements and encapsulate reusable code. At a high level, a component is a custom element to which Vue.js's compiler adds special functionality. In some cases, components can also appear as native HTML elements extended with the is attribute.

In vue, components are reusable Vue instances. Because components are reusable Vue instances, they receive the same options as new Vue, such as data, computed, watch, methods, and lifecycle hooks. The only exceptions are root-instance-specific options like el.

Component registration

##Global registration

Create components through Vue.component:

 Vue.component('my-component-name', {
 // ... 选项 ...
 })
Copy after login

These components are registered globally. That is to say, they can be used in the template of any newly created Vue root instance (new Vue) after registration. For example:

Vue.component('component-a', { /* ... */ })
Vue.component('component-b', { /* ... */ })
Vue.component('component-c', { /* ... */ })

new Vue({ el: '#app' })

<p id="app">
 <component-a></component-a>
 <component-b></component-b>
 <component-c></component-c>
</p>
Copy after login

The same is true for all sub-components, which means that these three components can also use each other internally.

Local registration

Global registration is often not ideal. For example, if you use a build system like webpack, registering all components globally means that even if you no longer use a component, it will still be included in your final build result. This results in an unnecessary increase in the amount of JavaScript downloaded by users.

In these cases, you can define the component via a plain JavaScript object:

var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
Copy after login

Then define the component you want in the components options Component to be used:

new Vue({
 el: &#39;#app&#39;
 components: {
 &#39;component-a&#39;: ComponentA,
 &#39;component-b&#39;: ComponentB
 }
})
Copy after login

For each attribute in the components object, its attribute name is the name of the custom element, and its attribute value is this The component's options object.

Note that locally registered components are not available in their child components. For example, if you want ComponentA to be available in ComponentB, you need to write:

var ComponentA = { /* ... */ }

var ComponentB = {
 components: {
 &#39;component-a&#39;: ComponentA
 },
 // ...
}
Copy after login

Registered components using Babel and webpack

import ComponentA from &#39;./ComponentA.vue&#39;

export default {
 components: {
 ComponentA
 },
 // ...
}
Copy after login

Note that in ES2015, putting a variable name similar to ComponentA in the object is actually the abbreviation of ComponentA: ComponentA, that is, the variable name is also:

The name of the custom element used in the template

The variable name that contains the option of this component

Automated global registration of basic components


I don’t understand .

data must be a function

data: {
 count: 0
}
Copy after login

The variables in data defined in this way are global variables. When using components, they are in a component Modifying the value of a variable will affect the value of the variable in all components. To avoid variable interference, a component's data option must be a function, so each instance can maintain a separate copy of the returned object:

data: function () {
 return {
 count: 0
 }
}
Copy after login

Dynamic components

It is very useful to dynamically switch between different components, such as in a multi-tab interface:

The above content can be achieved by adding a special is attribute to Vue's element:

<!-- 组件会在 `currentTabComponent` 改变时改变 -->
<component v-bind:is="currentTabComponent"></component>
Copy after login

You will notice that if you If you select an article, switch to the Archive tab, and then switch back to Posts, the article you selected previously will not continue to be displayed. This is because Vue creates a new currentTabComponent instance every time you switch to a new tab.

Recreating the behavior of dynamic components is often very useful, but in this case, we prefer that the component instances of those tags be cached when they are first created. To solve this problem, we can wrap its dynamic component with a element.

<!-- 失活的组件将会被缓存!-->
<keep-alive>
 <component v-bind:is="currentTabComponent"></component>
</keep-alive>
Copy after login

You can view dynamic component examples here. https://jsfiddle.net/chrisvfritz/Lp20op9o/

dom标签内使用组件

有些 HTML 元素,诸如