Vue中的组件

  在应用程序的开发中,组件是不可缺少的。在Vue的使用中,同样也会用到组件。
  vue组件的一般知识点:
  1、组件的名字唯一;
  2、组件以Html形式书写;
  3、组件可以复用;
  4、组件可以嵌套;
  5、组件可以相互调用;
  6、组件分为可视化组件和非可视化组件。
  一般情况下,组件写在一个单独的文件中,在使用的时候按需引入和使用。

  一、组件的定义和使用

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue组件的定义与使用</title><script src="vue.js"></script>    
</head>
<body><div id="demo1" style="background-color: aquamarine;"><p>这是demo1组件</p><my-html1></my-html1><my-html2></my-html2><my-html3></my-html3><my-html5></my-html5><my-html7 inline-template><div>{{title}}</div>                </my-html7></div><div id="demo2" style="background-color: darksalmon;"><p>这是demo2组件</p><my-html1></my-html1><my-html2></my-html2><my-html4></my-html4><my-html6></my-html6></div><template id="component5"><div><p>组件5</p></div></template>    <script type="text/x-template" id="component6"><div><p>{{ mytitle }}</p></div></script><script>var mycompponet5={template:"#component5"}//创建组件模板对象const mytemplate=Vue.extend ({template:`<div><p>标签组件1</p></div>`});//注册全局组件Vue.component('my-html1',mytemplate);Vue.component('my-html5',mycompponet5);Vue.component('my-html6',{template:'#component6',data(){{ return {mytitle:"组件6"}    }}});Vue.component('my-html7',{data(){return {title:'组件7' }}});//注册组件的另外方式是直接写内容Vue.component('my-html2',{data(){ return {count:1}    },            template:`<button v-on:click="count++">按钮组件2,点击数:{{count}}</button>`});const myhtml3={data(){ return { count:0}   },template:`<button v-on:click="count++">按钮组件3,点击数:{{count}}</button>`}//创建vue对象const vueApp1=new Vue({el:'#demo1',components:{'my-html3':myhtml3}});const vueApp2=new Vue({el:'#demo2',components:{'my-html4':{data(){ return { count:0}   },template:`<button v-on:click="count++">按钮组件4,点击数:{{count}}</button>`                    }}});        </script>
</body>
</html>

  显示结果:

  从上面的代码中可以看出,vue的组件有7种写法之多,实际的编码过程中可以根据个人喜好选择。

  二、组件的嵌套

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue组件的嵌套</title><script src="vue.js"></script>    
</head>
<body><div id="demo" style="background-color: aquamarine;"><my-html1></my-html1><my-html2></my-html2></div><script>//创建组件模板对象const AloneTemplate={template:`<div><p>独立的组件1</p></div>`};Vue.component('my-html1',{data(){ return {count:1}    },            template:`<div><button>按钮组件</button><childcomponet></childcomponet></div>`,components:{'childcomponet':AloneTemplate}});Vue.component('my-html2',{data(){ return {count:1}    },            template:`<div><button>按钮组件</button><childcomponet></childcomponet></div>`,components:{'childcomponet':{template:`<div><p>独立的组件2{{count}}</p></div>`                    }}});//创建vue对象const vueApp1=new Vue({el:'#demo'});</script>
</body>
</html>

  上面是组件嵌套的两种写法。

  三、组件的传值与通信

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue的通信</title><script src="vue.js"></script>    
</head>
<body><div id="demo" style="background-color: aquamarine;"><html-a v-bind:incomepara1="worker" v-bind:incomepara2="program"></html-a></div><script>const AloneTemplate={data(){ return { name:'人员列表' } },template:`<div><p>{{name}}</p><p>人员1:{{ incomepara1.name +"--"+incomepara1.age}}</p><p>人员2:{{incomepara2.name}}--{{incomepara2.age}}</p></div>`,props:['incomepara1','incomepara2']};const vueApp=new Vue({el:'#demo',data:{worker:{    name:"json",age:37    },program:{   name:"sdf",age:31   }},components:{'html-a':AloneTemplate}});</script>
</body>
</html>

  注意:
  1、props是一个数组,它起到桥梁的作用,可以传递多个参数,具体的参数可以是数组、变量名,也可是对象,传递对象就可以传递丰富的参数值。
  2、props可以理解为代理,对于组件而言,通过v-bind让props的参数指向父项的具体参数,对于组件内部就可以直接使用了。
  3、v-bind的绑定时括号里面可以是运算表达式。
  4、参数传递是对象的写法:

