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

vue如何解決this.refs拿取v-for下元素undefine問題

 更新時間:2023年05月20日 08:39:42   作者:凡塵向天  
這篇文章主要介紹了vue如何解決this.refs拿取v-for下元素undefine問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue解決this.refs拿取v-for下元素undefine問題

問題

我想從 v-for 底下拿取一個 ref=“articleContent”的div

<el-card ?class="box-card" shadow="hover" v-for="(moodEssayDate,index) in moodEssayDates" :key="moodEssayDate.pk_moodEssay">
? ? ? ?<div class="card">
? ? ? ? ?<div class="cardLeft">
? ? ? ? ? ?<el-avatar src=" //img.jbzj.com/file_images/article/201310/20131008165929119.jpg"></el-avatar>
? ? ? ? ? ?<swingingPendant class="swingingPendant" v-show="!isLogin"></swingingPendant>
? ? ? ? ?</div>
? ? ? ? ?<div class="cardCenter">
? ? ? ? ? ?<div class="cardCenterHeader">
? ? ? ? ? ? ?<div class="articleTitle"><label>{{moodEssayDate.essayTitle}}</label></div>
? ? ? ? ? ? ?<span><i class="el-icon-time"></i>{{moodEssayDate.ts}}</span>
? ? ? ? ? ?</div>
? ? ? ? ? ?<div class="cardCenterBody">
? ? ? ? ? ? ?<div class="cardCenterBodyTop">
? ? ? ? ? ? ? ?<div :class="[moodEssayDate.isAll?'articleContent':'articleContent2']" ?ref="articleContent">{{moodEssayDate.essayContent}}</div>
? ? ? ? ? ? ? ?<el-button class="checkArticle" type="text" @click="showAllContent(moodEssayDate,index)" v-show="moodEssayDate.showAllContentBtn">展開查看全文</el-button>
? ? ? ? ? ? ? ?<el-button style="margin: 0;" type="text" @click="NotShowAllContent(moodEssayDate,index)" v-show="moodEssayDate.packUpArticle" icon="el-icon-top">收起</el-button>
? ? ? ? ? ? ?</div>
? ? ? ? ? ? ?<div class="articleBtnS">
? ? ? ? ? ? ? ?<div ?v-show="!isLogin">——無心語錄</div>
? ? ? ? ? ? ? ?<el-button :loading="DelBtnLoading" ?v-show="isLogin" @click="DelMoodEssay(moodEssayDate)" ?type="primary" icon="el-icon-delete ">刪除</el-button>
? ? ? ? ? ? ?</div>
? ? ? ? ? ?</div>
? ? ? ? ?</div>
? ? ? ?</div>
? ? ? </el-card

我嘗試在mounted()里面拿取

? mounted() {
? ? ? this.$nextTick(()=>{
? ? ? console.log(this.refs.articleContent) //undefine
? ? ? //但是 refs里面是有的
? ? ? ? console.log(this.refs) // {} 這里面有 元素?
? ? ? })
? ? },

嘗試在update() 里面拿,也是undefined

最后設(shè)置了個定時器,解決

?setTimeout(() => {
? this.$nextTick(()=>{
? ? ?if(this.$refs.articleContent){
? ? ? ? ? ? ? ?console.log(this.refs.articleContent) //這時候會拿到一個元素數(shù)組
? ? ? ? ? }, 200);
? ? ? })
? ? ? //這時候 是可以拿取到的

            

反思:

之所以 拿取不到,是因為 容器還未渲染 我就 去獲取元素,因為我這里是異步請求數(shù)據(jù)來 渲染容器的

methods:{
? ?// 界面請求數(shù)據(jù) ?初始化
? ? ? getAllMessages(){
? ? ? ? getAllMessages().then(res=>{
? ? ? ? ? for(let i=0;i<res.data.moodEssayList.length;i++){
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'index', i)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'packUpArticle', false)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'showAllContentBtn', false)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'isAll', true)
? ? ? ? ? }
? ? ? ? ? this.currentTotal = res.data.moodEssayList.length
? ? ? ? ? this.moodEssayDates = res.data.moodEssayList.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
? ? ? ? })
? ? ? },
}
// 最終 在 請求的回調(diào)函數(shù),設(shè)置個計時器 完美解決 問題
? ? ? getAllMessages(){
? ? ? ? getAllMessages().then(res=>{
? ? ? ? ? for(let i=0;i<res.data.moodEssayList.length;i++){
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'index', i)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'packUpArticle', false)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'showAllContentBtn', false)
? ? ? ? ? ? this.$set(res.data.moodEssayList[i], 'isAll', true)
? ? ? ? ? }
? ? ? ? ? this.currentTotal = res.data.moodEssayList.length
? ? ? ? ? this.moodEssayDates = res.data.moodEssayList.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? if(this.$refs.articleContent){
? ? ? ? ? ? ? for(let i = 0;i<this.$refs.articleContent.length;i++){
? ? ? ? ? ? ? ? if(this.$refs.articleContent[i].offsetHeight>=189){
? ? ? ? ? ? ? ? ? this.$set(this.moodEssayDates[i], 'showAllContentBtn', true)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }, 200);
? ? ? ? })
? ? ? },

