Event from parent to child component

前端 未结 1 985
执笔经年
执笔经年 2020-12-17 01:04

I have event that is generated in parent component and child has to react to it. I know that this is not recommended approach in vuejs2 and i have to do a

相关标签:
1条回答
  • 2020-12-17 01:56

    Just have a variable (call it moreLoaded) that you increment each time loadMore is called. Pass that and currentPosition to your search component as props. In Search, you can watch moreLoaded and take action accordingly.

    Update
    Hacky? My solution? Well, I never! ;)

    You could also use a localized event bus. Set it up something like this:

    export default {
        components: {
            Search
        },
        data() {
            return {
                bus: new Vue(),
                transactions: [], 
                currentPosition: 0 
            }
        },
        methods: {
            loadMore() {
                this.bus.$emit('loadMore', {
                    currentPosition: this.currentPosition
                });
            }
        }
    }
    

    and pass it to Search:

    <search :bus="bus"></search>
    

    which would take bus as a prop (of course), and have a section like

    created() {
        this.bus.$on('loadMore', (args) => {
            // do something with args.currentPosition
        });
    }
    
    0 讨论(0)
提交回复
热议问题