How to render content into slot
P粉877114798
P粉877114798 2023-08-18 00:30:15
0
1
561
<p>As shown below, I have created a <code>button</code> with a slot in the child component. The name of the slot is <code>slotDigitizePolygonBtnLabel</code>. The <code>button</code> in the child component should have a property called <code>disabilityState</code> to indicate whether the button is disabled. </p> <p>In the parent component, I want to render the child component's <code>button</code> and pass the value of <code>disabilityState</code> from the parent component to the child component. </p> <p>When I run the code, nothing is rendered. Please tell me where is my error and how to fix it. </p> <p><strong>Subcomponent: DigitizePolygonButton.vue</strong></p> <pre class="brush:php;toolbar:false;"><template> <button id="idDigitizePolygonBtn" class="clsDigitizePolygonBtn" :disabilityState="isDigitizePolygonBtnDisabled"> <slot name="slotDigitizePolygonBtnLabel">text</slot> </button> </template> <script> export default { setup(props) { return { digitizePolygonBtnDisabilityState: props.isDigitizePolygonBtnDisabled, }; }, props: { isDigitizePolygonBtnDisabled: { type: Boolean, required: true, default: false, }, }, }; </script></pre> <p><strong>Parent component</strong>:</p> <pre class="brush:php;toolbar:false;"><template v-slot:slotDigitizePolygonBtnLabel> <DigitizePolygonButton :disabilityState="false"> test </DigitizePolygonButton> </template></pre>
P粉877114798
P粉877114798

reply all(1)
P粉278379495

You should change some properties in your code You wrote disabilityState instead of disabledState

<template>
<button id="idDigitizePolygonBtn" class="clsDigitizePolygonBtn" :disabled="disabledState">
<slot name="slotDigitizePolygonBtnLabel">text</slot>
</button>
</template>

<script>
export default {
    setup(props) {
        return {
            disabledState: props.isDigitizePolygonBtnDisabled,
        };
    },
    props: {
        isDigitizePolygonBtnDisabled: {
            type: Boolean,
            required: true,
            default: false,
        },
    },
};
</script>

In your parent component, you should pass the isDigitizePolygonBtnDisabled property to the child component instead of disabledState. Changes made to parent component:

<template>
    <DigitizePolygonButton :isDigitizePolygonBtnDisabled="false">
        <template v-slot:slotDigitizePolygonBtnLabel>
            <button>测试按钮</button>
        </template>
    </DigitizePolygonButton>
</template>

Making these changes should work fine.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template