Vue.js component usage and development example tutorial
高洛峰
Release: 2016-12-08 11:57:38
Original
1386 people have browsed it
Components
Components can extend HTML elements and encapsulate reusable code. At a high level, components are custom elements. The compiler of vue.js adds special functions to it. In some cases, components can also It is in the form of a native HTML element, extended with the is attribute.
Vue.js components can be understood as ViewModel classes with predefined behaviors. A component can predefine many options, but the core ones are the following:
Template (template): The template declares the mapping relationship between the data and the DOM that is ultimately displayed to the user.
Initial data (data): the initial data state of a component. For reusable components, this is usually private state.
Accepted external parameters (props): Data is transferred and shared between components through parameters. Parameters are bound one-way (top to bottom) by default, but can also be explicitly declared two-way.
Methods: Modification operations on data are generally performed within the methods of components. User input events and component methods can be bound through the v-on directive.
Lifecycle hooks: A component will trigger multiple lifecycle hook functions, such as created, attached, destroyed, etc. In these hook functions, we can encapsulate some custom logic. Compared with traditional MVC, it can be understood that the logic of the Controller is dispersed into these hook functions.
Private resources (assets): In Vue.js, user-defined instructions, filters, components, etc. are collectively called resources. Since globally registered resources can easily lead to naming conflicts, a component can declare its own private resources. Private resources can only be called by the component and its subcomponents.
In addition, components within the same component tree can also communicate through the built-in event API. Vue.js provides a complete API for defining, reusing and nesting components, allowing developers to use components to build the entire application interface like building blocks.
Components greatly improve the efficiency, maintainability and reusability of code.
Use component
Register
1. Create a component constructor:
var MyComponent = Vue.extend({
//选项
})
Copy after login
2. Use the constructor as a component and register it with Vue.component(tag,constructor):
Vue.component('my-component',MyComponent)
Copy after login
3. Use it in the module of the parent instance as a custom element :
<div id = "example">
<my-component></my-component>
</div>
<div id = "example">
<div>A custom component!</div>
</div>
Copy after login
Component’s The template replaces the custom element, which only serves as a mount point. You can use the instance option replace to decide whether to replace.
Partial registration
Register with the instance option components. There is no need to register each component globally. The component can only be used in other components:
Most options passed into the Vue constructor can also be used in Vue.extend(), except for data and el, if you simply use an object as the data option Passed to Vue.extend(), all instances will share the same data object, so we should use a function as the data option and let this function return a new object:
var MyComponent = Vue.extend({
data: function () {
return { a: 1 }
}
})
Copy after login
Template parsing
Vue The template is a DOM template that uses the browser's native parser, so it must be a valid HTML fragment. Some HTML elements have restrictions on what elements can be placed in it. Common restrictions are:
a cannot contain other interactive elements. (Such as buttons, links)
ul and ol can only directly contain li
select can only contain option and optgroup
table can only directly contain thead, tbody, tfoot, tr, caption, col, colgroup
tr can only Including th and td directly
In practice, these restrictions can lead to unexpected results. Although it may work in simple cases, you cannot rely on the result of a custom component's expansion before the browser validates it. For example
is not a valid template, even though the my-select component eventually expands to
Another result is that custom tags (including custom elements and special tags, such as , , ) cannot be used in ul, select, table, etc. that have restrictions on internal elements. within the label. Custom tags placed inside these elements will be lifted outside the element and render incorrectly.
For custom elements, the is attribute should be used:
The scope of the component instance is isolated, you can use props to pass the array For child components, props is a field of component data that is expected to be passed down from the parent component. The child component needs to explicitly declare props with the props option:
Vue.component('activate-example', {
activate: function (done) {
var self = this
loadDataAsync(function (data) {
self.someData = data
done()
})
}
})
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