Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setActiveTab in UiTabs doesn't work in mounted() hook #486

Open
Alona2156 opened this issue Feb 23, 2020 · 4 comments
Open

setActiveTab in UiTabs doesn't work in mounted() hook #486

Alona2156 opened this issue Feb 23, 2020 · 4 comments

Comments

@Alona2156
Copy link

I need to change selected tab programmatically when navigating to component containing UiTabs. Once the component is mounted I should have access to this.$refs, but when UiTabs call findTabById(id) this line this.$refs.tabHeaders.length tells "TypeError: Cannot read property 'length' of undefined"

@JosephusPaye
Copy link
Owner

There's another ref in UiTabs for the tab headers. I'm guessing that when your mounted hook is called that ref isn't ready yet.

From the docs, it says:

Note that mounted does not guarantee that all child components have also been mounted. If you want to wait until the entire view has been rendered, you can use vm.$nextTick inside of mounted:

So you can wrap your call to findTabById() in $nextTick():

mounted: function () {
  this.$nextTick(function () {
    // Code that will run only after the
    // entire view has been rendered
  })
}

@Alona2156
Copy link
Author

Unfortunately that didn't solve the issue, I still have the same error. I understand that all views must be rendered and available inside nextTick, but that's not the case.

@JosephusPaye
Copy link
Owner

Hmmm, don't know why the internal component refs are not available in mounted + nextTick.

Does it work when you run it a little bit after mounting? E.g. with a button that you click to change the active tab. If so then it's definitely a refs rendering issue, and you could use a short (maybe 300ms) timeout in mounted to change the active tab, as a workaround.

Need to figure out what's going wrong with the refs though and fix that.

@Alona2156
Copy link
Author

Yes, after mounting it works fine, I'm watching a value in Vuex, when it changes I change tab programmatically. That works quite fine. The problem appears only inside mounted hook. If the app is entered not on main route, but on a route that corresponds to a certain tab, I need the tab to change accordingly. Currently I use 1s timeout to avoid getting error, but that's more like a hack than solution. And sometimes I still see an error, so 1s is not always working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants