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组件中节流函数的失效的原因和解决方法
Dec 02 Vue.js
Vue实现小购物车功能
Dec 21 Vue.js
详解template标签用法(含vue中的用法总结)
Jan 12 Vue.js
Vue项目中使用mock.js的完整步骤
Jan 12 Vue.js
Vue多选列表组件深入详解
Mar 02 Vue.js
Vue-Element-Admin集成自己的接口实现登录跳转
Jun 23 Vue.js
详解gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)
Nov 27 Vue.js
vue3引入highlight.js进行代码高亮的方法实例
Apr 08 Vue.js
vue选项卡切换的实现案例
Apr 11 Vue.js
vue使用localStorage持久性存储实现评论列表
Apr 14 Vue.js
vue3语法糖内的defineProps及defineEmits
Apr 14 Vue.js
如何vue使用el-table遍历循环表头和表体数据
Apr 26 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 smarty模版引擎中变量操作符及使用方法
2009/12/11 PHP
phpmyadmin 常用选项设置详解版
2010/03/07 PHP
php随机生成数字字母组合的方法
2015/03/18 PHP
php使用gettimeofday函数返回当前时间并存放在关联数组里
2015/03/19 PHP
YII2.0之Activeform表单组件用法实例
2016/01/09 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
2019/03/29 PHP
seajs模块之间依赖的加载以及模块的执行
2016/10/21 Javascript
浅谈Nodejs中的作用域问题
2016/12/26 NodeJs
ztree实现左边动态生成树右边为内容详情功能
2017/11/03 Javascript
微信小程序实现的贪吃蛇游戏【附源码下载】
2018/01/03 Javascript
Vue底层实现原理总结
2018/02/17 Javascript
详解Angularjs 自定义指令中的数据绑定
2018/07/19 Javascript
vue中npm包全局安装和局部安装过程
2019/09/03 Javascript
vue实现图片懒加载的方法分析
2020/02/05 Javascript
Node.js中文件系统fs模块的使用及常用接口
2020/03/06 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
JS中锚点链接点击平滑滚动并自由调整到顶部位置
2021/02/06 Javascript
在Windows8上的搭建Python和Django环境
2014/07/03 Python
探究Python的Tornado框架对子域名和泛域名的支持
2015/05/02 Python
Python字符串格式化
2015/06/15 Python
python实现mysql的单引号字符串过滤方法
2015/11/14 Python
python三方库之requests的快速上手
2019/03/04 Python
python3 pygame实现接小球游戏
2019/05/14 Python
Python列表的切片实例讲解
2019/08/20 Python
Pandas DataFrame中的tuple元素遍历的实现
2019/10/23 Python
理解Django 中Call Stack机制的小Demo
2020/09/01 Python
html5 worker 实例(二) 图片变换效果
2013/06/24 HTML / CSS
澳大利亚手表品牌:Time IV Change
2018/10/06 全球购物
美术教学感言
2014/02/22 职场文书
党员群众路线教育实践活动学习笔记
2014/11/05 职场文书
青岛导游词
2015/02/12 职场文书
通知函的格式
2015/04/27 职场文书
2015年档案管理员工作总结
2015/05/13 职场文书
退货证明模板
2015/06/23 职场文书
Java tomcat手动配置servlet详解
2021/11/27 Java/Android
SpringBoot深入分析讲解监听器模式下
2022/07/15 Java/Android