Vue學(xué)習(xí)筆記一 (組件)


全局組件

使用 Vue.component(tagName,options) 可以注冊一個(gè)全局組件。組件是全局的,即在Vue的任何實(shí)例下都可以使用該組件

Vue.component('TodoItem',{
      props: ['content'],
      template: '<li>{{content}}</li>'
    })

局部組件

局部組件用選項(xiàng)對象 components 屬性實(shí)現(xiàn)注冊,只能在當(dāng)前實(shí)例中使用該組件

var TodoItem = {
      props: ['content'],
      template: '<li>{{content}}</li>'
    }
    var app = new Vue({
      el: '#app',
      components:{TodoItem},
      data: {
        list: [],
        inputValue:''
      },
      methods: {
        btn: function(){
          this.list.push(this.inputValue)
          this.inputValue = ''
        } 
      }
    })

Ps:實(shí)例帶碼

<div id='app'>
    <input type="text" v-model='inputValue'>
    <button @click='btn'>提交</button>
    <ul>
      <todo-item v-bind:content='item'                        //這里采用父組件向子組件傳值
                 v-for='item in list'>                
      </todo-item>
    </ul>
  </div>

父子組件間傳值

<div id="root">     //父 => 子    綁定屬性count
    <counter ref='one' :count='3' @change='handleIncrease'></counter>
    <counter ref='two' :count='2' @change='handleIncrease'></counter>
    <div>{{total}}</div>
  </div>
  <script>
    var counter = {
      props: ['count'],        
      template: '<div @click="handleClick">{{number}}</div>',
      data: function () {
        return {
          number: this.count
        }
      },
      methods: {
        handleClick: function () {
          this.number++
          this.$emit('change', 1)    // 子 =>父     綁定事件change
        }
      }
    }
    var vm = new Vue({
      el: '#root',
      data: {
        total: 5
      },
      components: {
        counter
      },
      methods: {
        handleIncrease: function (step) {
          // this.total += step
          this.total = this.$refs.one.number + this.$refs.two.number
        }
      }
    })
  </script>

組件參數(shù)校驗(yàn)

<div id="root">
    <child :content='123'></child>
  </div>
  <script>
    Vue.component('child', {
      props: {
        content: {
          type: String,
          // required: false,    是否要求必須傳遞值
          // default: 'ASDCXD',  如果沒有傳值,則默認(rèn)值為ASDCXD
          validator: function() {
            return value.length > 5      //對所傳的值進(jìn)行長度校驗(yàn)
          }
        }
      },
      template: '<div>{{content}}</div>'
    })

    var vm = new Vue({
      el: '#root',
    })
  </script>

組件使用的一些細(xì)節(jié)

每個(gè)row為一個(gè)組件,但tbody內(nèi)只能是tr,所以使用is,(ul ,select一樣,如果內(nèi)部子項(xiàng)為一個(gè)單獨(dú)組件,為了防止bug,使用is)
<div id="root">
    <table>
      <tbody>
        <tr is='row'></tr>
        <tr is='row'></tr>
        <tr is='row'></tr>
      </tbody>
    </table>
  </div>
  <script>
    Vue.component('row', {
      template: '<tr><td>this is row</td></tr>'
    })
    var vm = new Vue({
      el: '#root'
    })
  </script>

給組件綁定原生事件

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

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

  • Vue筆記系列1、Vue.js入門3、Vue.js進(jìn)階 API 以下會(huì)隨用隨記一些API,可能會(huì)不定期更新。 Vu...
    其心閱讀 2,137評論 0 10
  • Vue 實(shí)例 屬性和方法 每個(gè) Vue 實(shí)例都會(huì)代理其 data 對象里所有的屬性:var data = { a:...
    云之外閱讀 2,373評論 0 6
  • 此文基于官方文檔,里面部分例子有改動(dòng),加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,949評論 5 14
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,180評論 0 29
  • 我們都是一個(gè)不服輸?shù)娜耍覀冇袎粝?,不服輸,每每在家人,老師的勸說下,我們,不屑一顧,總認(rèn)為自己就是那么牛...
    狂奔的喔牛閱讀 458評論 0 2

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