<component>


Misol

Dinamik komponent yaratish uchun is atributiga ega o‘rnatilgan <component> elementidan foydalanish.

<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>
Misolni ishga tushirish »

Quyida ko‘proq misollarni ko‘ring.


ULASHISH

Ta’rif va qo‘llanilishi

O‘rnatilgan <component> elementi o‘rnatilgan is atributi bilan birgalikda HTML elementi yoki Vue komponentini yaratish uchun ishlatiladi.

HTML elementi: <component> elementi yordamida HTML elementi yaratish uchun is atributiga biz yaratmoqchi bo‘lgan HTML elementining nomi beriladi: yoki to‘g‘ridan-to‘g‘ri (1-misol), yoki v-bind yordamida dinamik tarzda (2-misol).

Vue komponenti: <component> elementi yordamida Vue komponentini render qilish uchun is atributiga biz yaratmoqchi bo‘lgan Vue komponentining nomi beriladi: yoki to‘g‘ridan-to‘g‘ri (3-misol), yoki dinamik komponent yaratish uchun v-bind yordamida dinamik tarzda (4-misol).

Dinamik komponent yaratishda faol bo‘lmagan komponentlarning state’ini eslab qolish uchun <component> elementini o‘rnatilgan <KeepAlive> komponenti bilan o‘rab olish mumkin. (5-misol)

Dinamik komponentdagi faol komponentni is atributida ternar ifoda ishlatib ham almashtirish mumkin. (6-misol)

Eslatma: v-model direktivasi <component> elementi yordamida yaratilgan oddiy (native) HTML forma kiritish teglari (masalan, <input> yoki <option>) bilan ishlamaydi. (7-misol)


Props

Prop Tavsif
is Majburiy. Faol bo‘lishi kerak bo‘lgan komponentga yoki yaratiladigan HTML elementiga teng qilib o‘rnatiladi.

Ko‘proq misollar

1-misol

<div> elementini yaratish uchun o‘rnatilgan <component> elementidan foydalanish.

<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>
Misolni ishga tushirish »

2-misol

Tartiblangan ro‘yxat va tartiblanmagan ro‘yxat o‘rtasida almashish uchun o‘rnatilgan <component> elementidan foydalanish.

<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>
Misolni ishga tushirish »

3-misol

is atributiga komponent nomini berish orqali komponentni render qilish uchun o‘rnatilgan <component> elementidan foydalanish.

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>
Misolni ishga tushirish »

4-misol

Ikki komponent o‘rtasida almashinishimiz mumkin bo‘lgan dinamik komponentni yaratish uchun o‘rnatilgan <component> elementidan foydalanish.

<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>
Misolni ishga tushirish »

5-misol

Komponentlar almashtirilganda kiritilgan qiymatlarni eslab qolish uchun <component> elementi o‘rnatilgan <KeepAlive> komponenti bilan o‘rab olinadi.

<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>
Misolni ishga tushirish »

6-misol

Qaysi komponent faol bo‘lishini almashtirish uchun is atributli <component> elementi va ternar ifodadan foydalanish.

<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>
Misolni ishga tushirish »

7-misol

v-model direktivasi <component> elementi yordamida yaratilgan <input> elementlari bilan ishlamasligini namoyish qilish.

<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>
Misolni ishga tushirish »


Tegishli sahifalar

Vue darsligi: Vue komponentlari

Vue darsligi: Dinamik komponentlar

Vue darsligi: Vue forma kiritish maydonlari

Vue darsligi: Vue v-model direktivasi

Vue ma’lumotnomasi: Vue is atributi

Vue ma’lumotnomasi: Vue v-bind direktivasi

Vue ma’lumotnomasi: Vue v-model direktivasi


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!