亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Vue 組件包裝到另一個 Vue 組件中

將 Vue 組件包裝到另一個 Vue 組件中

森欄 2022-05-26 10:38:18
我有一個側邊欄組件,我想使用兩次,但其中包含不同的其他組件。我真的很想這樣工作:<template>  <Sidebar class="left-0">    <ExampleComponentA/>  </Sidebar>  <Content/>  <Sidebar class="right-0">    <ExampleComponentB/>  </Sidebar></template>Vue有可能還是我真的需要一個SidebarLeft和一個SidebarRight組件?<template>  <SidebarLeft class="left-0"/> <!-- <ExampleComponentA/> is in this Component -->  <Content/>  <SidebarRight class="right-0"/> <!-- <ExampleComponentB/> is in this Component --></template>我為什么要那樣做?好吧,我的側邊欄的設置是相同的,只是content不同。所以我不需要復制洞邊欄。
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

這是一個沙盒示例:link。


嘗試使用slots來達到同樣的效果。


// App.vue (Parent component)

<template>

  <div id="app">

    <HelloWorld msg="Hello Vue in CodeSandbox!"> 

      <random slot="random"/> // Component 2 being called inside Component 1

    </HelloWorld>

  </div>

</template>

<script>

import random from "./components/random";

import HelloWorld from "./components/HelloWorld";


export default {

  name: "App",

  components: {

    HelloWorld,

    random

  }

};

</script>



// HelloWorld.vue (Component 1)

<template>

  <div class="hello">

    <h1>{{ msg }}</h1>

    <slot name="random"></slot>

  </div>

</template>


<script>

export default {

  name: "HelloWorld",

  props: {

    msg: String

  }

};

</script>


// random.vue (Component 2)

<template>

  <div>I am random</div>

</template>


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 316 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號