心得

問題就是分為 三個部分, 第一是 創(chuàng)建容器,第二是生命周期 獲取容器元素 ,第三 是 異步請求數(shù)據(jù)后渲染容器。

按照之前的解決辦法,出問題是因為,是這個順序,所以 就是 我在容器還未被數(shù)據(jù)渲染就去拿取元素,自然是undefine。

因此 想要解決 只需要將 第二步 和第三步 換個順序就行,所以我想到了設(shè)置定時器。

vue中this.$refs的使用

<!--父組件-->
<template>
    <div>
      <!-- 
        ref 寫在標簽上時:this.$refs.名字  獲取的是標簽對應(yīng)的dom元素
        ref寫在組件上時:這時候獲取到的是子組件(比如navChild)的引用  
      -->
      <Child ref="navChild"/>
    </div>
</template>
<script>
import Child from "@/components/child";
export default {
  name: "App",
  methods: {
    navFn(){
      // this.$refs.navChild 或者 this.$refs['navChild']
      // 第一種使用情況(父組件調(diào)用子組件的方法)
      this.$ref.navChild.initData();
      // 第二種使用情況(父組件調(diào)用子組件的方法,并通過方法傳值)
      this.$refs.navChild.initData('我是父組件的傳值')
      // 第四種使用情況(父組件獲取子組件的數(shù)據(jù))
      this.$ref.navChild.status
      // 第四種使用情況(父組件獲取子組件的數(shù)據(jù),并改變數(shù)據(jù))
      this.$ref.navChild.status = 1;
    }
  },
};
</script>
<!-- 子組件 -->
<template>
    <div>
      <div>{{message}}</div>
    </div>
</template>
<script>
export default {
  name: "Child",
  data() {
    return {
      message:'這是子組件',
      status:0
    };
  },
  components: {},
  created() {
      console.log(this.$data.status); //1(通過this.$data獲取所有變量)
      this.status = this.$data.status;
  },
  methods:{
    initData(val){
      // 方法二:獲取父組件的傳值
      console.log(val); //我是父組件的傳值
      this.message = val;
      let name = 'Hello World';
    }
  }
};
</script>
<style scoped>
</style>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue踩坑之backgroundImage路徑問題及解決

    vue踩坑之backgroundImage路徑問題及解決

    這篇文章主要介紹了vue踩坑之backgroundImage路徑問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 解決vue單頁面 回退頁面 keeplive 緩存問題

    解決vue單頁面 回退頁面 keeplive 緩存問題

    這篇文章主要介紹了解決vue單頁面 回退頁面 keeplive 緩存問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue實現(xiàn)接口封裝的實現(xiàn)示例

    vue實現(xiàn)接口封裝的實現(xiàn)示例

    本文主要介紹了vue實現(xiàn)接口封裝的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • vue中html2canvas給指定區(qū)域添加滿屏水印

    vue中html2canvas給指定區(qū)域添加滿屏水印

    本文主要介紹了vue中html2canvas給指定區(qū)域添加滿屏水印,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • 解析vue data不可以使用箭頭函數(shù)問題

    解析vue data不可以使用箭頭函數(shù)問題

    這篇文章主要介紹了vue data不可以使用箭頭函數(shù)問題,本文通過源碼解析給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Vue項目實現(xiàn)html5圖片上傳的示例代碼

    Vue項目實現(xiàn)html5圖片上傳的示例代碼

    本文主要介紹了Vue項目?html5圖片上傳,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實現(xiàn)

    Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實現(xiàn)

    這篇文章主要介紹了Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Vue 實現(xiàn)把表單form數(shù)據(jù) 轉(zhuǎn)化成json格式的數(shù)據(jù)

    Vue 實現(xiàn)把表單form數(shù)據(jù) 轉(zhuǎn)化成json格式的數(shù)據(jù)

    今天小編就為大家分享一篇Vue 實現(xiàn)把表單form數(shù)據(jù) 轉(zhuǎn)化成json格式的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue + element ui實現(xiàn)播放器功能的實例代碼

    vue + element ui實現(xiàn)播放器功能的實例代碼

    這篇文章主要介紹了vue + element ui實現(xiàn)播放器功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • vue3+elementplus基于el-table-v2封裝公用table組件詳細代碼

    vue3+elementplus基于el-table-v2封裝公用table組件詳細代碼

    在日常開發(fā)后端管理系統(tǒng)項目中,用于展示數(shù)據(jù)多會用表格進行展示,下面這篇文章主要給大家介紹了關(guān)于vue3+elementplus基于el-table-v2封裝公用table組件的相關(guān)資料,需要的朋友可以參考下
    2023-12-12

最新評論