admin管理员组文章数量:1416645
I have tabs like this...
<v-tabs v-model="tab" v-on:change="tabbed()" fixed-tabs>
<v-tab ripple>Tab A</v-tab>
<v-tab ripple>Tab B</v-tab>
<v-tab ripple>Tab C</v-tab>
// tab items, etc
I'd like the URL path to determine the tab, and I'd like a tab change to change the path. I wasn't able to do this with path (because I'm new), but I thought I could approximate what I want with a query, like /mypath?tab=2
data: () => ({
tab: null
}),
methods: {
tabbed () {
console.log(`we tabbed, now tab is ${this.tab} and route is ${JSON.stringify(this.$route.path)}`)
this.$router.replace({ path: this.$route.path, query: { tab: this.tab } })
}
},
created () {
console.log(`tab is ${this.$route.query.tab}`)
this.tab = this.$route.query.tab
}
I thought this would do the trick: on create, get the tab from the query and set the tab model. On tab change, get the tab from the tab model and set the query. But, when I navigate to /mypath?tab=2
, or any tab, I end up on the 0th tab and get console output like this:
tab is 2
we tabbed, now the tab is 0 and route is "/mypath"
Something about mounting changes the tab back to zero??
Can I make this work with routes, so I can go to "/mypath/tab-a"
Can I at least make this work with queries? It seems like my code is correct, but I don't understand why it fails to work
I have tabs like this...
<v-tabs v-model="tab" v-on:change="tabbed()" fixed-tabs>
<v-tab ripple>Tab A</v-tab>
<v-tab ripple>Tab B</v-tab>
<v-tab ripple>Tab C</v-tab>
// tab items, etc
I'd like the URL path to determine the tab, and I'd like a tab change to change the path. I wasn't able to do this with path (because I'm new), but I thought I could approximate what I want with a query, like /mypath?tab=2
data: () => ({
tab: null
}),
methods: {
tabbed () {
console.log(`we tabbed, now tab is ${this.tab} and route is ${JSON.stringify(this.$route.path)}`)
this.$router.replace({ path: this.$route.path, query: { tab: this.tab } })
}
},
created () {
console.log(`tab is ${this.$route.query.tab}`)
this.tab = this.$route.query.tab
}
I thought this would do the trick: on create, get the tab from the query and set the tab model. On tab change, get the tab from the tab model and set the query. But, when I navigate to /mypath?tab=2
, or any tab, I end up on the 0th tab and get console output like this:
tab is 2
we tabbed, now the tab is 0 and route is "/mypath"
Something about mounting changes the tab back to zero??
Can I make this work with routes, so I can go to "/mypath/tab-a"
Can I at least make this work with queries? It seems like my code is correct, but I don't understand why it fails to work
2 Answers
Reset to default 6Ok, I solved the same by this:
<template>
<el-tabs v-model="activeTab">
<el-tab-pane label="tab A" name="tabA"></el-tab-pane>
<el-tab-pane label="tab B" name="tabB"></el-tab-pane>
<el-tab-pane label="tab C" name="tabC"></el-tab-pane>
</el-tabs>
</template>
<script>
export default {
data() {
return {}
},
puted: {
activeTab: {
set(val) {
let query = {...this.$route.query};
query.tab = val;
this.$router.replace({query: query});
},
get() {
return (this.$route.query.tab || 'tabA');
}
}
}
}
</script>
Maybe helpful to you
I ran into this problem too and fixed it by using parseInt when handling the route:
this.tab = parseInt(this.$route.query.tab)
本文标签: javascriptChange vuetify tab with the route or queryStack Overflow
版权声明:本文标题:javascript - Change vuetify tab with the route or query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744733161a2622166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论