目录
vue: 父子组件传值
- 父组件向子组件传值:父组件通过向子组件传递参数,子组件通过props来接收参数。
- 子组件向父组件传值:子组件通过this.$emit触发一个父组件监听的事件,这个事件触发则会执行父组件的方法并传递参数到父组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta chartset="UTF-8">
<title>Hello World</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<div :style="{ fontSize: postFontSize + 'em' }">{{content}</div>
<button @click="clickC1">父组件调用子组件的事件</button>
<!-- 父组件通过属性向子组件传递数据 加 : 后面的内容是js表达式-->
<counter :count="0" ref="c1" v-on:enlarge-text="changeSize"></counter>
<counter :count="1"></counter>
</div>
<script>
var counter = {
//子组件接收父组件的参数
props: ['count'],
template: '<div @click="handleClick" >{{number}}</div>',
data: function (){
return {
number: this.count
}
},
methods: {
handleClick: function() {
//警告 不要直接修改父组件传过来的值,如果传过来的是一个引用类型,修改会对其他引用它的地方有影响
//this.count++;
this.number++;
//子组件向父组件传值 触发一个父组件监听的事件,当这个事件触发则执行父组件的方法
this.$emit('enlarge-text',0.1);
}
},
}
var vm = new Vue({
el: '#app',
data: {
content: 'hello world',
postFontSize: 1
},
components: {
counter: counter
},
methods: {
clickC1: function() {
this.$refs.c1.handleClick();
},
changeSize: function(size) {
this.postFontSize += size;
}
}
})
</script>
</body>
</html>
vue: 插槽
将元素插入到组件的标签中间,可以根据插槽的名字来匹配。使用插槽可以改变组件内的元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta chartset="UTF-8">
<title>Hello World</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<child content="<p>world</p>">
<p>插槽</p>
<h1 slot="header">有名字的插槽</h1>
</child>
</div>
<script>
Vue.component('child',{
props: ['content'],
template: `<div>
<p>hello</p>
<div v-html="this.content"></div>
<slot>默认内容</slot>
<slot name='header'></slot>
</div>`
})
var vm = new Vue({
el: '#app',
})
</script>
</body>
</html>
vue: 作用域插槽
父组件调用子组件给子组件传了一个插槽 ,这个插槽叫作用域插槽。 这个插槽必须是template开头结尾 。声明从子组件中声明的数据放在props中 子组件绑定了item传过来
<body>
<div id="app">
<child>
<template slot-scope="props">
<li>{{props.item}}</li>
</template>
</child>
</div>
<script>
Vue.component('child',{
props: ['content'],
template: `<div>
<ul>
<slot v-for="item of list" :item=item>
</slot>
</ul>
</div>`,
data: function() {
return {
list: [1,2,3,4,5]
}
}
})
var vm = new Vue({
el: '#app',
})
</script>
</body>
vue:动态组件和v-once指令
使用component标签可以动态的显示组件
v-once指令可以使组件在切换时不会销毁 ,放在内存中,提高性能
<!DOCTYPE html>
<html lang="en">
<head>
<meta chartset="UTF-8">
<title>Hello World</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<child>
<button @click="handleClick">change</button>
<component :is="type"></component>
</child>
</div>
<script>
//使用v-once 组件在切换时不会销毁 会放到内存中 下回拿出来 性能高一些
Vue.component('child-one',{
template: `<div v-once>child-one</div>`,
})
Vue.component('child-two',{
template: `<div v-once>child-two</div>`,
})
var vm = new Vue({
el: '#app',
data: {
type: 'child-one'
},
methods: {
handleClick: function() {
this.type = this.type === 'child-one' ? 'child-two' : 'child-one';
}
}
})
</script>
</body>
</html>
vue: 给组件绑定原生事件
如果组件已经内已经绑定了原生事件,那么在组件上直接绑定原生事件是不起作用的,需要使用如 @click.native 来绑定原生事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta chartset="UTF-8">
<title>Hello World</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<child @click1="handleClick" @click.native="nativeClick"></child>
</div>
<script>
Vue.component('child',{
template: '<div @click="childClick">child</div>',
methods: {
childClick: function() {
//自定义click1
this.$emit('click1');
}
}
})
var vm = new Vue({
el: '#app',
methods: {
handleClick: function() {
alert(22);
},
nativeClick: function(){
alert("点击组件触发原生事件")
}
}
})
</script>
</body>
</html>
vue: 计算属性、方法、侦听器
<!DOCTYPE html>
<!-- 计算属性、方法、侦听器 -->
<html>
<head>
<meta chartset="UTF-8">
<title>计算属性、方法、侦听器</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
{{fullName}}
<!-- 使用方法 -->
{{fullName2()}}
{{full}}
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
firstName: "jx",
lastName: "g",
age: 18,
full: 'full'
},
//计算属性 缓存:当计算属性依赖的值没有变化时,计算属性会取之前的值
computed: {
fullName: function() {
console.log("fullName");
return this.firstName + " " + this.lastName;
}
},
methods: {
fullName2: function() {
console.log("fullName2");
return this.firstName + " " + this.lastName;
}
},
//使用侦听器
watch: {
firstName: function(){
this.full = this.firstName + " " + this.lastName;
}
},
})
</script>
</body>
</html>
vue: 计算属性set 、get
<!DOCTYPE html>
<html>
<head>
<meta chartset="UTF-8">
<title>计算属性、方法、侦听器</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
{{fullName}}
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
firstName: "jx",
lastName: "g",
age: 18,
full: 'full'
},
//计算属性 缓存:当计算属性依赖的值没有变化时,计算属性会取之前的值
computed: {
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function(value) {
var arr = value.split(" ");
this.firstName = arr[0];
this.lastName = arr[1]; }
}
}
})
</script>
</body>
</html>
vue: ref
ref 可以获得组件或dom元素
<div ref="hello" @click="handleClick">hello world</div>
<counter ref="one" @change="handleChange"></counter>
<counter ref="two" @change="handleChange"></counter>
this.$refs.hello.innerHTML
this.total = this.$refs.one.number + this.$refs.two.number;
vue: 对象添加属性
根据官方文档定义:如果在实例创建之后添加新的属性到实例上,它不会触发视图更新。
使用$set 来更新
this.$set(this.obj,'e',02)
vue: transition结合keep-alive
keep-alive可以达到第二次进入页面缓存页面而不重新加载的效果,结合transition时使用
<transition name="fade-transform" mode="out-in">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
</transition>
<transition name="fade-transform" mode="out-in">
<router-view v-if="!$route.meta.keepAlive"></router-view>
</transition>