鍍金池/ 問答/HTML/ vue 具名插槽的名字可以動態(tài)綁定嗎

vue 具名插槽的名字可以動態(tài)綁定嗎

1.子組件:
clipboard.png

2.父組件:
clipboard.png

3.描述:
我嘗試在父組件里面動態(tài)綁定插槽的名字
clipboard.png

這樣是不成功的,但是我想實(shí)現(xiàn)點(diǎn)擊不同的標(biāo)簽,插入不同的內(nèi)容那樣的效果,請問怎么實(shí)現(xiàn)?
ε=(′ο`*)))

回答
編輯回答
耍太極

樓主你好,我寫了個Demo,可以實(shí)現(xiàn)動態(tài)綁定的呀,以下是我的Demo代碼。

父組件

<template>
  <div>
    <slot_>
      <h1 :slot="name">hello world!</h1>
    </slot_>
    <button @click="change">click</button>
  </div>
</template>

<script>
import slot_ from './slot.vue'
export default {
    components: { slot_ },
    data(){
      return {
        name: '',
        status: false
      }
    },
    mounted(){
      this.name = 'h1'
    },
    methods: {
      change(){
        this.name = this.status 
          ? 'h1' : 'h2';
        this.status = !this.status;
      }
    }
}
</script>
<style>
*{margin: 0}
</style>

子組件

<template>
  <div>
      <div class="header">
          <slot name="h1"></slot>
      </div>
      <div class="footer">
          <slot name="h2"></slot>
      </div>
  </div>
</template>
<style scoped>
.header{
    background-color: pink;
    width: 200px;
    height: 60px;
}
.footer{
    width: 200px;
    height: 60px;
    background-color: green;
}
</style>
2018年1月5日 07:08