vue 组件基础知识总结


Posted in Vue.js onJanuary 26, 2021

组件基础

1 组件的复用

组件是可复用的Vue实例。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<button-counter></button-counter>
			<button-counter></button-counter>
			<button-counter></button-counter>
		</div>
  <script>
			// 定义一个名为 button-counter 的新组件
			Vue.component('button-counter', {
				data: function () {
					return {
						count: 0
					}
				},
				template: '<button v-on:click="count++">点击了 {{ count }} 次.</button>'
			});

			new Vue({ el: '#app' });
  </script>
 </body>
</html>

注意当点击按钮时,每个组件都会各自独立维护它的count。这里自定义组件的data属性必须是一个函数,每个实例维护一份被返回对象的独立的拷贝。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<button-counter></button-counter>
			<button-counter></button-counter>
			<button-counter></button-counter>
		</div>
  <script>
			var buttonCounterData = {
				count: 0
			}
			// 定义一个名为 button-counter 的新组件
			Vue.component('button-counter', {
				data: function () {
					return buttonCounterData
				},
				template: '<button v-on:click="count++">点击了 {{ count }} 次.</button>'
			});

			new Vue({ el: '#app' });
  </script>
 </body>
</html>

2 通过 Prop 向子组件传递数据

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<blog-post title="My journey with Vue"></blog-post>
			<blog-post title="Blogging with Vue"></blog-post>
			<blog-post title="Why Vue is so fun"></blog-post>
		</div>
  <script>
			Vue.component('blog-post', {
				props: ['title'],
				template: '<h3>{{ title }}</h3>'
			})

			new Vue({ el: '#app' });
  </script>
 </body>
</html>

这里<blog-post>组件就是通过自定义属性title来传递数据。
我们可以使用v-bind来动态传递prop。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<blog-post v-for="post in posts" v-bind:key="post.id" v-bind:title="post.title"></blog-post>
		</div>
  <script>
			Vue.component('blog-post', {
				props: ['title'],
				template: '<h3>{{ title }}</h3>'
			})

			new Vue({
				el: '#app',
				data: {
					posts: [
						{ id: 1, title: 'My journey with Vue' },
						{ id: 2, title: 'Blogging with Vue' },
						{ id: 3, title: 'Why Vue is so fun' }
					]
				}
			});
  </script>
 </body>
</html>

3 单个根元素

每个组件必须只有一个根元素。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post"></blog-post>
		</div>
  <script>
			Vue.component('blog-post', {
				props: ['post'],
				template: `
					<div class="blog-post">
						<h3>{{ post.title }}</h3>
						<div v-html="post.content"></div>
					</div>
				`
			})

			new Vue({
				el: '#app',
				data: {
					posts: [
						{ id: 1, title: 'My journey with Vue', content: 'my journey...' },
						{ id: 2, title: 'Blogging with Vue', content: 'my blog...' },
						{ id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }
					]
				}
			});
  </script>
 </body>
</html>

注意到v-bind:post="post"绑定的post是一个对象,这样可以避免了需要通过很多prop传递数据的麻烦。

4 监听子组件事件

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<div :style="{fontSize: postFontSize + 'em'}">
				<blog-post v-for="post in posts" 
					v-bind:key="post.id" 
					v-bind:post="post"
					v-on:enlarge-text="postFontSize += 0.1" />
			</div>			
		</div>
  <script>
			Vue.component('blog-post', {
				props: ['post'],
				template: `
					<div class="blog-post">
						<h3>{{ post.title }}</h3>
						<button v-on:click="$emit('enlarge-text')">放大字体</button>
						<div v-html="post.content"></div>
					</div>
				`
			})

			new Vue({
				el: '#app',
				data: {
					postFontSize: 1,
					posts: [
						{ id: 1, title: 'My journey with Vue', content: 'my journey...' },
						{ id: 2, title: 'Blogging with Vue', content: 'my blog...' },
						{ id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }
					]
				}
			});
  </script>
 </body>
</html>

子组件通过$emit方法并传入事件名称来触发一个事件。父组件可以接收该事件。

我们可以使用事件抛出一个值。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<div :style="{fontSize: postFontSize + 'em'}">
				<blog-post v-for="post in posts" 
					v-bind:key="post.id" 
					v-bind:post="post"
					v-on:enlarge-text="postFontSize += $event" />
			</div>			
		</div>
  <script>
			Vue.component('blog-post', {
				props: ['post'],
				template: `
					<div class="blog-post">
						<h3>{{ post.title }}</h3>
						<button v-on:click="$emit('enlarge-text', 0.2)">放大字体</button>
						<div v-html="post.content"></div>
					</div>
				`
			})

			new Vue({
				el: '#app',
				data: {
					postFontSize: 1,
					posts: [
						{ id: 1, title: 'My journey with Vue', content: 'my journey...' },
						{ id: 2, title: 'Blogging with Vue', content: 'my blog...' },
						{ id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }
					]
				}
			});
  </script>
 </body>
