Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active November 21, 2022 17:56
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save JeffreyWay/f844ca4dd1887d566759849665068162 to your computer and use it in GitHub Desktop.
Save JeffreyWay/f844ca4dd1887d566759849665068162 to your computer and use it in GitHub Desktop.
vuecasts.com Episode 11 source
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.2.3/css/bulma.css">
<style>
body { padding-top: 40px; }
</style>
</head>
<body>
<div id="root" class="container">
<tabs>
<tab name="About Us" :selected="true">
<h1>Here is the content for the about us tab.</h1>
</tab>
<tab name="About Our Culture">
<h1>Here is the content for the about our culture tab.</h1>
</tab>
<tab name="About Our Vision">
<h1>Here is the content for the about our vision tab.</h1>
</tab>
</tabs>
</div>
<script src="https://unpkg.com/vue@2.1.3/dist/vue.js"></script>
<script src="main.js"></script>
</body>
</html>
Vue.component('tabs', {
template: `
<div>
<div class="tabs">
<ul>
<li v-for="tab in tabs" :class="{ 'is-active': tab.isActive }">
<a :href="tab.href" @click="selectTab(tab)">{{ tab.name }}</a>
</li>
</ul>
</div>
<div class="tabs-details">
<slot></slot>
</div>
</div>
`,
data() {
return { tabs: [] };
},
created() {
this.tabs = this.$children;
},
methods: {
selectTab(selectedTab) {
this.tabs.forEach(tab => {
tab.isActive = (tab.href == selectedTab.href);
});
}
}
});
Vue.component('tab', {
template: `
<div v-show="isActive"><slot></slot></div>
`,
props: {
name: { required: true },
selected: { default: false }
},
data() {
return {
isActive: false
};
},
computed: {
href() {
return '#' + this.name.toLowerCase().replace(/ /g, '-');
}
},
mounted() {
this.isActive = this.selected;
},
});
new Vue({
el: '#root'
});
@smwbtech
Copy link

smwbtech commented Dec 6, 2017

Thans for this example!

@Brnovich
Copy link

Brnovich commented Jan 4, 2018

Not working for me within my Laravel project.
When I look in Vue (inspecting element in Chrome) I don't see the isActive within props clicking on one of the tabs.
2018-01-04_19-40-45

@herbowicz
Copy link

herbowicz commented Feb 24, 2018

isActive is data, not props
image

@ghufranulhaq
Copy link

@Brnovich
your sample is showing contents of all the four tabs.
Try this inside selectTab method

        this.tabs.forEach(tab => {
            tab.isActive = (tab.name == selectedTab.name);

@sonalighadai
Copy link

remove this :class="{ 'is-active': tab.isActive } from code to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment