Home > Article > Web Front-end > This article will give you a detailed explanation of component programming in Vue
This article will talk about component programming in Vue, and share an understanding of vue componentization, the most important single-file component. I hope it will be helpful to everyone!
Componentization makes me feel more and more the power of the framework
Understanding of components
If we write a web page in the original way
I won’t talk about the confusion of dependencies, but why do you still say that the code reuse rate is not high? Didn’t I introduce all the reusable css and js? That's because our html is not reused. The top and bottom of the two pages above and below are the same. What I can do is directly Copy Note that this is copying and not reuse. [Related recommendations: vuejs video tutorial, web front-end development]
What the component can do is to combine each function A combination has been made, which contains all the files required for this function. If you want to reuse it elsewhere, just enter it directly. Note that our html is only the html part of this separate part.
Components can be nested, just like one of our sections can also subdivide other sections
Definition : Implement local functions code (css, html, js) and resources (mp3, MP4, ttf, etc.) Collection
Module: A js file is a module
Component: Collection
Modularization: It is to split a huge js file into multiple branch modules to jointly complete a function (same as the previous es6 modular programming)
Componentization: A web page is split into different components according to different functions
That is, a file (a. html) contains n components
Single file component: One file (a.vue) contains only one component
##1 .Basically use
The previous approach to complete such a small function ##1.1 Create a componentThere are several points to note when creating our components:
The component is just a brick. Move wherever needed. No matter how many components there are, they will eventually be managed by the big brother vm, so vm will configure el , to specify who to serve
A brand new configuration itemcomponentsPay attention to the way the key-value pairs are written inside. The attribute name is our real component name, and the following value is just the variable name we just took, but it is generally recommended to write the same, because you can directly write an abbreviation
And this is a partial registration method
Write the named component name in the form of html tag,named : Component tag
The data of each component tag is separate and does not interfere with each other
A single word (all lowercase or the first letter is capitalized), multiple words (all lowercase or the same as the previous custom instructions are connected with - and the original attribute name is returned to the original attribute name wrapped in ''. There is also a method to wrap the first letter of all words All letters must be capitalized including the first initial (but only applicable to scaffolding environment))
, you can specify this component in the developer tools The name used in
## 2. Nesting of components
First of all, we generally only have one component under the vm for formal development appThis component will proxy our vm and manage all components. The sub-components managed by our app include two hello and school, so we need to register them. In the app, write the component label in the template of the app. There is a sub-component student under school. Similarly, it needs to be registered under student. Its component label is written in the template of school. In the end, the vm instance has only one registered component. , app, our html structure also has only one component tag, app
In short, one thing to note when nesting is: The sub-component must write its own component tag in the parent component and register itself In the parent component, keep writing until the end of the app, and finally write the app in the vm
##3.VueComponent constructor
#Our component is essentially a VueComponent constructor, which is our Vue .extend will help us create a constructor and assign it to this variable
or the closing tag. Vue will generate an instance of this constructor during parsing and help us create it new
The this points of our functions in methods, computed, watch, etc. in new Vue are all vm instance objects. The this points of our functions in methods, computed, watch, etc. in the component They are all instance objects of VueComponent, abbreviated as vc (only appear in class, and component instance objects are mentioned outside), and they are basically the same as vm. They also have data proxies, data hijacking, etc.
4. An important built-in relationship
VueComponent.prototype.proto == Vue.prototypeThe thread in my heart needs to be built
Purpose: Let the component vc also use the properties and methods on the vue prototype
We said that a component.vue file contains html, js, css, so a standard single file component requires html (template tag), js (script tag), css (style tag)
Plug-in: vetur(pine wu)After installation, you can use the shortcut key
First create a functional component school
It should be noted that
one. Because our components need to be referenced by others, we need to expose them when we write the components. Generally, what is exposed is script, and this is an abbreviation. The real original version is as follows ,
二. There is also our name configuration item Generally speaking, the root file name is consistent, and our file name is generally in the form of capital letters, which can be consistent with the vue management tool
three. Our template tag should be wrapped by a div
You don’t have to write a style if you don’t have a style
The next componentmust have it. As mentioned before, a component that replaces the vm to manage all the following components is in one person. Next, the position above ten thousand people is the app component
This component is generally used to introduce our sub-components and register them. Note that the introduction is written outside the export, and then We also need to call
## in the template. Then we need a vm big brother to direct the component construction. For whom to serve, generally define a js file of main.js
Import and register our App component. If you want to keep the next page clean, you can write a template here. Write the app component tag, you can also see it on the next page
Note: Our vue must be introduced first before the new Vue in main.js can take effect
Since then, we have built a single-file component environment, but to run it, it must cooperate with the scaffolding environment.
(Learning video sharing:
vuejs introductory tutorialThe above is the detailed content of This article will give you a detailed explanation of component programming in Vue. For more information, please follow other related articles on the PHP Chinese website!