简体   繁体   中英

I am failing when I try to use scoped slots in Vue.js

I have read every tutorial, the vue.js manual and watched video tutorials, but I still can't make scoped-slots work for me. Below is the minimal code I have been testing with. I am clearly missing something, but what. Can somebody who understands this tell me how i need to change this code so it works. Ultimately, I want to be able to reference data collected by the (via ajax) in the inner slot - which ultimately, will be another component.

<!DOCTYPE HTML>
<html>
<head>
    <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'>
    </script>
    <script>
        "use strict"
        window.addEventListener('load', function () {
            Vue.component('comp-onent', {
                data:function () {
                    return {dataitem:"from the 'test' component instance"}
                },
                template:`
                    <dl>
                        <dt>From inside the 'test' component</dt>
                        <dd>{{dataitem}}</dd>
                        <dt>Rendered from the slot</dt>
                        <dd><slot :dataitem="dataitem"></slot></dd>
                    </dl>
                `
            });

            let vm = new Vue({
                el:'#vue-root',
                data:{dataitem:"from the root Vue instance"}
            });
        });
    </script>
</head>
<body>
    <div id='vue-root'>
        <comp-onent "slot-scope"="fromcomponent">
            <p>Inside 'test' invocation</p>
            <dl>
                <dt>From root instance:            </dt><dd>{{dataitem}}</dd>
                <dt>From 'test' component instance:</dt><dd>{{fromcomponent.dataitem}}</dd>
            </dl>
        </comp-onent>
        <dl><dt>Outside of 'test' invocation</dt><dd>{{dataitem}}</dd></dl>
    </div>
</body>
</html>

I considered firing an event to pass the data up to the root Vue instance, but this fails if I have more than one <comp-onent>, so it is not a solution in this case.

I would suggest to start with a simple working example without slots. Try this and play:

 <!DOCTYPE HTML> <html> <head> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'> </script> <script> "use strict" window.addEventListener('load', function () { Vue.component('comp-onent', { props: { dataitem:{ default: "from the 'test' component instance" } }, template:` <dl> <dt>From inside the 'test' component</dt> <dd>{{dataitem}}</dd> </dl> ` }); let vm = new Vue({ el:'#vue-root', data:{dataitem:"from the root Vue instance"} }); }); </script> </head> <body> <div id='vue-root'> <comp-onent dataitem="something else"></comp-onent> <comp-onent :dataitem="dataitem"></comp-onent> </div> </body> </html> 

And try the slots in another step.

I finally tracked down what stops it from working thanks to this jsfiddle: https://jsfiddle.net/dronowar/uyvmtmrt/ slot-scope has to be defined on an element INSIDE the component invocation, not on the component itself, so

<comp-onent slot-scope="comp">
    <p :class="comp.compclass">something</p>
</comp-onent>

DOESN'T WORK BUT

<comp-onent >
    <div slot-scope="comp">
       <p :class="comp.compclass">something</p>
    </div>
</comp-onent>

does work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM