欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue動(dòng)態(tài)組件component標(biāo)簽的用法大全

 更新時(shí)間:2022年08月20日 11:47:22   作者:IT利刃出鞘  
這篇文章主要介紹了Vue動(dòng)態(tài)組件component標(biāo)簽的用法,在Vue中,可以通過(guò)component標(biāo)簽的is屬性動(dòng)態(tài)指定標(biāo)簽,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

簡(jiǎn)介

說(shuō)明

本文介紹Vue的動(dòng)態(tài)組件的用法。

在Vue中,可以通過(guò)component標(biāo)簽的is屬性動(dòng)態(tài)指定標(biāo)簽,例如:

<component :is="componentName"></component>

此時(shí),componentName的值是什么,就會(huì)引入什么組件。

官網(wǎng)網(wǎng)址

https://v2.cn.vuejs.org/v2/guide/components.html#動(dòng)態(tài)組件

示例

路由設(shè)置

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Parent from '../components/Parent'
 
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    name: 'Parent',
    component: Parent
  }
]
 
const router = new VueRouter({
  routes
})
 
export default router

父組件

components/Parent.vue

<template>
  <div class="outer">
    <h2>這是父組件</h2>
    <component :is="componentName" :propA="propAValue"></component>
  </div>
</template>
 
<script>
import ChildA from './ChildA'
import ChildB from './ChildB'
 
export default {
  name: 'Parent',
  components: { ChildA, ChildB },
  data () {
    return {
      componentName: 'ChildB',
      propAValue: 'aaa'
    }
  }
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

子組件

components/ChildA.vue

<template>
  <div class="outer">
    <h3>這是ChildA</h3>
    <div>傳入進(jìn)來(lái)的propA值為:{{propA}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'ChildA',
  props: ['propA']
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

components/ChildA.vue

<template>
  <div class="outer">
    <h3>這是ChildB</h3>
    <div>傳入進(jìn)來(lái)的propA值為:{{propA}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'ChildB',
  props: ['propA']
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

測(cè)試

訪問(wèn):http://localhost:8080/

到此這篇關(guān)于Vue動(dòng)態(tài)組件component標(biāo)簽的用法大全的文章就介紹到這了,更多相關(guān)Vue--動(dòng)態(tài)組件component標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論