Vue进阶 1.全局组件和局部组件 2..父组件和子组件 3.组件通信-父传子 4.组件通信-子传父 5.组件通信结合双向绑定 6.Vue实现一个简单计算器 7.Vue实现全选和反选
1.全局组件和局部组件
< html> < head> < meta charset = " utf-8" > < title> title> head> < body> < div id = " app" > < com> com> < com> com> < jubu> jubu> div> < div id = " app2" > < com> com> //报错无法显示,局部组件无法在其它Vue实例中使用 div> < template id = " qj" > < button @click = " count++" > 点了{{count}}次 button> template> < template id = " jb" > < h1> 自定义局部组件! h1> template> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > Vue. component ( "com" , { template: '#qj' , data ( ) { return { count: 0 } } } ) ; const jubu = Vue. extend ( { template: '#jb' } ) ; const vue = new Vue ( { el: "#app" , components: { 'jubu' : jubu} } ) ; const vue2 = new Vue ( { el: '#app2' } ) ; script> body>
html>
2…父组件和子组件
< html> < head> < meta charset = " utf-8" > < title> title> head> < body> < div id = " app" > < parent> parent> < parent> parent> div> < template id = " zi" > < h1> 子组件 h1> template> < template id = " fu" > < div> < h2> 父组件 h2> < child> child> div> template> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const con1 = Vue. extend ( { template: '#zi' } ) ; const con2 = Vue. extend ( { template: '#fu' , components: { child: con1} } ) ; const app = new Vue ( { el: '#app' , components: { parent: con2} } ) script> body>
html>
3.组件通信-父传子
< html> < head> < meta charset = " utf-8" > < title> title> head> < body> < div id = " app" > < parent> parent> div> < template id = " child" > < ul> < li v-for = " (value,key) in x" > {{key}}={{value}}--{{y}} li> ul> template> < template id = " parent" > < div> {{attr1}} < h2> 我是父组件 h2> < child :x = " attr1" :y = " count" > child> div> template> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const child = Vue. extend ( { template: '#child' , props: { x: { type: [ String, Object] } , y: { type: Number} } } ) ; const parent = Vue. extend ( { template: '#parent' , data ( ) { return { count: 0 } } , props: { attr1: { type: [ String, Number, Object] , default : function ( ) { return { id: '001' , name: 'zs' } ; } } } , components: { 'child' : child} } ) ; new Vue ( { el: '#app' , data: { message: 'i am hahah' } , components: { 'parent' : parent} } ) script> body>
html>
4.组件通信-子传父
< html> < head> < meta charset = " utf-8" > < title> title> head> < body> < div id = " app" > 总价格: {{totalprice}}< br> < parent @asc = " lasc" @desc = " ldesc" > parent> < parent @asc = " lasc" @desc = " ldesc" > parent> div> < template id = " child" > < div> < button @click = " increment" > + button> < button @click = " decrement" > - button> div> template> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const child = Vue. extend ( { template: '#child' , data: function ( ) { return { counter: 0 } } , props: { } , methods: { increment: function ( ) { this . counter++ ; this . $emit ( 'asc' , this . counter) } , decrement: function ( ) { this . counter-- ; this . $emit ( 'desc' , this . counter) } } } ) ; const app = new Vue ( { el: '#app' , data: { totalprice: 200 } , components: { parent: child} , methods: { lasc: function ( value) { console. log ( value+ '+++++++++++++' ) ; this . totalprice += value; } , ldesc: function ( value) { console. log ( value+ '-------------' ) ; this . totalprice -= value; } } } ) ; script> body>
html>
5.组件通信结合双向绑定
< html> < head> < meta charset = " utf-8" > < title> title> head> < body> < div id = " app" > < h1> PARENT-NUM1===={{num1}} h1> < h1> PARENT-NUM2===={{num2}} h1> < parent :attr1 = " num1" :attr2 = " num2" @first = " firsttop" @second = " secondtop" > parent> div> < template id = " child" > < div> < h1> CHILD1===={{attrfirst}} h1> < input type = " text" :value = " attrfirst" @input = " add1" /> < br> < h2> CHILD2===={{attrsecond}} h2> < input type = " text" :value = " attrsecond" @input = " add2" /> div> template> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const child = Vue. extend ( { template: '#child' , props: { attr1: { type: Number, required: true } , attr2: { type: Number, required: true } } , data ( ) { return { attrfirst: this . attr1, attrsecond: this . attr2} } , methods: { add1: function ( evnet) { this . attrfirst = event. target. value; this . $emit ( 'first' , this . attrfirst) ; this . attrsecond = this . attrfirst* 100 ; this . $emit ( 'second' , this . attrsecond) ; } , add2: function ( event) { this . attrsecond = event. target. value; this . $emit ( 'second' , this . attrsecond) ; this . attrfirst = this . attrsecond/ 100 ; this . $emit ( 'first' , this . attrfirst) ; } } } ) ; const app = new Vue ( { el: '#app' , data: { num1: 1 , num2: 2 } , components: { parent: child} , methods: { firsttop: function ( value) { this . num1 = parseFloat ( value) ; } , secondtop: function ( value) { this . num2 = parseFloat ( value) ; } } } ) script> body>
html>
6.Vue实现一个简单计算器
< html> < head> < meta charset = " utf-8" > head> < body> < div id = " app" > < input type = " text" v-model = " num1" /> < select v-model = " oper" > + option> < option value = " -" > - option> < option value = " *" > * option> < option value = " /" > / option> select> < input type = " text" v-model = " num2" /> < input type = " button" value = " =" @click = " eval01" /> < input type = " text" v-model = " num3" /> div> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const vue = new Vue ( { el: "#app" , data: { num1: 0 , oper: "+" , num2: 0 , num3: 0 } , methods: { eval01 ( ) { this . num3= eval ( this . num1+ this . oper+ this . num2) } } } ) script> body>
html>
7.Vue实现全选和反选
< html> < head> < meta charset = " utf-8" > < title> title> head> < body > < div id = " app" > < h1 style =" color : royalblue; " > v-bind和@input实现全选反选 h1> < br> < h1 style =" color : #4476A7; " > {{ck}} h1> < input id = " ck01" type = " checkbox" :checked = " ck[0]" @input = " change(0)" /> < label for = " ck01" > 语文 label> < br> < input id = " ck02" type = " checkbox" :checked = " ck[1]" @input = " change(1)" /> < label for = " ck02" > 数学 label> < br> < input id = " ck03" type = " checkbox" :checked = " ck[2]" @input = " change(2)" /> < label for = " ck03" > 英语 label> < br> < input id = " ck04" type = " checkbox" :checked = " ck[3]" @input = " change(3)" /> < label for = " ck04" > 生物 label> < br> < input type = " button" value = " 全选" @click = " qx" /> < input type = " button" value = " 反选" @click = " fx" /> < br> div> < script src = " js/vue.js" type = " text/javascript" charset = " utf-8" > script> < script type = " text/javascript" > const app = new Vue ( { el: '#app' , data: { ck: [ false , false , false , false ] , flag: true } , methods: { qx: function ( ) { if ( this . flag) { for ( let index in this . ck) { this . ck. splice ( index, 1 , true ) } } else { for ( let index in this . ck) { this . ck. splice ( index, 1 , false ) } } this . flag = ! this . flag; } , fx: function ( ) { for ( let index in this . ck) { this . ck. splice ( index, 1 , ! this . ck[ index] ) ; } } , change: function ( index) { this . ck. splice ( index, 1 , ! this . ck[ index] ) ; } } } ) script> body>
html>
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】 进行投诉反馈!