点击事件和v-if(v-show)的组合使用
<template><div class="aBody"><span class="title">我,子组件。</span><!-- 通过v-if或v-show的方式让标签消失出现 --><span class="info" v-show="isShow">{{title}}</span><span class="info" v-if="isShow">{{title}}</span><!-- 给btn添加点击事件 @ 是 v-on: 的语法糖--><button class="btn" @click="changeInfo">展示info</button></div></template><script>export default {data(){// 必须有返回值,且返回一个对象// 返回的是一个vue的对象,所以里面的变量可以是用, this.xxx获取return {title:'',isShow:false}},methods:{changeInfo:function() {this.title = '啊被发现了吗! ,,ԾㅂԾ,,';// 点击后将isShow取反后的值赋值给他自己,// 以实现通过点击然一个标签消失,出现this.isShow = !this.isShow;}}}</script><style>.btn {width: 100px;}</style>
slot插槽
通过使用插槽可以在子组件中写入信息,如下:
<JuniorSon @results = "accept" class="son" :index = 'infos' item = 'haha'><span>{{something}}</span><span v-show="isShow">son发来贺电:{{toBStr}}</span><button class="f-btn">点击发送信息给son</button></JuniorSon>
在子组件中通过slot接收上面JuniorSon标签中的信息,否则JuniorSon标签中的信息就不会显示
slot所在位置就是JuniorSon标签中的信息所在位置
<template><div class="aBody"><!-- 插槽让组件的标签可以些内容,这些内容将放到slot中 --><slot></slot></div></template>
计算函数
模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护。
通过计算函数,减少标签中的逻辑性代码
计算函数都放在computed中,并且计算函数不能使用箭头函数(除非你不使用this)
如下计算函数toBStr,在使用时直接使用函数名即可
<JuniorSon @results = "accept" class="son" :index = 'infos' item = 'haha'><span>{{something}}</span><span v-show="isShow">son发来贺电:{{toBStr}}</span><button class="f-btn">点击发送信息给son</button></JuniorSon>export default {components:{JuniorSon},data() {return {something:'我是你爸爸',infos:'我是你爸爸,我有钱。哎╮(╯_╰)╭',// 存放son传递过来的数据sonData:[],isShow:false}},methods:{// 通过自定义事件接收juniorSon发送的信息accept:function(event) {// 打印信息console.log(event,arguments)// 将信息存入sonDataconst list = Array.from(arguments);list.forEach(item => {this.sonData.push(item);})// 接收并显示信息this.isShow = true}},computed:{toBStr:function(){let str = '';this.sonData.forEach(item => {str += item + ' ';})return str;}}}
监听函数
监听函数一般放在watch中
函数名就是需要监听的变量名,
参数1—变化后的值
参数2—变化前的值
watch:{isShow:function(newData,oldData) {console.log(newData,oldData);}}
组件的生命周期
生命周期函数
beforeCreate(){}
created(){}
beforeMount(){}
mounted(){}
beforeUpdate(){}
updated(){}
beforeDestroy(){}
destroyed(){}
生命周期的4个阶段
生命周期函数的使用
export default {beforeCreate(){console.log('实例化前')},created(){console.log('完成实例')},beforeMount(){console.log('挂载前')},mounted(){console.log('完成挂载')},beforeUpdate(){console.log('数据更新,视图未更新')},updated(){console.log('数据更新,视图更新')},beforeDestroy(){console.log('销毁前')},destroyed(){console.log('销毁完成')}}
双向绑定
v-model
通过向input中输入值,v-model将输入的值保存到message中,
<div class="cla">{{message}}</div><input type="text" v-model="message">
请求接口的时机
created(){}中请求接口
接口的请求是异步的(请求的时间不确定),解决
1.默认值
2.loading
数据没有加载完就使用默认的数据(loading显示),加载完成后就使用真正的数据(loading隐藏)
数据没有加载完就只能看见loading的加载页面
父组件,子组件传值
父组件传值子组件
1.在父组件中先引入子组件,并注册。
2.然后就可以通过给组件JuniorSon设置动态绑定的属性,或静态属性,将数据传递给子组件
<!-- : 是v-bind: 的语法糖 使用后属性就可以直接使用变量了 --><!-- 如果传递给子组件的信息是自定义属性,这样子组件的props不能获取到传递的信息,但可以通过this.$attrs获取到自定义属性信息 --><JuniorSon @results = "accept" class="son" :index = 'infos' item = 'haha'><span>{{something}}</span><span v-show="isShow">son发来贺电:{{toBStr}}</span><button class="f-btn">点击发送信息给son</button></JuniorSon>
3.子组件通过props接收父组件传过来的信息
4.接收完毕后就可以使用了,用法和data中的变量一样
this.$props指向vue实例的props
this.props指向data中的props
this.$attrs可以获取到父组件传递过来的自定义属性的信息(如果没有在props中接收,那么就可以在attrs中找到未被接收的信息,如果props将数据全部接收那么,attrs中就没有可用的数据)
props有两种接收信息的方式
props: ["index","item"]//通过数组接收信息,变量名就是前面的自定义属性名(不能改变)
props:{index:{type:String,required:true},item:{type:String,required:true}},//通过对象接收信息,这样可以设置每一个变量的类型等。
子组件接收数据注意事项
子组件接受的父组件的值分为——引用类型和普通类型两种,
1.普通类型:字符串(String)、数字(Number)、布尔值(Boolean)、空(Null)
2.引用类型:数组(Array)、对象(Object)
其中,普通类型是可以在子组件中更改,不会影响其他兄弟子组件内同样调用的来自父组件的值,
但是,引用类型的值,当在子组件中修改后,父组件的也会修改,
那么后果就是,其他同样引用了改值的子组件内部的值也会跟着被修改。
除非你有特殊的要求这么去做,否则最好不要这么做。
父组件传给子组件的值,在子组件中千万不能修改,因其数据是公用的,改了所有引用的子组件就都改了。
子组件传递数据给父组件
子组件通过this.$emit()将数据传给父组件
// 参数1---父子间中,使用子组件时定义的自定义事件的事件处理函数// 参数2,3,4---传递参数this.$emit('results','你是个大帅逼','没错说的就是你','big SB',)
父组件通过JuniorSon上的自定义事件的事件处理函数,接收并处理数据
<!-- 如果传递给子组件的信息是自定义属性,这样子组件的props不能获取到传递的信息,但可以通过this.$attrs获取到自定义属性信息 --><JuniorSon @results = "accept" class="son" :index = 'infos' item = 'haha'><span>{{something}}</span><span v-show="isShow">son发来贺电:{{toBStr}}</span><button class="f-btn">点击发送信息给son</button></JuniorSon>
兄弟组件互传信息
其中一个兄弟组件将信息先传递给父组件,再通过父组件将信息传递给另有一个兄弟组件
父组件
<template><!-- : 是v-bind: 的语法糖 使用后属性就可以直接使用变量了 --><!-- 传递给子组件的信息不能是自定义属性,这样子组件的props不能获取到传递的信息,但可以通过this.$attrs获取到自定义属性信息 --><JuniorSon @results = "accept" class="son" :index = 'infos' item = 'haha'><span>{{something}}</span><span v-show="isShow">son发来贺电:{{toBStr}}</span><button class="f-btn">点击发送信息给son</button></JuniorSon></template><script>import JuniorSon from './JuniorSon.vue';export default {components:{JuniorSon},data() {return {something:'我是你爸爸',infos:'我是你爸爸,我有钱。哎╮(╯_╰)╭',// 存放son传递过来的数据sonData:[],isShow:false}},methods:{// 通过自定义事件接收juniorSon发送的信息accept:function(event) {// 打印信息console.log(event,arguments)// 将信息存入sonDataconst list = Array.from(arguments);list.forEach(item => {this.sonData.push(item);})// 接收并显示信息this.isShow = true}},computed:{toBStr:function(){let str = '';this.sonData.forEach(item => {str += item + ' ';})return str;}}}</script><style>/* 设置组件的样式 */.son {width: 100%;display: flex;flex-direction: column;align-content: center;justify-content: center;}span {width: 200px;}.f-btn {margin: 20px 0;width: 200px;}</style>
子组件
<template><div class="aBody"><!-- 插槽让组件的标签可以些内容,这些内容将放到slot中 --><slot></slot><span class="title">我,子组件。</span><!-- 通过v-if或v-show的方式让标签消失出现 --><!-- 前者是删除,后者是隐藏 --><span class="info" v-show="isShow">{{title}}</span><span class="info" v-if="isShow">father发来亲切的问候:{{index}}</span><!-- 给btn添加点击事件 @ 是 v-on: 的语法糖--><button class="btn" @click="changeInfo">展示info</button><button class="f-btn" @click="sendInfoToFather">点击发送信息给father</button></div></template><script>export default {//// props: ["index","item"],//props:{index:{type:String,required:true},item:{type:String,required:true}},data(){// 必须有返回值,且返回一个对象// 返回的是一个vue的对象,所以里面的变量可以是用, this.xxx获取return {title:'',isShow:false}},methods:{changeInfo:function() {this.title = '啊被发现了吗! ,,ԾㅂԾ,,';// 点击后将isShow取反后的值赋值给他自己,以实现通过点击然一个标签消失,出现this.isShow = !this.isShow;// 获取信息console.log(this.$attrs)console.log(this.$props)console.log(this.index,this.item)},// 将信息发送给SeniorFathersendInfoToFather: function() {// 参数1---父子间中,使用子组件时定义的自定义事件的事件处理函数// 参数2,3,4---传递参数this.$emit('results','你是个大帅逼','没错说的就是你','big SB',)}}}</script><style>.info ,.title {width: 200px;}.btn {width: 200px;}.f-btn {width: 200px;margin-top: 20px;}</style>
