Vue組件學(xué)習(xí)筆記(持續(xù)更新中...)

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


局部組件的使用


簡易 demo

<div id="app"></div>
<script>
  // 1. 聲明子組件
  var Vheader = {
    template: '<div>我是頭部組件</div>'
  }

  var App = {
    template: '<div>我是入口組件</div>'
  }

  new Vue({
    el: '#app',
    data() {
      return {}
    },
    // 2. 掛載子組件
    components: {
      App
    },
    // 3. 使用子組件
    template: '<App/>'
  })
</script>

組件通信


總結(jié):

  • 子組件通過 props 接受父組件的數(shù)據(jù)
  • 子組件通過$emit 觸發(fā)父組件的自定義事件來向父組件傳遞數(shù)據(jù)

組件通信:子 => 父 demo


Vue.js 的文件路徑記得修改

  1. 先給父組件中綁定自定義屬性
  2. 在子組件中使用 props 接收父組件傳遞的數(shù)據(jù)
  3. 可以在子組件中任意使用
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>組件通信-通過prop往子組件通信</title>
    <script type="text/javascript" src="JS/Vue.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript">

      // 2. Child組件
      var Child = {
        template: `
        <div>
          <p>我是一個(gè)子組件:{{ childData }}</p>
          <input type="text" v-model='childData' />
        </div>
        `,
        props: ['childData']
      }

      // 1. Parent組件
      var Parent = {
        data() {
          return {
            msg: '我是父組件的數(shù)據(jù)'
          }
        },
        template: `
        <div>
          <p>我是一個(gè)父組件</p>
          <Child :childData='msg'/>
        </div>
        `,
        components: {
          Child
        }
      }

      var App = {
        data() {
          return {}
        },
        template: `
        <div>
          <Parent/>
        </div>
        `,
        components: {
          Parent
        }
      }

      new Vue({
        el: '#app',
        data() {
          return {}
        },
        components: {
          App
        },
        template: `
          <App/>        `
      })
    </script>
  </body>
</html>

組件通信:父 => 子 demo


  1. 在父組件綁定自定義的事件
  2. 在子組件中觸發(fā)原生的事件,在函數(shù)中使用$emit 觸發(fā)自定義的事件(childHandler)
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>通過事件向子組件通信</title>
    <script type="text/javascript" src="JS/Vue.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript">
      // Child組件
      var Child = {
        template: `
        <div>
          <p>我是一個(gè)子組件:{{ childData }}</p>
          <input type="text" v-model='childData' @input='changeValue(childData)' /> // v-model改變傳遞的數(shù)據(jù)會(huì)報(bào)warn,本例只是演示,不必顧慮
        </div>
        `,
        methods: {
          changeValue(val) {
            // 在函數(shù)中
            // $emit(自定義的事件名,傳遞的 消息)
            // 自定義的事件一定通過this.$emit去觸發(fā)
            this.$emit('childHandler', val)
          }
        },
        props: ['childData']
      }

      // Parent組件
      var Parent = {
        data() {
          return {
            msg: '我是父組件的數(shù)據(jù)'
          }
        },
        // childHandler為父組件綁定的自定義事件(步驟1),@是v-on的縮寫,用于監(jiān)聽事件
        template: `
        <div>
          <p>我是一個(gè)父組件</p>
          <Child :childData='msg' @childHandler='childHandler'/>
        </div>
        `,
        methods: {
          childHandler(val) {
            console.log(val)
          }
        },
        components: {
          Child
        }
      }

      var App = {
        data() {
          return {}
        },
        template: `
        <div>
          <Parent/>
        </div>
        `,
        components: {
          Parent
        }
      }

      new Vue({
        el: '#app',
        data() {
          return {}
        },
        components: {
          App
        },
        template: `
          <App/>
        `
      })
    </script>
  </body>
</html>

最后小結(jié):在本例中,父組件上為子組件綁定 childHandler 方法,子組件綁定 changeValue 方法,而在 changeValue 方法中,使用 this.$emit()向父組件的方法 childHandler 傳遞數(shù)據(jù),即完成了父子組件之間的通信。

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

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

  • Vue 學(xué)習(xí)筆記入門篇 可復(fù)用性的組件 7.1 使用組件的原因 作用:提高代碼的復(fù)用性 7.2 組件的使用方法 全...
    hzl的學(xué)習(xí)小記閱讀 234評(píng)論 0 0
  • 摘要: 總有一款合適的通信方式。 作者:浪里行舟 Fundebug經(jīng)授權(quán)轉(zhuǎn)載,版權(quán)歸原作者所有。 前言 組件是 v...
    Fundebug閱讀 15,646評(píng)論 3 57
  • 前言 組件是 vue.js最強(qiáng)大的功能之一,而組件實(shí)例的作用域是相互獨(dú)立的,這就意味著不同組件之間的數(shù)據(jù)無法相互引...
    Vicky丶Amor閱讀 6,119評(píng)論 10 162
  • 1、感恩美好的一天開始了,一切都是新的,我也是新的,幸福從此刻開始。 2、感恩天地滋養(yǎng)萬物,感恩國家培養(yǎng)護(hù)佑,感恩...
    胡藝瓊閱讀 177評(píng)論 0 0
  • 回去的路因?yàn)樘崆傲藘商欤吠竞玫闹虚g都控制不住的超速了,天清氣朗,聽著歌,在回家的路上翱翔。 回來的時(shí)候也提前了兩...
    淺気閱讀 81評(píng)論 0 1

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