How to use component in vue.js
How to use components in vue.js: 1. Extend HTML elements and encapsulate reusable code; 2. Components are custom elements, and the compiler of [Vue.js] adds special functions to it; 3. , components can also be in the form of native HTML elements, extended with the is attribute.
【Recommended related articles: vue.js】
How to use component in vue.js:
What is a component
According to convention, quote A sentence from the Vue official website:
Component is one of the most powerful features of Vue.js. Components can extend HTML elements, encapsulating 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 take the form of native HTML elements, extended with the is attribute.
Registration of component component
Global component:
Vue.component('todo-item',{ props:['grocery'], template:'<li>{{grocery.text}}</li>' }) var app7 = new Vue({ el:"#app7", data:{ groceryList:[ {"id":0,"text":"蔬菜"}, {"id":1,"text":"奶酪"}, {"id":2,"text":"其他"} ] } })
<div id="app7"> <ol> <todo-item v-for="grocery in groceryList" v-bind:grocery="grocery" v-bind:key="grocery.id"> </todo-item> </ol> </div>
Local registration:
var Child = { template: '<div>A custom component!</div>' } new Vue({ // ... components: { // <my-component> 将只在父模板可用 'my-component': Child } })
DOM template parsing instructions
Components will be subject to some restrictions under certain HTML tags.
For example, in the code, under the table tag, the component is invalid.
<table> <my-row>...</my-row> </table>
The workaround is to use the component via the is attribute
<table> <tr is="my-row"></tr> </table>
It should be noted that if you use a string template from one of the following sources, it will not be restricted
<script type="text/x-template">
JavaScript inline template string
.vue
Component
data uses functions to avoid multiple components affecting each other
html
<div id="example-2"> <simple-counter></simple-counter> <simple-counter></simple-counter> <simple-counter></simple-counter> </div>
js
var data = { counter: 0 } Vue.component('simple-counter', { template: '<button v-on:click="counter += 1">{{ counter }}</button>', data: function () { return data } }) new Vue({ el: '#example-2' })
As above, there are three components under the div, and each component shares a counter. When any component is clicked, the counters of all components will be incremented by one.
The solution is as follows
js
Vue.component('simple-counter', { template: '<button v-on:click="counter += 1">{{ counter }}</button>', data: function () { return {counter:0} } }) new Vue({ el: '#example-2' })
In this way, after each component is generated, it will have its own exclusive counter.
Related free learning recommendations: JavaScript (video)
The above is the detailed content of How to use component in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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.

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.

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.

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.

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.
