javascript - How do I reference a Svelte component's parent component? - Stack Overflow

Per the Svelte documentation on Props I am using props to pass a reference to the parent ponent to a ch

Per the Svelte documentation on Props I am using props to pass a reference to the parent ponent to a child.

Props, short for 'properties', are the means by which you pass data down from a parent to a child ponent

That's exactly what I want to do. Here is a Svelte REPL with my code, that is also copied below:

My parent is App.html:

<div class='widget-container'>
    <Widget foo bar="static" {baz}/>
</div>

<script>
    import Widget from './Widget.html';

    export default {
        data: function(){ 
            return {
                baz: 'click me and check the console'
            }
        },
        ponents: {
            Widget
        }
    };
</script>

The child ponent is Widget.html:

<p>foo: {foo}</p>
<p>bar: {bar}</p>
<p>baz: {baz}</p>

<script>
    export default {
        oncreate: function(){
            window.document.body.addEventListener('click', function(event){
                console.log(`Clicked!, ${baz}`)
            });
        }
    }

</script>   

Thanks to the props, the HTML <p> elements can clearly reference the parent. However how can I reference the values in the parent ponent in the child ponent's JavaScript?

Per the Svelte documentation on Props I am using props to pass a reference to the parent ponent to a child.

Props, short for 'properties', are the means by which you pass data down from a parent to a child ponent

That's exactly what I want to do. Here is a Svelte REPL with my code, that is also copied below:

My parent is App.html:

<div class='widget-container'>
    <Widget foo bar="static" {baz}/>
</div>

<script>
    import Widget from './Widget.html';

    export default {
        data: function(){ 
            return {
                baz: 'click me and check the console'
            }
        },
        ponents: {
            Widget
        }
    };
</script>

The child ponent is Widget.html:

<p>foo: {foo}</p>
<p>bar: {bar}</p>
<p>baz: {baz}</p>

<script>
    export default {
        oncreate: function(){
            window.document.body.addEventListener('click', function(event){
                console.log(`Clicked!, ${baz}`)
            });
        }
    }

</script>   

Thanks to the props, the HTML <p> elements can clearly reference the parent. However how can I reference the values in the parent ponent in the child ponent's JavaScript?

Share Improve this question edited Jul 1, 2019 at 7:53 Thaddeus Albers 4,1925 gold badges36 silver badges43 bronze badges asked Jul 25, 2018 at 16:16 mikemaccanamikemaccana 124k110 gold badges432 silver badges534 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

Inside lifecycle hooks and methods, access state with this.get():

<p>foo: {foo}</p>
<p>bar: {bar}</p>
<p>baz: {baz}</p>

<script>
    export default {
        oncreate: function(){
            window.document.body.addEventListener('click', () => {
                const { baz } = this.get();
                console.log(`Clicked!, ${baz}`)
            });
        }
    }l
</script> 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744109082a4558865.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信