最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-父子組件和ref實例詳解

 更新時間:2019年11月10日 07:42:24   作者:跌倒的小黃瓜  
這篇文章通過實例代碼給大家介紹了vue-父子組件傳值和ref獲取dom和組件的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

父組件向子組件傳值

<div id="app">
  <!-- 父組件,可以在引用子組件的時候, 通過 屬性綁定(v-bind:) 的形式, 把 需要傳遞給 子組件的數(shù)據(jù),以屬性綁定的形式,傳遞到子組件內部,供子組件使用 -->
  <com1 v-bind:parentmsg="msg"></com1>
 </div>
// 創(chuàng)建 Vue 實例,得到 ViewModel
  var vm = new Vue({
   el: '#app',
   data: {
    msg: '123 啊-父組件中的數(shù)據(jù)'
   },
   methods: {},
   components: {
    // 結論:經(jīng)過演示,發(fā)現(xiàn),子組件中,默認無法訪問到 父組件中的 data 上的數(shù)據(jù) 和 methods 中的方法
    com1: {
     data() { // 注意: 子組件中的 data 數(shù)據(jù),并不是通過 父組件傳遞過來的,而是子組件自身私有的,比如: 子組件通過 Ajax ,請求回來的數(shù)據(jù),都可以放到 data 身上;
      // data 上的數(shù)據(jù),都是可讀可寫的;
      return {
       title: '123',
       content: 'qqq'
      }
     },
     template: '<h1 @click="change">這是子組件 --- {{ parentmsg }}</h1>',
     // 注意: 組件中的 所有 props 中的數(shù)據(jù),都是通過 父組件傳遞給子組件的
     // props 中的數(shù)據(jù),都是只讀的,無法重新賦值
     props: ['parentmsg'], // 把父組件傳遞過來的 parentmsg 屬性,先在 props 數(shù)組中,定義一下,這樣,才能使用這個數(shù)據(jù),只讀,寫的話會報警告
     directives: {},
     filters: {},
     components: {},
     methods: {
      change() {
       this.parentmsg = '被修改了'
      }
     }
    }
   }
  });

父組件向子組件傳方法

<div id="app">
  <!-- 父組件向子組件 傳遞 方法,使用的是 事件綁定機制; v-on, 當我們自定義了 一個 事件屬性之后,那么,子組件就能夠,通過某些方式,來調用 傳遞進去的 這個 方法了 -->
  <com2 @func="show"></com2>
 </div>
 <template id="tmpl">
  <div>
   <h1>這是 子組件</h1>
   <input type="button" value="這是子組件中的按鈕 - 點擊它,觸發(fā) 父組件傳遞過來的 func 方法" @click="myclick">
  </div>
 </template>
 // 定義了一個字面量類型的 組件模板對象
  var com2 = {
   template: '#tmpl', // 通過指定了一個 Id, 表示 說,要去加載 這個指定Id的 template 元素中的內容,當作 組件的HTML結構
   data() {
    return {
     sonmsg: { name: '小頭兒子', age: 6 }
    }
   },
   methods: {
    myclick() {
     // 當點擊子組件的按鈕的時候,如何 拿到 父組件傳遞過來的 func 方法,并調用這個方法???
     // emit 英文原意: 是觸發(fā),調用、發(fā)射的意思
     // this.$emit('func123', 123, 456)
     this.$emit('func', this.sonmsg)
    }
   }
  }
  // 創(chuàng)建 Vue 實例,得到 ViewModel
  var vm = new Vue({
   el: '#app',
   data: {
    datamsgFormSon: null
   },
   methods: {
    show(data) {
     // console.log('調用了父組件身上的 show 方法: --- ' + data)
     console.log(data);
     this.datamsgFormSon = data
    }
   },
   components: {
    com2
    // com2: com2
   }
  });

vue+本地存儲實現(xiàn)評論功能

難道在于理解父組件向子組件傳方法

<div id="app">
  <cmt-box @func="loadComments"></cmt-box>
  <ul class="list-group">
   <li class="list-group-item" v-for="item in list" :key="item.id">
    <span class="badge">評論人: {{ item.user }}</span>
    {{ item.content }}
   </li>
  </ul>
 </div>
 <template id="tmpl">
  <div>
   <div class="form-group">
    <label>評論人:</label>
    <input type="text" class="form-control" v-model="user">
   </div>
   <div class="form-group">
    <label>評論內容:</label>
    <textarea class="form-control" v-model="content"></textarea>
   </div>
   <div class="form-group">
    <input type="button" value="發(fā)表評論" class="btn btn-primary" @click="postComment">
   </div>
  </div>
 </template>
