組件相關(guān)

一.組件基礎(chǔ)

1.全局組件

<div  class='nr'>
      <my-component><.my-conmponent>    
</div>
<script  scr:"js鏈接"></script>
<script>
      Vue.component('my-component',{        ('my-component' 引號中的內(nèi)容由自己定,但格式必須為‘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.組件中加入點擊事件(點擊按鈕頁面彈出數(shù)字“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’是父傳子組件的關(guān)鍵屬性)
            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._  引號中為自定義的函數(shù)名)
                  }
            }
      })
      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:"將內(nèi)容傳給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>

練習(xí)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>

練習(xí)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>數(shù)量</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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 如何判斷當前BroadcastReceiver接受到的是有序廣播還是無序廣播 在BroadcastReceiver...
    侯蛋蛋_閱讀 1,556評論 0 1
  • (1)Gameobject類 GameObject是Unity場景里面所有實體的基類. 一、查找游戲?qū)ο?1. ...
    _涼笙閱讀 1,820評論 0 1
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,199評論 3 119
  • “番茄鐘”介紹 番茄鐘,是根據(jù)一個瑞典人所寫的番茄工作法理論進行開發(fā)的一款方便、實用的日程管理軟件。指的是把工作任...
    MrYun閱讀 2,015評論 1 0
  • 見過無數(shù)的雞湯 都奉勸我得要好好愛著自己 但可能雞湯們也不了解也不在乎這個活生生的你 你不可能準確活在它們設(shè)定的條...
    斷斯閱讀 214評論 0 0

友情鏈接更多精彩內(nèi)容