vuejs取得dom的方法:1、在元件的DOM部分,任意標籤中寫上「ref="xxx"」;2、透過元件物件「this.$refs.xxx」取得到元素即可。
本文操作環境:windows7系統、vue2.9.6版,DELL G3電腦。
Vue.js實例學習:取得DOM元素
#一、取得DOM元素
在Vue中取得DOM元素,我們可以用ref
。
用法(和React一樣):
(1)在元件的DOM部分,任意標籤中寫上:ref="xxx"#
## (2)透過元件物件
this.$refs.xxx 取得到元素
<div id="app"></div>
<script type="text/javascript">
let App = {
template: `
<div>
<button ref="btn">我是按钮</button>
</div>`,
beforeCreate() {
//这里不能操作数据
console.log('beforeCreate: ', this.$refs.btn);
},
created() {
//这里可以操作数据了
console.log('created: ', this.$refs.btn);
},
beforeMount() {
//new Vue 发生装载, 替换 <div id="app">之前
console.log('beforeMount: ', this.$refs.btn);
},
mounted() {
//装在数据之后
console.log('mounted: ', this.$refs.btn);
},
};
new Vue({
el: '#app',
components: {
app: App
},
template: `<app />`,
});
</script>
控制台輸出: 說明:mounted()時才能取得this.$refs.btn
#2、取得元件的DOM範例2:
<div id="app"></div>
<script type="text/javascript">
let Temp = {
template: `
<div>我是子组件</div>
`,
};
let App = {
components: {
temp: Temp,
},
template: `<temp ref="tmp"/>`,
mounted() {
console.log(this.$refs.tmp);
},
};
let vm = new Vue({
el: '#app',
components: {
app: App
},
template: `<app />`,
});
</script>
控制台輸出: 我們看到控制台
輸出的
是
temp元件。
這裡我們要關注的是元件的各個屬性(eg: $ el、$ parent 等)···
console.log(this.$refs.tmp.$el);控制台會輸出下圖,由此可知$el 代表著什麼~
- $children:················ 的子元件
- $root:取得new Vue的實例(即上方的:vm)
- $el: 取得目前元件的DOM元素
範例:
要求:在顯示input元素的瞬間,取得input的焦點
<div id="app"></div> <script type="text/javascript"> let App = { template: ` <div> <input type="text" v-if="isShow" ref="myInput" /> </div>`, data() { return { isShow: false, }; }, mounted() { this.isShow = true; //显示input元素 this.$refs.myInput.focus(); //获取input的焦点 }, }; let vm = new Vue({ el: '#app', components: { app: App }, template: `<app />`, }); </script>
運行後報錯:
報錯顯示focus不存在,原因是this. $refs.myInput
也是undefined,為什麼ref沒取得DOM元素呢? 我們先思考,如果我們把mounted函數內改成:
mounted() { this.isShow = true; this.isShow = false; this.isShow = true; },
運行過程中,input元素會 先顯示,再消失,然後再顯示嗎?
答案是否定的。因為Vue會先讓程式碼執行完,然後才會依照最終的值,進行DOM操作。
其實上面的程式碼等同於下面的程式碼:<pre class="brush:php;toolbar:false">mounted() {
this.isShow = true;
},</pre>
那麼要怎麼解決呢?
這裡我們用
$nextTick解決~
什麼時候用:
在Vue渲染DOM到頁面後面立即做某件事,用$nextTick<pre class="brush:php;toolbar:false">this.$nextTick(function() {
·····dosomething
})</pre>
let App = { template: ` <div> <input type="text" v-if="isShow" ref="myInput" /> </div>`, data() { return { isShow: false, }; }, mounted() { //显示input元素的瞬间,获取焦点 this.isShow = true; this.$nextTick(function() { this.$refs.myInput.focus(); }); }, }; let vm = new Vue({ el: '#app', components: { app: App }, template: `<app />`, });
推薦學習:《
最新的5個vue.js影片教學精選#以上是vuejs裡怎麼取得dom的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Vue.js是一種漸進式JavaScript框架,適用於構建複雜的用戶界面。 1)其核心概念包括響應式數據、組件化和虛擬DOM。 2)實際應用中,可以通過構建Todo應用和集成VueRouter來展示其功能。 3)調試時,建議使用VueDevtools和console.log。 4)性能優化可通過v-if/v-show、列表渲染優化和異步加載組件等實現。

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。

Vue 中實現組件跳轉有以下方法:使用 router-link 和 <router-view> 組件進行超鏈接跳轉,指定 :to 屬性為目標路徑。直接使用 <router-view> 組件顯示當前路由渲染的組件。使用 router.push() 和 router.replace() 方法進行程序化導航,前者保存歷史記錄,後者替換當前路由不留記錄。

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。

Vue.js提供了三種跳轉方式:原生 JavaScript API:使用 window.location.href 進行跳轉。 Vue Router:使用 <router-link> 標籤或 this.$router.push() 方法進行跳轉。 VueX:通過 dispatch action 或 commit mutation 來觸發路由跳轉。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能