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()
})
}
})
slot allows the external environment to insert content into the component's view structure.
Statement of this Website
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
When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.
Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.
The difference between componentization and modularization: Modularization is divided from the perspective of code logic; it facilitates code layered development and ensures that the functions of each functional module are consistent. Componentization is planning from the perspective of UI interface; componentization of the front end facilitates the reuse of UI components.
Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.
In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.
There are two ways to query the current Vue version: 1. In the cmd console, execute the "npm list vue" command to query the version. The output result is the version number information of Vue; 2. Find and open the package.json file in the project and search You can see the version information of vue in the "dependencies" item.
Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.
When we used Amap, the official recommended many cases and demos to us, but these cases all used native methods to access and did not provide demos of vue or react. Many people have written about vue2 access on the Internet. However, in this article, we will take a look at how vue3 uses the commonly used Amap API. I hope it will be helpful to everyone!