1、v-for的优先级比v-if/v-show都大
v-bind也可以绑定自定义的属性
2、父组件向子组件传值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="./node_modules/vue/dist/vue.js"></script> <title>Document</title> </head> <body> <div id="app"> </div> </body> <script> //全局组件 let temp={//必须先声明,然后再在其他组件中使用 template: '<div><div v-for="(item,index) in posts">{{item.name}}</div></div>', data(){ return{ } }, props: ['posts']//里边的变量名务必加引号 } new Vue({ el: '#app',//目的地 template: '<temp :posts="posts"></temp>',//在子组件中进行传值。这里的属性绑定前一个是自定义的属性名,用来在props中做名称使用 data() { return { posts: [{ id: 1, name: 'cc', age: 12 }, { id: 2, name: 'ec', age: 14 }, { id: 3, name: 'tc', age: 15 }] } }, components: { temp } }) </script> </html>
3、子组件向父组件传值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="./node_modules/vue/dist/vue.js"></script> <title>Document</title> </head> <body> <div id="app"> </div> </body> <script> //全局组件 let temp = {//必须先声明,然后再在其他组件中使用 template: '<div><button @click="changeSize">changeSize</button>你好吗猪妹</div>', data() { return { } }, methods: { changeSize() { this.$emit("change", 1) // } } } new Vue({ el: '#app',//目的地 template: `<div :style='{fontSize:size+"em"}'><temp @change="changeHandler"></temp></div>`,//在子组件中进行传值。这里的属性绑定前一个是自定义的属性名,用来在props中做名称使用 data() { return { posts: [{ id: 1, name: 'cc', age: 12 }, { id: 2, name: 'ec', age: 14 }, { id: 3, name: 'tc', age: 15 }], size: 1 } }, components: { temp }, methods: { changeHandler(interval) { this.$data.size += interval } } }) </script> </html>
6、当我们使用公用组件的时候,用于公用组件的某些属性值都是一样的,导致我们在使用时不能按需修改,非常
不方便,这时候我们需要借助vue提供的slot标签,将作为分发内容的出口。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./node_modules/vue/dist/vue.js"></script> </head> <body> <div id="app"></div> </body> <script> let temp={ template: '<div><slot></slot></div>' } new Vue({ el: '#app', template:"<div><temp>天气</temp><temp>运势</temp></div>", data() { return { } }, methods: { }, components:{ temp } }) </script> </html>
也可以如下图般使用:
7、过滤器
1)声明私有过滤器
filters:{
filterName:function(value){
//内部一定要return
}}
2)声明公共过滤器
Vue.filter(filterName,function(value){
return value.toLocaleUpperCase()
})
3)使用过滤器,借助管道符分隔,data就是所需要格式化的数据
{{data|fiter1(args)|filter2(args)}
8、watcher与computed
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./node_modules/vue/dist/vue.js"></script> </head> <body> <div id="app"></div> </body> <script> new Vue({ el: '#app', template: "<div>{{msg}}--------{{msg2}}</div>", data() { return { msg:'天气', arr:[]//无法监听复杂数据类型,需要进行深度监视 } }, watch: {//一般监听单个属性 "msg":function(newV,oldV){//务必加引号 //监听属性变化,一旦改变,触发本事件 //logic } }, computed:{ msg2(){//虽说是函数,但是当做属性来使用 return this.msg+'变化啦'//必须return一个值,该值将作为msg2的值 } }, }) </script> </html>