简体   繁体   中英

Access the prop of the root component in VueJS

In my page, I have a set of components and subcomponents like that:

<TournamentTabs :tournament="{{ json_encode($tournament) }}">    
   <TournamentTab>
      <TournamentTabGeneral></TournamentTabGeneral>
      <TournamentTabVenue></TournamentTabVenue>
      etc.
   </TournamentTab>
</TournamentTabs>

Right now, I use Laravel for getting $tournament value.

Now, I would like prop tournament to be available to all TournamentTabs children, but when accessing this.tournament , I get undefined

How should I access tournament value in all chidlren???

There are many ways to do it.

Firstly, the TournamentTabs is not a root component as I can see. Root component - it's the component where Vue instance mounts (in most cases div#app ) that can be accessible via this.$root anywhere.

You can access it via this.$parent.tournament , but this is not a best way to do it, because you will end up like this.$parent.$parent.$parent....tournament if you need to access property from deeply nested components.

You can try vuex library to implement the central application storage.

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