组件相关

一.组件基础

1.全局组件

<div  class='nr'>
      <my-component><.my-conmponent>    
</div>
<script  scr:"js链接"></script>
<script>
      Vue.component('my-component',{        ('my-component' 引号中的内容由自己定,但格式必须为‘xx-xx’)
            template:`
                        <li>这是组件部分</li>
            `
      })
      new Vue({
            el:".nr"
      })
</script>

2.局部组件

<div  class='nr'>
      <my-component><.my-conmponent>    
</div>
<script  scr:"js链接"></script>
<script>
      new Vue({
            el:".nr",
            components:{
                  "my-component":{
                        template:`
                              <li>这是组件部分</li>
                        `
                  }
            }
      })

二.组件进阶

1.组件中加入点击事件(点击按钮页面弹出数字“123456”)

<div  class='nr'>
      <my-first><.my-first>    
</div>
<script  scr:"js链接"></script>
<script>
      Vue.component('my-first',{
            template:`
                  <div>
                        <li>{{msg}}</li>
                        <button @click='fun'>按钮</button>
                  </div>
            `,
            data:function(){
                  return{
                        msg:'这是一个组件'
                  }
            },
            methods:{
                  fun:function(){
                        alert(123456)
                  }
            }
      })
      new Vue({
            el:'.nr'
      })
</script>

2.元素属性--父传子(属性传递)

<div  class='nr'>
      <my-one><.my-one>    
</div>
<script  scr:"js链接"></script>
<script>
      Vue.component('my-one',{
            template:`
                  <div>
                        <h1>这是my-one标签</h1>
                        <my-two  v-bind:msg='mas'></my-two>
                  </div>
            `,
            data:function(){
                  return{
                        mas:'这是一个p标签'
                  }
            }
      }),
      Vue.component('my-two',{
            props:['msg'],           (‘props’是父传子组件的关键属性)
            template:`
                  <div>
                        <h2>这是my-two标题</h2>
                        <p>{{msg}}</p>
                  </div>
            `
      })
      new Vue({
            el:'.nr'
      })
</script>

3.元素属性--子传父(事件传递)

<div  class='nr'>
      <my-first><.my-first>    
</div>
<script  scr:"js链接"></script>
<script>
      Vue.component('my-first',{
            template:`
                  <div>
                        <h1>{{asd}}</h1>
                        <my-second  @chuan="di"></my-second>
                  </div>
            `,
            data:function(){
                  return{
                        asd:''
                  }
            },
            methods:{
                  di:function(text){
                        this.asd=text
                  }
            }
      });
      Vue.component('my-second',{
            template:`
                  <button @click='fun'>传递</button>
            `,
            data:function(){
                  return{
                        txt:'组件子传父'
                  }
            },
            methods:{
                  fun:function(){
                        this.$emit('chuan',this.txt)
                      (‘’,this._  引号中为自定义的函数名)
                  }
            }
      })
      new Vue({
            el:'.nr'
      })
</script>

4.元素属性--非父子传递(同级传递)

<div  class='nr'>
      <my-first><.my-first>    
      <my-one></my-one>
</div>
<script  scr:"js链接"></script>
<script>
      var low=new Vue();
      Vue.component('my-first',{
            template:`
                  <div>
                        <h1>这是first事件</h1>
                        <button @click="fun">传递</button>
                  </div>
            `,
            data:function(){
                  return{
                        txt:"将内容传给one"
                  }
            },
            methods:{
                  fun:function(){
                        low.$emit('show',this.txt)
                  }
            }
      });
      Vue.component('my-one',{
            template:`
                  <div>
                        <h1>这是one组件</h1>
                        <p>{{text}}</p>
                  </div>
            `,
            data:function(){
                  return{
                        text:''
                  }
            },
            mounted:function(){
                  low.$on('show',txt=>{
                        this.text=txt
                  })
            }
      });
      new Vue({
            el:'.nr'
      })
</script>

练习1.添加列表项