props:{age:Number,//表示年龄是数字name:[String,Number],//表示姓名可以是字符串或者数字mydate:{Date,default:'2000-1-1'},arrlist:{type:Array,default:[],required:true    //表示必须输入},myobj}

  下面是一个子组件与父组件相互通信的例子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue Component Communication</title><script src="vue.js"></script>
</head>
<body><script type="text/x-template" id="mycomponent"><div style="background-color: aquamarine;width: 500px;"><h2>这是子组件</h2>姓名:<input type="text" v-model="myPerson.name"/>年龄:<input type="number" v-model="myPerson.age"/><h3>这是来自父组件的信息:我是{{ proxyperson.name }},今年{{proxyperson.age}}岁。</h3><button @click="emitEvent">发送信息给父组件</button></div></script><div id="app" style="background-color: rgb(140, 91, 201);width: 600px;"><h2>这是父组件</h2>姓名:<input type="text" v-model="dawn.name"/>年龄:<input type="number" v-model="dawn.age"/><h3>这是来自子组件的信息:我是{{ childperson.name }},今年{{childperson.age}}岁。</h3><mycomponent :proxyPerson="dawn" @custom-event="handleEvent"></mycomponent> </br></div><script>Vue.component('mycomponent', {data(){return {  myPerson:{  name:'John',age:23  }}},template: '#mycomponent',props: ['proxyperson'],methods: {emitEvent() { this.$emit('custom-event', this.myPerson);  }}});new Vue({el: '#app',data: {dawn:{  name:'SDF',age:35 },childperson:{ name:'',age:0 }},methods: {handleEvent(obj) {  this.childperson=obj; }}});</script>
</body>
</html>

  显示结果:

  父组件中的信息变化与子组件中的信息同步,这是因为prop起到了绑定对象的作用,子组件中的信息变化在点击按钮【发送信息给父组件】后因为对象绑定了,在自定义的事件custom-event中调用了父组件的方法handleEvent(obj),所以也是实时变化同步。

  组件之间的通讯也可以借助父对象来进行,这样父对象起到桥梁的作用,不过这样的方法不值得推荐,特殊的情况下可以使用。

<!DOCTYPE html>
<html>
<head><title>Vue组件间通信示例-人员列表</title><script src="vue.js"></script>
</head>
<body><div id="app"><add-person :mypersons="parentpersonlist"></add-person><person-list :mypersons="parentpersonlist"></person-list></div><!-- 组件 AddPerson --><template id="add-person"><div><label>姓名:</label><input v-model="name" type="text"><button @click="addPerson">增加人员</button></div></template><template id="person-list"><div><ul><li v-for="person in persons">{{ person }}</li></ul></div></template><script>//组件:AddPersonVue.component('add-person', {data(){return {  childpersons:this.mypersons,name:''  }},template: '#add-person',methods: {addPerson() {this.childpersons.push(this.name);this.$emit('update:parentpersonlist');}},props: ['mypersons']});//组件:PersonListVue.component('person-list', {data(){ return { persons:[] } },template: '#person-list',props: ['mypersons'],created() { this.persons=this.mypersons;  }});new Vue({el: '#app',data:{  parentpersonlist:['张1', '李2', '王3']  }});</script>
</body>
</html>

  结果显示:

  2023年一月份的时候学习vue,写了三篇文章,分别是:
  1、Vue组件化编程的基础知识要点
  2、Vue组件化编程的组件通信
  3、三种简洁易行的方法解决基于Vue.js的组件通信

  一年过去了,我都快忘记了,以前是断断续续地学,并没有做个项目,看来学习需要实时跟进并且要加以适当的练习。
  编程就是这样,学会容易,上手也很快,但是不做项目加以巩固,等于没有学!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/419081.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

68. redis计数与限流中incr+expire的坑以及解决办法(Lua+TTL)

文章目录 一、简介二、代码演进第一版代码&#xff08;存在bug隐患&#xff09;第二版代码&#xff08;几乎无隐患&#xff09;第三版代码(完美无瑕&#xff09; 一、简介 在日常工作中&#xff0c;经常会遇到对某种操作进行频次控制或者统计次数的需求&#xff0c;此时常用的…

Git教程学习:09 Git分支

文章目录 1 分支的简介2 分支的相关操作2.1 分支的创建2.2 分支的切换2.3 分支的合并2.4 分支推送到远程2.5 分支的删除2.6 分支的重命名 3 分支开发工作流程3.1 长期分支3.2 短期分支 1 分支的简介 几乎所有的版本控制系统都以某种形式支持分支。使用分支意味着我们可以把我们…

《Linux C编程实战》笔记:信号的捕捉和处理

Linux系统中对信号的处理主要由signal和sigaction函数来完成&#xff0c;另外还会介绍一个函数pause&#xff0c;它可以用来响应任何信号&#xff0c;不过不做任何处理 signal函数 #include <signal.h> void (*signal(int signum, void (*handler)(int)))(int);可以分解…

vue3+vite:封装Svg组件

前言 在项目开发过程中&#xff0c;以svg图片引入时&#xff0c;会遇到当hover态时图片颜色修改的场景&#xff0c;我们可能需要去引入另一张不同颜色的svg图片&#xff0c;或者用css方式修改&#xff0c;为了方便这种情况&#xff0c;需要封装svg组件来自定义宽高和颜色&…

DiffMIC:融合局部和全局分析,基于扩散模型的医学图像分类方法

DiffMIC&#xff1a;基于扩散模型的医学图像分类方法 DiffMIC的核心思想糖尿病视网膜病变分级 网络结构去噪扩散模型&#xff1a;提升特征清晰度双粒度条件引导&#xff08;DCG&#xff09;&#xff1a;融合局部和全局分析条件特定的最大均值差异&#xff08;MMD&#xff09;正…

数据结构与算法教程,数据结构C语言版教程!(第五部分、数组和广义表详解)三

第五部分、数组和广义表详解 数组和广义表&#xff0c;都用于存储逻辑关系为“一对一”的数据。 数组存储结构&#xff0c;99% 的编程语言都包含的存储结构&#xff0c;用于存储不可再分的单一数据&#xff1b;而广义表不同&#xff0c;它还可以存储子广义表。 本章重点从矩阵…

go语言(十一)----面向对象继承

一、面向对象继承 写一个父类 package mainimport "fmt"type Human struct {name stringsex string }func (this *Human) Eat() {fmt.Println("Human.Eat()...") }func (this *Human) Walk() {fmt.Println("Human.Walk()...") }func main() {h…

Spring成长之路—Spring MVC

在分享SpringMVC之前&#xff0c;我们先对MVC有个基本的了解。MVC(Model-View-Controller)指的是一种软件思想&#xff0c;它将软件分为三层&#xff1a;模型层、视图层、控制层 模型层即Model&#xff1a;负责处理具体的业务和封装实体类&#xff0c;我们所知的service层、poj…

安达发|APS工序排程甘特图功能介绍

工序排程甘特图的主要功能 1. 显示工序时间安排&#xff1a;工序排程甘特图可以清晰地展示生产过程中各个工序的开始时间、结束时间和持续时间&#xff0c;从而帮助企业了解生产过程中各个环节的时间安排。 2. 显示工序进度情况&#xff1a;通过工序排程甘特图&#xff0c;企业…

爬虫之Cookie获取:利用浏览器模拟一个cookie出来、面对反爬虫、加密的cookie的应对方法

爬虫之Cookie获取&#xff1a;利用浏览器模拟一个cookie出来、面对反爬虫、加密的cookie的应对方法 在爬虫或模拟请求时&#xff0c;特别是获取验证码的时候&#xff0c;反爬虫的网站的cookie或定期失效&#xff0c;复制出来使用是不行的为了应对这种方式&#xff0c;我们可能…

LLMs的Chain-of-Note(CoN)检索

英文原文地址&#xff1a;https://cobusgreyling.medium.com/chain-of-note-con-retrieval-for-llms-763ead1ae5c5 Chain-of-Note (CoN) 旨在通过解决噪声数据、不相关文档和domain场景来改进 RAG 实现。 2023 年 11 月 17 日 CoN 的要点 CoN 框架由三种不同类型组成&#x…

【JS逆向】某居深圳登陆信息加密逆向分析探索!

某二手房深圳站点的登陆信息加密逆向分析探索&#xff0c;需要分析查找关键的加密位置&#xff0c;位置在前上部分&#xff0c;需要理解一点代码&#xff0c;往上寻找一段代码&#xff0c;加密特征比较明显&#xff0c;找到后即可调试出来&#xff01; 网址&#xff1a; aHR0cH…