【问题标题】:Vue. Change the contents of one slot from anotherVue。从另一个插槽更改一个插槽的内容
【发布时间】:2019-04-12 07:10:12
【问题描述】:

我有一个带有特定插槽的组件。

    <v-split-container ref="splitContainer" class="panel-panel">
        <template slot="left">
           Test
        </template>
        <template slot="right">
            <router-view></router-view> //Show Compontent A
        </template>
        <template slot="bottom"> //Slot B
            Hello
        </template>
    </v-split-container>

我可以从组件 A 中通过调用组件内部的函数来更改插槽 B 的内容吗?

【问题讨论】:

    标签: vue.js vuejs2


    【解决方案1】:

    您好,您可以使用Scoped Slot 来完成。我已经为您创建了一个示例。请注意我使用的 v-slots(Some context here) 只能从 v2.6 使用。

    请看代码示例:https://codesandbox.io/s/2030jo20j0?fontsize=14

    子组件

    <template>
      <div>
        <div>
          <slot name="msgSlot">{{msg}}</slot>
        </div>
        <br>
        <slot name="bottom" :updateMsg="updateMsg"></slot>
      </div>
    </template>
    
    <script>
    export default {
      name: "HelloWorld",
      data: () => ({
        msg: "Default Message"
      }),
      methods: {
        updateMsg(text) {
          this.msg = text;
        }
      }
    };
    </script>
    

    父组件

    <template>
      <div id="app">
        <HelloWorld>
          <template v-slot:msgSlot></template>
          <template v-slot:bottom="{updateMsg}">
            <input type="text" v-model="msg">
            <button @click="updateMsg(msg)">Change Msg</button>
          </template>
        </HelloWorld>
      </div>
    </template>
    
    <script>
    import HelloWorld from "./components/HelloWorld";
    
    export default {
      name: "App",
      data: () => ({
        msg: ""
      }),
      components: {
        HelloWorld
      }
    };
    </script>
    

    【讨论】:

    • 如果您需要添加复杂的文本和组件?
    • 你应该仍然可以做到。您需要给我一个工作代码示例以进一步帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2019-06-08
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多