</html>

在父组件中,我们可以通过$event访问到被抛出的这个值。
我们可以在组件上使用v-model。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<!-- <input v-model="searchText"> -->
			<input v-bind:value="searchText" v-on:input="searchText = $event.target.value">
			<p>{{ searchText }}</p>
		</div>
  <script>
			new Vue({
				el: '#app',
				data: {
					searchText: ''
				}
			});
  </script>
 </body>
</html>
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">  
  <style>
   
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
		<div id="app">
			<custom-input v-model="searchText"></custom-input>
			<custom-input v-bind:value="searchText" v-on:input="searchText = $event"></custom-input>
			<p>{{ searchText }}</p>
		</div>
  <script>
			Vue.component('custom-input', {
				props: ['value'],
				template: `<input v-bind:value="value" v-on:input="$emit('input', $event.target.value)" >`
			})

			new Vue({
				el: '#app',
				data: {
					searchText: ''
				}
			});
  </script>
 </body>
</html>

最后,注意解析 DOM 模板时的注意事项。

以上就是vue 组件基础知识总结的详细内容,更多关于vue 组件的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 Vue.js
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
Dec 25 Vue.js
vue 动态生成拓扑图的示例
Jan 03 Vue.js
vue实现桌面向网页拖动文件的示例代码(可显示图片/音频/视频)
Mar 01 Vue.js
如何理解Vue简单状态管理之store模式
May 15 Vue.js
Vue中插槽slot的使用方法与应用场景详析
Jun 08 Vue.js
解决Vue+SpringBoot+Shiro跨域问题
Jun 09 Vue.js
idea编译器vue缩进报错问题场景分析
Jul 04 Vue.js
vue使用refs获取嵌套组件中的值过程
Mar 31 Vue.js
三种方式清除vue路由跳转router-link的历史记录
Apr 10 Vue.js
vue代码分块和懒加载非必要资源文件
Apr 11 Vue.js
vue组件vue-esign实现电子签名
Apr 21 Vue.js
深入了解Vue动态组件和异步组件
Jan 26 #Vue.js
如何在 Vue 表单中处理图片
Jan 26 #Vue.js
Vue实现摇一摇功能(兼容ios13.3以上)
Jan 26 #Vue.js
Vue中的nextTick作用和几个简单的使用场景
Jan 25 #Vue.js
Vue使用Ref跨层级获取组件的步骤
Jan 25 #Vue.js
如何在Vue项目中添加接口监听遮罩
Jan 25 #Vue.js
使用vue3重构拼图游戏的实现示例
Jan 25 #Vue.js
You might like
PHP HTML代码串截取代码
2008/12/29 PHP
PHP中利用substr_replace将指定两位置之间的字符替换为*号
2011/01/27 PHP
PHP中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
PHP5中Cookie与 Session使用详解
2013/04/30 PHP
实现获取http内容的php函数分享
2014/02/16 PHP
php根据年月获取季度的方法
2014/03/31 PHP
Laravel日志用法详解
2016/10/09 PHP
jquery随机展示头像代码
2011/12/21 Javascript
JQuery-tableDnD 拖拽的基本使用介绍
2013/07/04 Javascript
js闭包实例汇总
2014/11/09 Javascript
基于jquery实现发送文章到手机的代码
2014/12/26 Javascript
jQuery分页插件jquery.pagination.js使用方法解析
2017/02/09 Javascript
Javascript中引用类型传递的知识点小结
2017/03/06 Javascript
详解ECMAScript6入门--Class对象
2017/04/27 Javascript
SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题的解决方法
2018/01/09 Javascript
node.js文件的复制、创建文件夹等相关操作
2021/02/05 Javascript
Python实现全局变量的两个解决方法
2014/07/03 Python
python回溯法实现数组全排列输出实例分析
2015/03/17 Python
用Python分析3天破10亿的《我不是药神》到底神在哪?
2018/07/12 Python
python计算两个矩形框重合百分比的实例
2018/11/07 Python
Python3.7 dataclass使用指南小结
2019/02/22 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
如何实现更换Jupyter Notebook内核Python版本
2020/05/18 Python
ECCO爱步加拿大官网:北欧丹麦鞋履及皮具品牌
2017/07/08 全球购物
JMS中Topic和Queue有什么区别
2013/05/15 面试题
追悼会主持词
2014/03/20 职场文书
小学生期末评语大全
2014/04/21 职场文书
高中班级口号
2014/06/09 职场文书
农民工预备党员思想汇报
2014/09/14 职场文书
2014年小学工作总结
2014/11/26 职场文书
2015教师节通讯稿
2015/07/20 职场文书
学子宴致辞大全
2015/07/27 职场文书
校长新学期致辞
2015/07/30 职场文书
生产设备维护保养制度
2015/08/06 职场文书
《半截蜡烛》教学反思
2016/02/19 职场文书
导游词之海南天涯海角
2019/12/05 职场文书