What are Vue components? How to use Vue components? (code example)
This article brings you an introduction to what Vue components are? How to use Vue components? (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Introduction to components
The component system divides a large interface into smaller controllable ones. unit.
Components are reusable and maintainable.
The component has strong encapsulation and is easy to use.
In large-scale applications, the interaction between components can be decoupled.
Using Vue components
Using global components
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <link> <script></script> <div> <my-header></my-header> </div> <script> //全局组建的定义 Vue.component("my-header", { template: '<h1>全局组件' }); var app = new Vue({ el: '#app', }); </script>
Use of local components
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <link> <script></script> <div> <my-header></my-header> </div> <script> //局部组件定义 var app = new Vue({ el: '#app', components: { 'my-header': { template: '<h1>局部组件' } } }); </script>
Characteristics of component data
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> <my-header></my-header> <my-header></my-header> <br> <my-header1></my-header1> <my-header1></my-header1> </div> <script> //组件数据之间不共享,Vue实例中的数据也不能共享给组件,并且组件中的data只能使用函数 //组件的数据data使用函数的特点是每次点击或操作组件函数会重新执行,这样就不会影响其它组件,通过下面两个例子可以看出 var data = { count: 0 }; var app = new Vue({ el: '#app', data: { message: "Hello Vue!!!" }, components: { 'my-header': { template: '<h1 v-on:click="changeCount">{{count}}', data: function() { return data; }, methods: { changeCount: function() { this.count++; } } }, 'my-header1': { template: '<div v-on:click="changeCount1">{{count}}', data: function() { return { count: 0 }; }, methods: { changeCount1: function() { this.count++; } } } } }); </script>
The relationship between Vue instances and components
The Vue component is actually an extensible Vue instance.
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> {{message}} </div> <script> //Vue组件其实是一个可扩展的Vue实例,Vue实例也可以看成是一个组件 // var app = new Vue({ // el: '#app', // template: '<h1>app应用' // }); //使用继承实现Vue组件 var myComponent = Vue.extend({ data: function() { return { message: "Hello Vue@@@" } } }); var vm = new myComponent({ el: '#app' }); </script>
The template method of Vue components
\
`
.vue single file
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> <my-header></my-header> <my-header-1></my-header-1> <my-header-2></my-header-2> <my-header-3></my-header-3> </div> <template> <div> <p> </p> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </div> </template> <script> <div> <p> <ul> <li>1 <li>2 <li>3 </script> <script> //Vue模版添加方式 var app = new Vue({ el: '#app', components: { 'my-header': { template: '<div>\ <p>\ <ul>\ <li>1\ <li>2\ <li>3\ \ \ ', data: function() { return { message: "第一项" } } }, 'my-header-1': { template: `<div> <p> <ul> <li>1 <li>2 <li>3 `, }, 'my-header-2': { template: '#temp' }, 'my-header-3': { template: '#temp1' } } }); </script>
Vue parent component communicates with child components (props)
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> <!-- 数据在组件中 --> <my-header></my-header> <!-- 父组件向子组件传递数据 --> <my-header-1></my-header-1> <!-- 父组件向子组件传递数据不给值 --> <my-header-1></my-header-1> </div> <!-- 数据在组件中的模版 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> </div> </template> <!-- 父组件向子组件传递数据的模版 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> <my-nav></my-nav> </div> </template> <!-- 子组件向子组件传递数据的模版 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> </div> </template> <script> var vm = new Vue({ el: '#app', data: { temp_2_list: ["1", "2", "3"] }, components: { //数据在自己组件中的实例 'my-header': { template: '#temp-1', data: function() { return { list: ["1", "2", "3"], message: "组件中的数据" }; } }, //父组件向子组件传递数据 'my-header-1': { //props: ["list"], template: '#temp-2', data: function() { return { message: "父组件向子组件传递数据" }; }, //属性的验证与默认值 props: { list: { type: Array, default: ["4", "5", "6"] } }, //子组件的子组件 components: { 'my-nav': { template: '#temp-3', data: function() { return { message: "子组件中的子组件" }; }, props: ["itemlist"] } } } } }); </script>
Child component communicates with parent component (EmitEvents)
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> <my-header-1></my-header-1> </div> <!-- 父组件向子组件传递数据的模版 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> <my-nav></my-nav> </div> </template> <!-- 子组件向子组件传递数据的模版 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> </div> </template> <script> //子组件向父组件传递数据,是发布订阅模式 var vm = new Vue({ el: '#app', data: { temp_2_list: ["1", "2", "3"] }, components: { //父组件向子组件传递数据 'my-header-1': { //props: ["list"], template: '#temp-2', data: function() { return { message: "父组件向子组件传递数据" }; }, //属性的验证与默认值 props: { list: { type: Array, default: ["4", "5", "6"] } }, methods: { getChildContent: function(str) { debugger alert(str); } }, //子组件的子组件 components: { 'my-nav': { template: '#temp-3', data: function() { return { message: "子组件中的子组件" }; }, props: ["itemlist"], methods: { getContent: function(ev) { // console.log(this); // console.log(ev.target.innerHTML); this.$emit("change-events", ev.target.innerHTML); } } } } } } }); </script>
Communication of Vue non-parent-child components
Empty instance With custom events
$emit
$on
Vuex state management
state
mutation
commit
Use of empty instances and custom events (suitable for small projects)
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <style> ul { list-style-type: none; } </style> <div> <my-header-1></my-header-1> <my-header-2></my-header-2> </div> <script> //非父子组件通信 //1.0 使用空实例进行非父子组件通信 //定义空实例 //创建步骤是: //1、首先定义一个空实例 //2、需要给被传递数据的A组件使用$emit绑定自定义事件,并将A组件的数据发布给B组件 //3、使用$on订阅A组件发布过来的数据,从而获取数据 var busVm = new Vue(); var vm = new Vue({ el: '#app', components: { //组件B 'my-header-1': { template: `<h1>{{message}}`, data: function() { return { message: "非父子组件通信" }; }, mounted() { //使用bind(this)修正this busVm.$on("changeEnvents", function(param) { this.message = param; }.bind(this)); }, }, //组件A 'my-header-2': { template: `<ul><li @click="getContent" v-for="item in list">{{item}}`, data: function() { return { list: ["第一项", "第二项", "第三项"] }; }, methods: { getContent: function(ev) { busVm.$emit("changeEnvents", ev.target.innerHTML); } } } } }); </script>
Vuex state management
Vue Component content distribution
Single
tag usage
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <style> ul { list-style-type: none; } </style> <div> <my-header-1> <h1 id="我是标题">我是标题</h1> </my-header-1> <my-header-1> <my-header-2></my-header-2> </my-header-1> </div> <script> //单插槽<slot> var vm = new Vue({ el: '#app', components: { 'my-header-1': { template: `<div>我是头部:<slot>`, }, 'my-header-2': { template: `<h1>我是标题`, } } }); </script>
- Multiple
tags use
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <style> ul { list-style-type: none; } </style> <div> <my-header-1> <button>←</button> <button>→</button> </my-header-1> </div> <script> //多插槽的使用,则使用name属性来指定要插入的位置 var vm = new Vue({ el: '#app', components: { 'my-header-1': { template: `<div><slot name="left"> 我是头部:<slot name="right">`, }, 'my-header-2': { template: `<h1>我是标题`, } } }); </script>
-
Default value
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <style> ul { list-style-type: none; } </style> <div> <my-header-1> <button>←</button> <button>→</button> </my-header-1> </div> <script> //多插槽的使用,则使用name属性来指定要插入的位置 var vm = new Vue({ el: '#app', components: { 'my-header-1': { template: `<div><slot name="left"> 我是头部:<slot name="right"><button slot="right">+`, }, 'my-header-2': { template: `<h1>我是标题`, } } }); </script>
Vue component development process
- Writing basic HTML and CSS ##Extraction Component
- Data transmission
- Content distribution
- Add events and methods
nbsp;html> <meta> <meta> <title>Page Title</title> <meta> <script></script> <div> <my-header-1></my-header-1> </div> <!-- 父组件向子组件传递数据的模版,ref特性用于DOM操作,使用this.$refs.row获取添加特性的DOM元素 --> <template> <div> <h1 id="message">{{message}}</h1> <ul> <li>{{item}}</li> </ul> </div> </template> <script> //子组件向父组件传递数据,是发布订阅模式 var vm = new Vue({ el: '#app', data: { temp_2_list: ["1", "2", "3"] }, components: { //父组件向子组件传递数据 'my-header-1': { //props: ["list"], template: '#temp-2', data: function() { return { message: "父组件向子组件传递数据" }; }, //属性的验证与默认值 props: { list: { type: Array, default: ["4", "5", "6"] } }, methods: { updateStyle: function(ev) { ev.target.style.color = 'red'; // this.$refs.row.style.color = 'red'; console.log(this.$refs.row); this.$refs.row.forEach(element => { console.log(element); element.style.color = 'red'; }); } } } } }); </script>
The above is the detailed content of What are Vue components? How to use Vue components? (code example). For more information, please follow other related articles on the PHP Chinese website!

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
