Home> Web Front-end> Vue.js> body text

What is vue component

青灯夜游
Release: 2021-10-26 11:51:03
Original
14829 people have browsed it

In vue, a component is a reusable Vue instance. It has a unique component name. It can extend HTML elements and use the component name as a custom HTML tag. Components can greatly improve code reuse.

What is vue component

The operating environment of this tutorial: Windows 7 system, vue version 2.9.6, DELL G3 computer.

What are components?

Component is an important concept in Vue. It is a reusable Vue instance. It has a unique component name. It can extend HTML elements and use the component name as customized HTML. Label. Because components are reusable Vue instances, they receive the same options as new Vue(), such as data, computed, watch, methods, and life cycle hooks. The only exceptions are root-instance-specific options like el.

Extract some common modules and write them into separate tool components or pages, which can be directly introduced into the required pages. Then we can extract it as a component for reuse.

For example, if multiple pages use the same components, they can be made into components, which improves the code reuse rate.

Using Vue components

Let’s talk about our directory first

1. Create our component B.vue

What is vue component

##Similarly,Create a new hello_word.vue as the parent

What is vue component

Then you will see the result like this

What is vue component

Key points:

Introduce the B.vue module into

hello_word.vue // import showB from './B.vue'Introduce components

What is vue component

Custom module name of B file showB

Registered component

components:{ showB, },  //使用组件
Copy after login

Note:Define component names in camel case, such as:Use PascalCaseUse kebab-case

Start getting to the main topic props

The parent component passes the value to the child component(The parent component binds data such as: value="It's time to work ", the child component obtains it through props)The props option of the child component can receive data from the parent component. That's right, it can only be received. Props are one-way bound, that is, they can only be passed from the parent component to the child component, not the other way around.
Usage is as follows:

B component

Parent component

What is vue component

Then you will see Go to the page and display

What is vue component

. Print the value of value under created

What is vue component##. The result is

The child component passes the value to the parent componentIn the child component:

回复父组件

chuanzhi() { this.$emit('msg', '知道了知道了') //执行 msg 函数并把要改变的值作为参数带过去 }
Copy after login

What is vue componentParent component:

Introduce @msg='msg' in DOM

 msg(val){ console.log(val,'val') }
Copy after login

Receive through the method

What is vue componentRun, click to reply to the parent component

What is vue componentPrint results:

For more programming-related knowledge, please visit:Programming Learning! !

The above is the detailed content of What is vue component. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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