欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

父組件向子組件傳值

<div id="app">
  <!-- 父組件,可以在引用子組件的時候, 通過 屬性綁定(v-bind:) 的形式, 把 需要傳遞給 子組件的數(shù)據(jù),以屬性綁定的形式,傳遞到子組件內(nèi)部,供子組件使用 -->
  <com1 v-bind:parentmsg="msg"></com1>
 </div>
// 創(chuàng)建 Vue 實例,得到 ViewModel
  var vm = new Vue({
   el: '#app',
   data: {
    msg: '123 啊-父組件中的數(shù)據(jù)'
   },
   methods: {},
   components: {
    // 結(jié)論:經(jīng)過演示,發(fā)現(xiàn),子組件中,默認(rè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, 當(dāng)我們自定義了 一個 事件屬性之后,那么,子組件就能夠,通過某些方式,來調(diào)用 傳遞進去的 這個 方法了 -->
  <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 元素中的內(nèi)容,當(dāng)作 組件的HTML結(jié)構(gòu)
   data() {
    return {
     sonmsg: { name: '小頭兒子', age: 6 }
    }
   },
   methods: {
    myclick() {
     // 當(dāng)點擊子組件的按鈕的時候,如何 拿到 父組件傳遞過來的 func 方法,并調(diào)用這個方法???
     // emit 英文原意: 是觸發(fā),調(diào)用、發(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('調(diào)用了父組件身上的 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>評論內(nèi)容:</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è)務(wù)邏輯
     // 1. 評論數(shù)據(jù)存到哪里去???  存放到了 localStorage 中 localStorage.setItem('cmts', '')
     // 2. 先組織出一個最新的評論數(shù)據(jù)對象
     // 3. 想辦法,把 第二步中,得到的評論對象,保存到 localStorage 中:
     // 3.1 localStorage 只支持存放字符串?dāng)?shù)據(jù), 要先調(diào)用 JSON.stringify 
     // 3.2 在保存 最新的 評論數(shù)據(jù)之前,要先從 localStorage 獲取到之前的評論數(shù)據(jù)(string), 轉(zhuǎn)換為 一個 數(shù)組對象, 然后,把最新的評論, push 到這個數(shù)組
     // 3.3 如果獲取到的 localStorage 中的 評論字符串,為空不存在, 則 可以 返回一個 '[]' 讓 JSON.parse 去轉(zhuǎn)換
     // 3.4 把 最新的 評論列表數(shù)組,再次調(diào)用 JSON.stringify 轉(zhuǎn)為 數(shù)組字符串,然后調(diào)用 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: '我姓馬, 風(fēng)吹草低見牛羊的馬' }
    ]
   },
   beforeCreate(){ // 注意:這里不能調(diào)用 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('調(diào)用了子組件的方法')
    }
   }
  }
  // 創(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
   }
  });

總結(jié)

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

相關(guān)文章

  • Vue?2源碼閱讀?Provide?Inject?依賴注入詳解

    Vue?2源碼閱讀?Provide?Inject?依賴注入詳解

    這篇文章主要為大家介紹了Vue?2源碼閱讀?Provide?Inject?依賴注入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • vue 實現(xiàn) rem 布局或vw 布局的方法

    vue 實現(xiàn) rem 布局或vw 布局的方法

    這篇文章主要介紹了vue 實現(xiàn) rem 布局的 或者 vw 布局的方法,本文給提供多種方法,需要的朋友可以參考下
    2019-11-11
  • 詳解Vue template 如何支持多個根結(jié)點

    詳解Vue template 如何支持多個根結(jié)點

    這篇文章主要介紹了詳解Vue template 如何支持多個根結(jié)點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Vue router動態(tài)路由實現(xiàn)過程

    Vue router動態(tài)路由實現(xiàn)過程

    Vue動態(tài)路由(約定路由),聽起來好像很玄乎的樣子,但是你要是理解了實現(xiàn)思路,你會發(fā)現(xiàn)沒有想象中的那么難,下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)動態(tài)路由添加功能的簡單方法,需要的朋友可以參考下
    2023-03-03
  • Vue3中watch的使用詳解

    Vue3中watch的使用詳解

    這篇文章主要介紹了Vue3中watch的詳解,主要包括Vue2使用watch及Vue3使用watch的方法,通過多種情況實例代碼相結(jié)合給大家詳細(xì)講解,需要的朋友可以參考下
    2022-11-11
  • 解決Vue3使用Element-Plus導(dǎo)航刷新后active高亮消失的問題

    解決Vue3使用Element-Plus導(dǎo)航刷新后active高亮消失的問題

    這篇文章主要給大家介紹了如何解決Vue3使用Element-Plus導(dǎo)航刷新后active高亮消失的問題,文中有相關(guān)的代碼講解,需要的朋友可以參考下
    2023-08-08
  • vue代理模式解決跨域詳解

    vue代理模式解決跨域詳解

    這篇文章主要介紹了vue代理模式解決跨域詳解的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • vue實現(xiàn)登陸功能

    vue實現(xiàn)登陸功能

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)登陸功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 如何在Vue項目中添加接口監(jiān)聽遮罩

    如何在Vue項目中添加接口監(jiān)聽遮罩

    這篇文章主要介紹了如何在Vue項目中添加接口監(jiān)聽遮罩,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Vue3中實現(xiàn)選取頭像并裁剪

    Vue3中實現(xiàn)選取頭像并裁剪

    這篇文章主要詳細(xì)介紹了在vue3中如何選取頭像并裁剪,文章中有詳細(xì)的代碼示例,需要的朋友可以參考閱讀
    2023-04-04

最新評論