Vue <component> Element
উদাহরণ
আপনার নিজস্ব Vue সার্ভার পান
একটি ডাইনামিক কম্পোনেন্ট তৈরি করতে is অ্যাট্রিবিউটের সাথে একটি নেস্টেড <component> উপাদান ব্যবহার করে।
<template>
<h1>Dynamic Components</h1>
<p>App.vue switches between which component to show.</p>
<button @click="toggleValue = !toggleValue">Switch component</button>
<component :is="activeComp"></component>
</template>
নীচে আরো উদাহরণ দেখুন.
সংজ্ঞা এবং প্রয়োগ
বিল্ট-ইন <component> এলিমেন্টটি একটি HTML এলিমেন্ট তৈরি করতে ব্যবহৃত হয় বা বিল্ট-ইন ইজ অ্যাট্রিবিউটের সাথে Vue কম্পোনেন্ট ব্যবহার করা হয়।
এইচটিএমএল উপাদান
একটি HTML উপাদান তৈরি করতে
Vue উপাদান
Vue কম্পোনেন্ট রেন্ডার করতে
HTML উপাদান:
একটি <component> উপাদান দিয়ে একটি HTML উপাদান তৈরি করতে, is attributeটি HTML এলিমেন্টের নামের সমান সেট করা হয় যা আমরা তৈরি করতে চাই, হয় সরাসরি (উদাহরণ 1) অথবা গতিশীলভাবে v-bind ব্যবহার করে (উদাহরণ 2)।
Vue উপাদান:
একটি <component> উপাদানের সাথে একটি Vue কম্পোনেন্ট রেন্ডার করতে, is attributeটি Vue কম্পোনেন্টটির নামের সমান সেট করা হয় যা আমরা তৈরি করতে চাই, হয় সরাসরি (উদাহরণ 3) অথবা ডায়নামিক কম্পোনেন্ট তৈরি করতে v-bind ব্যবহার করে (উদাহরণ 4)।
দ্রষ্টব্য:
একটি গতিশীল উপাদান তৈরি করার সময়, বিল্ট-ইন <KeepAlive> উপাদানটি নিষ্ক্রিয় উপাদানগুলির অবস্থা মনে রাখতে <component> উপাদানের চারপাশে ব্যবহার করা যেতে পারে। (উদাহরণ 5)
গুরুত্বপূর্ণ নোট:
ভি-মডেল কৌশলটি <component> উপাদান ব্যবহার করে তৈরি করা নেটিভ HTML ফর্ম ইনপুট ট্যাগের (যেমন <input> বা <option>) সাথে কাজ করে না। (উদাহরণ 7)
প্রপস
| প্রপস | ব্যাখ্যা |
|---|---|
| is | প্রয়োজনীয় সক্রিয় হতে উপাদানটির সমান সেট করুন, অথবা তৈরি করা HTML উপাদানের সমান সেট করুন৷ |
আরো উদাহরণ
উদাহরণ 1
একটি <div> উপাদান তৈরি করতে নেস্টেড <component> উপাদান ব্যবহার করে।
<template>
<h2>Example Built-in 'component' Element</h2>
<p>The component element is rendered as a div element with is="div":</p>
<component is="div">This is a DIV element</component>
</template>
<style scoped>
div {
border: solid black 1px;
background-color: lightgreen;
}
</style>
উদাহরণ 2
বিল্ট-ইন <component> উপাদান ব্যবহার করে একটি অর্ডার করা তালিকা এবং একটি ক্রমবিহীন তালিকার মধ্যে স্যুইচ করুন।
<template>
<h2>Example Built-in 'component' Element</h2>
<p>Using the component element to toggle between an ordered list (ol), and an unordered list (ul):</p>
<button v-on:click="toggleValue = !toggleValue">Toggle</button>
<p>Animals from around the world</p>
<component :is="tagType">
<li>Kiwi</li>
<li>Jaguar</li>
<li>Bison</li>
<li>Snow Leopard</li>
</component>
</template>
<script>
export default {
data() {
return {
toggleValue: true
}
},
computed: {
tagType() {
if (this.toggleValue) {
return 'ol'
}
else {
return 'ul'
}
}
}
}
</script>
উদাহরণ 3
বিল্ট-ইন <component> এলিমেন্ট ব্যবহার করে কম্পোনেন্টের নাম is attribute-এ পাস করে কম্পোনেন্ট রেন্ডার করা।
App.vue:
<template>
<h2>Example Built-in 'is' Attribute</h2>
<p>The component element below is set to be a component by the use of 'is="child-comp"'.</p>
<component is="child-comp"></component>
</template>
ChildComp.vue:
<template>
<div>
<h3>ChildComp.vue</h3>
<p>This is the child component</p>
</div>
</template>
<style scoped>
div {
border: solid black 1px;
background-color: lightgreen;
padding: 10px;
max-width: 250px;
margin-top: 20px;
}
</style>
উদাহরণ 4
বিল্ট-ইন <component> উপাদান ব্যবহার করে একটি গতিশীল উপাদান তৈরি করা যা আমরা দুটি উপাদানের মধ্যে পরিবর্তন করতে পারি।
<template>
<h1>Dynamic Components</h1>
<p>App.vue switches between which component to show.</p>
<button @click="toggleValue = !toggleValue">Switch component</button>
<component :is="activeComp"></component>
</template>
<script>
export default {
data () {
return {
toggleValue: true
}
},
computed: {
activeComp() {
if(this.toggleValue) {
return 'comp-one'
}
else {
return 'comp-two'
}
}
}
}
</script>
<style>
#app {
width: 350px;
margin: 10px;
}
#app > div {
border: solid black 2px;
padding: 10px;
margin-top: 10px;
}
</style>
উদাহরণ 5
একটি বিল্ট-ইন <KeepAlive> উপাদান <component> উপাদানের চারপাশে ব্যবহার করা হয় যাতে উপাদানগুলির মধ্যে পরিবর্তন করার সময় ইনপুটগুলি মনে রাখা হয়।
<template>
<h1>Dynamic Components</h1>
<p>App.vue switches between which component to show.</p>
<p>With the <KeepAlive> tag the components now remember the user inputs.</p>
<button @click="toggleValue = !toggleValue">Switch component</button>
<KeepAlive>
<component :is="activeComp"></component>
</KeepAlive>
</template>
<script>
export default {
data () {
return {
toggleValue: true
}
},
computed: {
activeComp() {
if(this.toggleValue) {
return 'comp-one'
}
else {
return 'comp-two'
}
}
}
}
</script>
<style>
#app {
width: 350px;
margin: 10px;
}
#app > div {
border: solid black 2px;
padding: 10px;
margin-top: 10px;
}
h2 {
text-decoration: underline;
}
</style>
উদাহরণ 6
কোন উপাদান সক্রিয় হওয়া উচিত তা পরিবর্তন করতে is attribute এবং তিনটি কম্পোনেন্ট এক্সপ্রেশন সহ <component> উপাদান ব্যবহার করে।
<template>
<h1>Dynamic Components</h1>
<p>Refresh the page and there is a chance the dynamic component will toggle.</p>
<component :is="Math.random() > 0.5 ? 'comp-one' : 'comp-two'"></component>
</template>
<style>
#app {
width: 350px;
margin: 10px;
}
#app > div {
border: solid black 2px;
padding: 10px;
margin-top: 10px;
}
</style>
উদাহরণ 7
প্রমাণ করা যে v-মডেল কৌশলটি <component> উপাদান ব্যবহার করে তৈরি করা <input> উপাদানগুলির সাথে কাজ করে না।
<template>
<h1>Dynamic Components</h1>
<p><mark>The v-model directive does not work with input element created with the component element.</mark></p>
<hr>
<p>Does not work, not updating:</p>
<component is="input" type="number" v-model="inpVal1"></component> (try to change value)
<p class="pResult1">inpVal1: {{ inpVal1 }}</p>
<hr>
<p>How it should work, updates:</p>
<input type="number" v-model="inpVal2"> (try to change value)
<p class="pResult2">inpVal2: {{ inpVal2 }}</p>
</template>
<script>
export default {
data() {
return {
inpVal1: 4,
inpVal2: 7,
}
}
}
</script>
<style>
#app {
width: 350px;
margin: 10px;
}
.pResult1 {
background-color: lightpink;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
padding: 5px;
}
.pResult2 {
background-color: lightgreen;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
padding: 5px;
}
</style>
নিয়ন্ত্রণ:
v-মডেল কৌশলটি <component> উপাদান ব্যবহার করে তৈরি ইনপুট উপাদানগুলির সাথে কাজ করে না। এটি Vue-এর একটি নিয়ন্ত্রণ।
ভিউ টিউটোরিয়াল
ব্যায়াম দিয়ে নিজেকে পরীক্ষা করুন
<component> উপাদান ব্যবহার করে একটি গতিশীল উপাদান তৈরি করতে কোন বৈশিষ্ট্য প্রয়োজন?
<component : ="activeComp"></component>