• Vue2高级-scoped样式、组件自定义事件



    一、Scoped样式

    1. 作用:让样式在局部生效,防止冲突。
    2. 写法:<style scoped>
    <template>
        <div class="demo">
            <h2 class="title">学生姓名:{{ name }}</h2>
            <h2 class="atguigu">学生性别:{{ sex }}</h2>
        </div>
    </template>
    
    <script>
    export default {
        name: 'Student',
        data() {
            return {
                name: '张三',
                sex: '男'
            }
        }
    }
    </script>
    <!-- lang可以设置style的语言 -->
    <style lang="less" scoped>
    .demo {
        background-color: pink;
    
        .atguigu {
            font-size: 40px;
        }
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    二、组件的自定义事件

    1. 一种组件间通信的方式,适用于:子组件 ===> 父组件

    2. 使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)。

    3. 绑定自定义事件:

      1. 第一种方式,在父组件中:<Demo @atguigu="test"/><Demo v-on:atguigu="test"/>

      2. 第二种方式,在父组件中:

        <Demo ref="demo"/>
        ......
        mounted(){
           this.$refs.xxx.$on('atguigu',this.test)
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
      3. 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法。

    4. 触发自定义事件:this.$emit('atguigu',数据)

    5. 解绑自定义事件

      1. this.$off('atguigu') 解绑一个
      2. this.$off([事件名,事件名]) 解绑多个
      3. this.$off() 解绑所有
    6. 组件上也可以绑定原生DOM事件,需要使用native修饰符。

    7. 注意:通过this.$refs.xxx.$on('atguigu',回调)绑定自定义事件时,回调要么配置在methods中要么用箭头函数,否则this指向会出问题!

    • 代码示例:给组件绑定自定义事件,通过$emit触发

    App.vue

    <template>
    	<div class="app">
    		<h1>{{msg}},学生姓名是:{{studentName}}</h1>
    
    		<!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或v-on) -->
    		<Student @atguigu="getStudentName"/>
    
    		<!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法,使用ref) -->
    		<!-- Vue2中,当需要在组件本身添加Vue原生事件时,需要在事件后加.native修饰符 -->
    		<Student ref="student" @click.native="show"/> 
    	</div>
    </template>
    
    <script>
    	import Student from './components/Student'
    
    	export default {
    		name:'App',
    		components:{Student},
    		data() {
    			return {
    				msg:'你好啊!',
    				studentName:''
    			}
    		},
    		methods: {
    			getSchoolName(name){
    				console.log('App收到了学校名:',name)
    			},
    			show(){
    				alert(123)
    			}
    		},
    		mounted() {
    			
    			// 绑定自定义事件 (第一种写法,需要methods定义方法,推荐!!!)
    			// 直接通过refs获取student的vc对象,通过$on绑定事件。
    			this.$refs.student.$on('atguigu',this.getStudentName) 
    
    			// 绑定自定义事件 (第二种写法,无需在methods定义方法)
    			// function的this指向为被调用的组件,()=>箭头函数没有this指向,默认向外查找this
    			this.$refs.student.$on('atguigu2',function (name,...params) {  
    				// 当使用function函数体时,函数体内的this指向是"谁调用就是谁",atguigu2是Student组件调用,所以this为Student的vc对象
    				console.log('App收到了学生名:',name,params)
    				this.studentName = name
    			})
    			
    			// 绑定自定义事件(一次性写法)
    			this.$refs.student.$once('atguigu3',this.getStudentName) 
    		},
    	}
    </script>
    
    <style scoped>
    	.app{
    		background-color: gray;
    		padding: 5px;
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    Student.vue

    <template>
    	<div class="student">
    		<button @click="sendStudentlName">把学生名给App</button>
    		<button @click="unbind">解绑atguigu事件</button>
    		<button @click="death">销毁当前Student组件的实例(vc)</button>
    	</div>
    </template>
    
    <script>
    	export default {
    		name:'Student',
    		data() {
    			return {
    				name:'张三',
    			}
    		},
    		methods: {
    			sendStudentlName(){
    				//触发Student组件实例身上的atguigu事件,第二个参数以后都为传参。
    				this.$emit('atguigu',this.name,666,888,900)
    			},
    			unbind(){
    				this.$off('atguigu') //解绑一个自定义事件
    				// this.$off(['atguigu','demo']) //解绑多个自定义事件
    				// this.$off() //解绑所有的自定义事件
    			},
    			death(){
    				// 销毁了当前Student组件的实例,销毁后所有Student实例的自定义事件全都不奏效。
    				this.$destroy() 
    			}
    		},
    	}
    </script>
    
    <style lang="less" scoped>
    	.student{
    		background-color: pink;
    		padding: 5px;
    		margin-top: 30px;
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 代码示例:通过props读取方法,触发自定义事件

    App.vue

    <template>
    	<div class="app">
    		<h1>{{msg}},学生姓名是:{{studentName}}</h1>
    		<!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
    		<School :getSchoolName="getSchoolName"/>
    	</div>
    </template>
    
    <script>
    	import Student from './components/School'
    
    	export default {
    		name:'App',
    		components:{School},
    		data() {
    			return {
    				msg:'你好啊!',
    				studentName:''
    			}
    		},
    		methods: {
    			getSchoolName(name){
    				console.log('App收到了学校名:',name)
    			},
    		}
    	}
    </script>
    
    <style scoped>
    	.app{
    		background-color: gray;
    		padding: 5px;
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    School.vue

    <template>
    	<div class="school">
    		<button @click="sendSchoolName">把学校名给App</button>
    	</div>
    </template>
    
    <script>
    	export default {
    		name:'School',
    		props:['getSchoolName'],
    		data() {
    			return {
    				name:'阿伟',
    			}
    		},
    		methods: {
    			sendSchoolName(){
    				this.getSchoolName(this.name)
    			}
    		},
    	}
    </script>
    
    <style scoped>
    	.school{
    		background-color: skyblue;
    		padding: 5px;
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    源代码出处:尚硅谷Vue2.0+Vue3.0全套教程丨vuejs从入门到精通

  • 相关阅读:
    网络安全(黑客)自学
    猿创征文|Android 11.0 Launcher3 时钟动态图标的定制化
    外泌体化合物库
    Flutter开发 - iconfont妙用(手把手教程)
    论文翻译:多延迟块频域自适应滤波器
    vscode 根据 ESLint 规范自动格式化代码
    从零开始的C++(十六)
    专注于元宇宙实际应用方案的企业
    计算机网络之传输层 + 应用层
    SSM - Springboot - MyBatis-Plus 全栈体系(十五)
  • 原文地址:https://blog.csdn.net/qq_33399435/article/details/125624394