Vue - 将插槽传递给子组件

时间:2018-04-04 22:49:35

标签: javascript vue.js vuejs2

我在下面使用的图片是加载了vue-svg-loader的svg组件(请参阅app.vue

我有两个组件parentchild

  • parent使用child并显示带有文字的孩子的图片。
  • child可以在没有parent的情况下使用,只显示图像。

两者都使用命名槽来显示图像,问题是我想将父组件中的命名槽传递给子组件,但似乎不能按照我想要的方式使用名为slot的vuejs并开始非常混乱,还有另一种解决这个问题的方法吗?

Parent.vue

<template>
    <child>
        <label>Text with image</label>
        <slot name="img">
    </child>
<template>

Child.vue

<template>
    <slot name="img">
<template>

App.vue

<template>
    <parent>
        <mySvg slot="img"/>
    </parent>
<template>
<script>
    import mySvg from 'pathToSvg';
    export default {
        components: {
            mySvg,
        }
    }
</script>

1 个答案:

答案 0 :(得分:2)

尝试使用父组件

<template>
<child>
    <label>Text with image</label>
    <slot slot="img" name="img">
</child>
<template>
相关问题