<div class="nr">
    <my-first></my-first>
</div>
<script src="js文档/vue.js"></script>
<script>
      Vue.component('my-first',{
            template:`
                  <div>
                        <input type="text" v-model="kong">
                        <button @click="fun">添加</button>
                        <my-second v-bind:txt="arr"></my-second>
                  </div>
            `,
            data:function(){
                  return{
                      arr:['吃饭','睡觉','打豆豆'],
                      kong:''
                  }
            },
            methods:{
                  fun:function(){
                        this.arr.push(this.kong);
                        this.kong=''
                  }
            }
      });
      Vue.component('my-second',{
            props:['txt'],
            template:`
                  <ul>
                        <li v-for="(value,index) in txt">
                              {{value}}<button @click="shan(index)">删除</button>
                        </li>
                  </ul>
            `,
            methods:{
                 shan:function(ind){
                       this.txt.splice(ind,1)
                 }
            }
      });
      new Vue({
            el:'.nr'
      })
</script>

练习2.计算总价

<div class="nr">
    <my-first></my-first>
</div>
<script src="js文档/vue.js"></script>
<script>
      Vue.component('my-first',{
            template:`
                  <my-second v-bind:list="arr" v-bind:zj="he"></my-second>
            `,
            data:function(){
                  return{
                        arr:[
                              {name:'香蕉',price:1,count:0,sub:0},
                              {name:'苹果',price:2,count:0,sub:0},
                              {name:'梨',price:3,count:0,sub:0}
                        ],
                        he:0
                  }
            }
      });
      Vue.component('my-second',{
            props:['list','zj'],
            template:`
                  <div>
                        <table class='table table-bordered text-center'>
                              <thead>
                                    <tr>
                                          <th>编号</th>
                                          <th>品名</th>
                                          <th>单价</th>
                                          <th>数量</th>
                                          <th>小计</th>
                                    </tr>
                              </thead>
                              <tbody>
                                    <tr>
                                          <td>{{index+1}}</td>
                                          <td>{{fruit.name}}</td>
                                          <td>{{fruit.price}}</td>
                                          <td>
                                                <button @click="jian(index)">-</button>
                                                <span>{{fruit.count}}</span>
                                                <button @click="jia(index)">+</button>
                                          </td>
                                          <td>{{fruit.sub}}</td>
                                    </tr>
                                    <tr>
                                          <td colspan="5">总价:{{zj}}元</td>
                                    </tr>
                              </tbody>
                        </table>
                  </div>
            `,
            methods:{
                  jian:function(ind){
                        if(this.list[ind].count>0){
                              this.list[ind].count--;
                        }
                        this.list[ind].sub=this.list[ind].price*this.list[ind].count;
                        this.zong()
                  },
                  jia:function(ind){
                        this.list[ind].count++;
                        this.list[ind].sub=this.list[ind].price*this.list[ind].count;
                        this.zong()
                  },
                  zong:function(){
                        for(var i=0;tota=0;i<this.list.length;i++){
                              tota+=this.list[i].sub
                        }
                        this.zj=tota
                  }
            }
      });
      new Vue({
            el:'.nr'
      })
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容

  • 如何判断当前BroadcastReceiver接受到的是有序广播还是无序广播 在BroadcastReceiver...
    侯蛋蛋_阅读 1,492评论 0 1
  • (1)Gameobject类 GameObject是Unity场景里面所有实体的基类. 一、查找游戏对象 1. ...
    _凉笙阅读 1,717评论 0 1
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,982评论 3 119
  • “番茄钟”介绍 番茄钟,是根据一个瑞典人所写的番茄工作法理论进行开发的一款方便、实用的日程管理软件。指的是把工作任...
    MrYun阅读 1,884评论 1 0
  • 见过无数的鸡汤 都奉劝我得要好好爱着自己 但可能鸡汤们也不了解也不在乎这个活生生的你 你不可能准确活在它们设定的条...
    断斯阅读 142评论 0 0