var commentBox = {
   data() {
    return {
     user: '',
     content: ''
    }
   },
   template: '#tmpl',
   methods: {
    postComment() { // 發(fā)表評論的方法
     // 分析:發(fā)表評論的業(yè)務邏輯
     // 1. 評論數(shù)據(jù)存到哪里去???  存放到了 localStorage 中 localStorage.setItem('cmts', '')
     // 2. 先組織出一個最新的評論數(shù)據(jù)對象
     // 3. 想辦法,把 第二步中,得到的評論對象,保存到 localStorage 中:
     // 3.1 localStorage 只支持存放字符串數(shù)據(jù), 要先調用 JSON.stringify 
     // 3.2 在保存 最新的 評論數(shù)據(jù)之前,要先從 localStorage 獲取到之前的評論數(shù)據(jù)(string), 轉換為 一個 數(shù)組對象, 然后,把最新的評論, push 到這個數(shù)組
     // 3.3 如果獲取到的 localStorage 中的 評論字符串,為空不存在, 則 可以 返回一個 '[]' 讓 JSON.parse 去轉換
     // 3.4 把 最新的 評論列表數(shù)組,再次調用 JSON.stringify 轉為 數(shù)組字符串,然后調用 localStorage.setItem()
     var comment = { id: Date.now(), user: this.user, content: this.content }
     // 從 localStorage 中獲取所有的評論
     var list = JSON.parse(localStorage.getItem('cmts') || '[]')
     list.unshift(comment)
     // 重新保存最新的 評論數(shù)據(jù)
     localStorage.setItem('cmts', JSON.stringify(list))
     this.user = this.content = ''
     // this.loadComments() // ?????
     this.$emit('func')
    }
   }
  }
  // 創(chuàng)建 Vue 實例,得到 ViewModel
  var vm = new Vue({
   el: '#app',
   data: {
    list: [
     { id: Date.now(), user: '李白', content: '天生我材必有用' },
     { id: Date.now(), user: '江小白', content: '勸君更盡一杯酒' },
     { id: Date.now(), user: '小馬', content: '我姓馬, 風吹草低見牛羊的馬' }
    ]
   },
   beforeCreate(){ // 注意:這里不能調用 loadComments 方法,因為在執(zhí)行這個鉤子函數(shù)的時候,data 和 methods 都還沒有被初始化好
   },
   created(){
    this.loadComments()
   },
   methods: {
    loadComments() { // 從本地的 localStorage 中,加載評論列表
     var list = JSON.parse(localStorage.getItem('cmts') || '[]')
     this.list = list
    }
   },
   components: {
    'cmt-box': commentBox
   }
  });

ref獲取DOM和組件

vue中如何操作DOM

 <div id="app">
  <input type="button" value="獲取元素" @click="getElement" ref="mybtn">
  <h3 id="myh3" ref="myh3">哈哈哈, 今天天氣太好了?。?!</h3>
  <hr>
  <login ref="mylogin"></login>
 </div>
var login = {
   template: '<h1>登錄組件</h1>',
   data() {
    return {
     msg: 'son msg'
    }
   },
   methods: {
    show() {
     console.log('調用了子組件的方法')
    }
   }
  }
  // 創(chuàng)建 Vue 實例,得到 ViewModel
  //vm中有一個屬性叫ref
  var vm = new Vue({
   el: '#app',
   data: {},
   methods: {
    getElement() {
     // console.log(document.getElementById('myh3').innerText)
     // ref 是 英文單詞 【reference】  值類型 和 引用類型 referenceError
     // console.log(this.$refs.myh3.innerText)
     console.log(this.$refs.mylogin.msg)
     this.$refs.mylogin.show()
    }
   },
   components: {
    login
   }
  });

總結

以上所述是小編給大家介紹的vue-父子組件和ref實例詳解,希望對大家有所幫助!

相關文章

最新評論

汾阳市| 江口县| 黄浦区| 旺苍县| 彰化县| 平遥县| 会理县| 华安县| 兰坪| 华安县| 延庆县| 泽库县| 新化县| 镇江市| 洛隆县| 枣庄市| 玛纳斯县| 凤庆县| 收藏| 横峰县| 汨罗市| 浏阳市| 云南省| 安泽县| 涞源县| 肃南| 来安县| 乌拉特中旗| 侯马市| 四平市| 灵丘县| 定西市| 通化市| 共和县| 枣阳市| 亳州市| 嫩江县| 翼城县| 泸州市| 额尔古纳市| 东源县|