VueDay02 用v-if和v-show实现的一个简单的tab切换
v-if实现tab切换代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-- view -->
<div id="app">
<h3 v-if="tab==1">首页</h3>
<h3 v-else-if="tab==2">新闻页</h3>
<h3 v-else>个人中心</h3>
<button @click="tabChange" data-id="1" type="button">首页</button>
<button @click="tabChange" data-id="2" type="button">新闻</button>
<button @click="tabChange" data-id="3" type="button">个人</button>
</div>
<div id="app-3">
<p v-if="seen">现在你看到我了</p>
</div>
<script type="text/javascript">
let app =new Vue({
el:"#app",
data:{
tab:1
},
methods:{
tabChange:function(e){
console.log(e)
let tabid = e.target.dataset.id
console.log(this)
console.log(app)
app.tab = tabid
// this.tab = tabid
}
},
})
var app3 = new Vue({
el:'#app-3',
data:{
seen:true
}
})
</script>
</body>
</html>
v-show实现tab切换代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-- view -->
<div id="app">
<h3 v-show="tab==1">首页</h3>
<h3 v-show="tab==2">新闻页</h3>
<h3 v-show="tab==3" >个人中心</h3>
<button @click="tabChange" data-id="1" type="button">首页</button>
<button @click="tabChange" data-id="2" type="button">新闻</button>
<button @click="tabChange" data-id="3" type="button">个人</button>
</div>
<div id="app-3">
<p v-if="seen">现在你看到我了</p>
</div>
<script type="text/javascript">
let app =new Vue({
el:"#app",
data:{
tab:1
},
methods:{
tabChange:function(e){
// console.log(e)
let tabid = e.target.dataset.id
// console.log(this)
// console.log(app)
// app.tab = tabid
this.tab = tabid
}
},
})
var app3 = new Vue({
el:'#app-3',
data:{
seen:true
}
})
</script>
</body>
</html>
这里还是用v-show,涉及到来回的渲染页面,用v-show比用v-if好