0
点赞
收藏
分享

微信扫一扫

VUE学习(十三)、ref属性、props配置项、minix混入、自定义插件、scoped样式

VUE学习(十三)、ref属性、props配置项、minix混入、自定义插件、scoped样式

一、ref属性

<template>
	<div>
		<h1 v-text="msg" ref="title"></h1>
		<button ref="btn" @click="showDOM">点我输出上方的DOM元素</button>
		<School ref="sch"/>
	</div>
</template>

<script>
	//引入School组件
	import School from './components/School'

	export default {
		name:'App',
		components:{School},
		data() {
			return {
				msg:'欢迎学习Vue!'
			}
		},
		methods: {
			showDOM(){
				console.log(this.$refs.title) //真实DOM元素
				console.log(this.$refs.btn) //真实DOM元素
				console.log(this.$refs.sch) //School组件的实例对象(vc)
			}
		},
	}
</script>

二、props配置项

props传入的值尽量在传入后就不要修改了,需要修改的话按照特定方法修改(下有)

配置:

<template>
	<div>
		<h1>{{msg}}</h1>
		<h2>学生姓名:{{name}}</h2>
		<h2>学生性别:{{sex}}</h2>
		<h2>学生年龄:{{myAge+1}}</h2>
		<button @click="updateAge">尝试修改收到的年龄</button>
	</div>
</template>

<script>
	export default {
		name:'Student',
		data() {
			console.log(this)
			return {
				msg:'我是一个xxx大学的学生',
				myAge:this.age //props配置项的数据不能轻易修改,如果真的想修改就用此方法
			}
		},
		methods: {
			updateAge(){
				this.myAge++
			}
		},
		//简单声明接收
		// props:['name','age','sex'] 

		//接收的同时对数据进行类型限制
		/* props:{
			name:String,
			age:Number,
			sex:String
		} */

		// required和default不能同时存在
		//接收的同时对数据:进行类型限制+默认值的指定+必要性的限制
		props:{
			name:{
				type:String, //name的类型是字符串
				required:true, //name是必要的
			},
			age:{
				type:Number,
				default:99 //默认值
			},
			sex:{
				type:String,
				required:true
			}
		}
	}
</script>

使用

<template>
    <div>
        <Student name="李四" sex="女" :age="18" />
    </div>
</template>

<script>
import Student from "./components/Student";

export default {
    name: "App",
    components: { Student }
};
</script>

三、mixin混入(混合)

mixin.js文件

/* 多个组件公有的配置 */
export const hunhe = {
	methods: {
		showName(){
			alert(this.name)
		}
	},
	mounted() {
		console.log('你好啊!')
	},
}
export const hunhe2 = {
	data() {
		return {
			x:100,
			y:200
		}
	},
}
 

局部使用(单组件)

<template>
	<div>
		<h2 @click="showName">学校名称:{{name}}</h2>
		<h2>学校地址:{{address}}</h2>
	</div>
</template>

<script>
	//引入一个hunhe(局部)
	// import {hunhe,hunhe2} from '../mixin'

	export default {
		name:'School',
		data() {
			return {
				name:'xxx大学',
				address:'北京',
				x:666 //数据和混入整合时以当前数据为主,但是生命周期钩子是都要,并且混入里面的钩子先执行
			}
		},
		// 使用一个hunhe(局部)
		// mixins:[hunhe,hunhe2],
	}
</script>

全局(main.js中)

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
// 全局混入
import {hunhe,hunhe2} from './mixin'
//关闭Vue的生产提示
Vue.config.productionTip = false

// 全局混入使用
Vue.mixin(hunhe)
Vue.mixin(hunhe2)


//创建vm
new Vue({
	el:'#app',
	render: h => h(App)
})

四、自定义插件

自定义插件文件pbugin.js

export default {
	install(Vue,x,y,z){
		console.log(x,y,z)
		//全局过滤器
		Vue.filter('mySlice',function(value){
			return value.slice(0,4)
		})

		//定义全局指令
		Vue.directive('fbind',{
			//指令与元素成功绑定时(一上来)
			bind(element,binding){
				element.value = binding.value
			},
			//指令所在元素被插入页面时
			inserted(element,binding){
				element.focus()
			},
			//指令所在的模板被重新解析时
			update(element,binding){
				element.value = binding.value
			}
		})

		//定义混入
		Vue.mixin({
			data() {
				return {
					x:100,
					y:200
				}
			},
		})

		//给Vue原型上添加一个方法(vm和vc就都能用了)
		Vue.prototype.hello = ()=>{alert('你好啊')}
	}
}

使用

<template>
	<div>
		<h2>学生姓名:{{name}}</h2>
		<h2>学生性别:{{sex}}</h2>
		<input type="text" v-fbind:value="name">
	</div>
</template>

<script>
	export default {
		name:'Student',
		data() {
			return {
				name:'张三',
				sex:'男'
			}
		},
	}
</script>
<template>
	<div>
		<h2>学校名称:{{name | mySlice}}</h2>
		<h2>学校地址:{{address}}</h2>
		<button @click="test">点我测试一个hello方法</button>
	</div>
</template>

<script>
	export default {
		name:'School',
		data() {
			return {
				name:'xxx大学atguigu',
				address:'北京',
			}
		},
		methods: {
			test(){
				this.hello()
			}
		},
	}
</script>

五、scoped样式及lang属性指定样式语言

每一个插件的style中的样式最后都会整合,如果多个插件中的样式类名,id等相同,则可能出现样式渲染出错,此时用scoped解决此问题

<style scoped>
	.demo{
		background-color: skyblue;
	}
</style>

当使用lang属性时如果没有less等编译环境需要安装

如果写了lang属性,则一定要赋值,没有则默认css

<style lang="less" scoped>
	.demo{
		background-color: pink;
		.ceshi{
			font-size: 40px;
		}
	}
</style>
举报

相关推荐

0 条评论