Vue 'রেফ' বৈশিষ্ট্য
Vue.js 'ref' DOM . .
প্রধান আবেদন:
রেফ অ্যাট্রিবিউট এবং $refs অবজেক্টটি ভিউতে ব্যবহার করা যেতে পারে প্লেইন জাভাস্ক্রিপ্ট পদ্ধতি যেমন getElementById() বা querySelector() এর বিকল্প হিসেবে।
উদাহরণ
আপনার নিজস্ব Vue সার্ভার পান
রেফ অ্যাট্রিবিউট ব্যবহার করে একটি <p> ট্যাগের ভিতরে পাঠ্য প্রতিস্থাপন করা:
<div id="app">
<p ref="pEl">Initial text.</p>
<button v-on:click="changeText">Change text</button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
const app = Vue.createApp({
methods: {
changeText(){
this.$refs.pEl.innerHTML = "Hello!";
}
}
})
app.mount('#app')
</script>
নীচে আরো উদাহরণ দেখুন.
সংজ্ঞা এবং প্রয়োগ
রেফ অ্যাট্রিবিউটটি <টেমপ্লেট>-এ উপাদানগুলি চিহ্নিত করতে ব্যবহৃত হয় যাতে সেগুলি <script>-এর ভিতরে $refs অবজেক্ট থেকে অ্যাক্সেস করা যায়।
যদি v-for দিয়ে তৈরি HTML উপাদানগুলির একটি রেফ অ্যাট্রিবিউট থাকে, তাহলে ফলস্বরূপ DOM উপাদানগুলি $refs অবজেক্টে অ্যারে হিসাবে যোগ করা হয়:
উদাহরণ:
<ul>
<li v-for="x in liTexts" ref="liEl">{{ x }}</li>
</ul>
গুরুত্বপূর্ণ নোট:
রেফ অ্যাট্রিবিউটটি সরাসরি DOM ম্যানিপুলেশনের জন্য। Vue-এর প্রতি-সম্পত্তি পদ্ধতি প্রায়শই সর্বোত্তম বিকল্প, তবে কিছু ক্ষেত্রে রেফ দরকারী।
আরো উদাহরণ
উদাহরণ 1
একটি <p> উপাদানের মধ্যে পাঠ্য প্রতিস্থাপিত হয়।
<template>
<h1>Example</h1>
<p>Click the button to put "Hello!" as the text in the green p element.</p>
<button @click="changeVal">Change Text</button><br>
<p ref="pEl" id="pEl">This is the initial text</p>
</template>
<script>
export default {
methods: {
changeVal() {
this.$refs.pEl.innerHTML = "Hello!";
}
}
};
</script>
উদাহরণ 2
প্রথম <p> ট্যাগের টেক্সট দ্বিতীয় <p> ট্যাগে কপি করা হয়।
<template>
<h1>Example</h1>
<p ref="p1">Click the button to copy this text into the paragraph below.</p>
<button @click="transferText">Transfer text</button>
<p ref="p2">...</p>
</template>
<script>
export default {
methods: {
transferText() {
this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
}
}
};
</script>
উদাহরণ 3
একটি <p> উপাদান ইনপুট ক্ষেত্রে লেখা হওয়ার সাথে সাথে বিষয়বস্তু গ্রহণ করে।
<template>
<h1>Example</h1>
<p>Start writing inside the input element, and the text will be copied into the last paragraph by the use of the '$refs' object.</p>
<input ref="inputEl" @input="getRefs" placeholder="Write something..">
<p ref="pEl"></p>
</template>
<script>
export default {
methods: {
getRefs() {
this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
}
}
};
</script>
উদাহরণ 4
বোতামটি $refs অবজেক্টের ভিতরে একটি অ্যারে উপাদান হিসাবে সংরক্ষিত তৃতীয় তালিকা উপাদানটিকে প্রকাশ করে।
<template>
<h1>Example</h1>
<p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
<button @click="getValue">Get the 3rd list element</button><br>
<ul>
<li v-for="x in liTexts" ref="liEl">{{ x }}</li>
</ul>
<pre>{{ thirdEl }}</pre>
</template>
<script>
export default {
data() {
return {
thirdEl: ' ',
liTexts: ['Apple','Banana','Kiwi','Tomato','Lichi']
}
},
methods: {
getValue() {
this.thirdEl = this.$refs.liEl[2].innerHTML;
console.log("this.$refs.liEl = ",this.$refs.liEl);
}
}
};
</script>
Vue 'রেফ' অ্যাট্রিবিউট টিউটোরিয়াল
Vue-তে 'রেফ' অ্যাট্রিবিউট সম্পর্কে আপনার বোঝার পরীক্ষা করার জন্য এই টিউটোরিয়ালটি ব্যবহার করে দেখুন।