《Vue进阶》

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实例中使用	Vue.component("com",{template:'#qj',//组件之间的变量互不影响,data必须是一个函数data(){return{count:0//count的初始值}}});//局部组件,只能在自己的Vue实例中使用const jubu = Vue.extend({template: '#jb'});//Vue实例1const vue = new Vue({el:"#app",components:{'jubu':jubu}});//Vue实例2const 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}});//root组件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>{count}}-->{{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',//返回一个count属性到子组件data(){return   {count:0}},props:{attr1:{type:[String,Number,Object],//当不写attr1这个属性的时候会默认传递一个对象进子组件即会触发default:function(){return {id:'001',name:'zs'};}}},components:{//注册子组件'child':child}});//根组件Vue实例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(){//每个子组件都维护自己的counter变量,这就是返回函数的意义return{counter:0}},props:{},methods:{increment:function(){this.counter++;//$emit表示发送给父组件的asc事件,并带上counter值this.$emit('asc',this.counter)},decrement:function(){this.counter--;this.$emit('desc',this.counter)}}});//父组件Vue实例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{//使用父组件传递过来的attr1值进行初始化attrfirst:this.attr1,//使用父组件传递过来的attr2值进行初始化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">

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>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部