vue中ref的作用

vue中的ref其實(shí)功能很強(qiáng)大,下面介紹一下如何使用。

基本用法,本頁(yè)面獲取dom元素

<template>
  <div id="app">
    <div ref="testDom">11111</div>
    <button @click="getTest">獲取test節(jié)點(diǎn)</button>
  </div>
</template>

<script>
export default {
  methods: {
    getTest() {
      console.log(this.$refs.testDom)
    }
  }
};
</script>
image.png

其實(shí)ref除了可以獲取本頁(yè)面的dom元素,還可以拿到子組件中的data和去調(diào)用子組件中的方法

獲取子組件中的data

子組件

<template>
  <div>
      {{ msg }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      msg: "hello world"
    }
  }
}
</script>

父組件

<template>
  <div id="app">
    <HelloWorld ref="hello"/>
    <button @click="getHello">獲取helloworld組件中的值</button>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {}
  },
  methods: {
    getHello() {
      console.log(this.$refs.hello.msg)
    }
  }
};
</script>
image.png

調(diào)用子組件中的方法

子組件

<template>
  <div>
  </div>
</template>

<script>
export default {
  methods: {
    open() {
      console.log("調(diào)用到了")
    }
  }
}
</script>

父組件

<template>
  <div id="app">
    <HelloWorld ref="hello"/>
    <button @click="getHello">獲取helloworld組件中的值</button>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {}
  },
  methods: {
    getHello() {
      this.$refs.hello.open();
    }
  }
};
</script>
image.png

子組件調(diào)用父組件方法

子組件

<template>
  <div>
</div>
</template>

<script>
export default {
  methods: {
    open() {
      console.log("調(diào)用了");
      //  調(diào)用父組件方法
      this.$emit("refreshData");
    }
  }
}
</script>

父組件

<template>
  <div id="app">
    <HelloWorld ref="hello" @refreshData="getData"/>
    <button @click="getHello">獲取helloworld組件中的值</button>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {}
  },
  methods: {
    getHello() {
      this.$refs.hello.open()
    },
    getData() {
      console.log('111111')
    }
  }
};
</script>
image.png

未完待續(xù)

最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 40、React 什么是React?React 是一個(gè)用于構(gòu)建用戶界面的框架(采用的是MVC模式):集中處理VIE...
    萌妹撒閱讀 1,179評(píng)論 0 1
  • 1.vue中的ref作用 (1)基本作用是在本頁(yè)面獲取dom元素 <template> 11111 ...
    初柚_eeab閱讀 558評(píng)論 0 0
  • 1.vue.js的兩個(gè)核心是什么? vue.js的兩個(gè)核心分別是數(shù)據(jù)驅(qū)動(dòng)(MVVM)和組件化。 一、數(shù)據(jù)驅(qū)動(dòng) 數(shù)據(jù)...
    fengcol閱讀 1,107評(píng)論 0 3
  • 主要還是自己看的,所有內(nèi)容來(lái)自官方文檔。 介紹 Vue.js 是什么 Vue (讀音 /vju?/,類似于 vie...
    Leonzai閱讀 3,535評(píng)論 0 25
  • 沒(méi)有天賦的人做事,貴在持之有恒; 沒(méi)有寫(xiě)作的能力,便做一個(gè)文字的搬運(yùn)工; 在該文集里都是摘抄讀周國(guó)平先生的《只是眷...
    天乙少康閱讀 309評(píng)論 0 0

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