做点赞和讨厌的按钮功能
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<counter heading="Likes" color="green"></counter>
<counter heading="Dislikes" color="red"></counter>
</div>
<template id="counter-template">
<div>
<h1> {{ heading }} </h1>
<button @click="count+=1" style="background: {{color}}"> {{ count }} </button>
</div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
<script>
Vue.component('counter',{
template:'#counter-template',
props:['heading'],
data:function(){
return {count:0};
}
});
new Vue({
el:"#app"
});
</script>
</body>
</html>
做level判断的功能
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
level : {{level}}
</div>
<script src="js/vue.min.js"></script>
<script>
new Vue({
el:"#app",
data:{
a:200
},
computed:{
level:function(){
if(this.a<=100){
return '普通会员';
}
return 'VIP会员'
}
}
})
</script>
</body>
</html>
做数组里的值的展示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<ul>
<li v-for="task in tasks">{{task}}</li>
</ul>
</div>
<script src="js/vue.min.js"></script>
<script>
new Vue({
el:'#app',
data:{
tasks:[
'Ronaldo',
'Messi',
'Nermal'
]
}
});
</script>
</body>
</html>
做一些点击处理
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.shanchuxian{
text-decoration: line-through;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li @click="dianji(task)"
:class="task.completed?'shanchuxian':''"
v-for="task in tasks">{{task.body}}
</li>
</ul>
</div>
<script src="js/vue.min.js"></script>
<script>
new Vue({
el:"#app",
data:{
tasks:[
{body: '天津泰达',completed:false},
{body: '北京国安',completed:true},
{body: '上海申花',completed:false}
]
},
methods:{
dianji:function(task){
task.completed = !task.completed;
}
}
});
</script>
</body>
</html>
多功能的展示
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.shanchuxian{
text-decoration: line-through;
}
</style>
</head>
<body>
<div id="app">
<task-app :list="tasks">
</task-app>
<task-app :list="[{body:'盐城权健',completed:flase}]">
</task-app>
</div>
<template id="task-template">
<h1>未完成的数量:<span v-show="remaining">{{remaining}}</span></h1>
<ul>
<li @click="dianji(task)"
:class="task.completed?'shanchuxian':''"
v-for="task in list">
{{task.body}}
<strong @click="delete(task)">X</strong>
</li>
</ul>
</template>
<!--<script src="js/vue.min.js"></script>-->
<script src="http://cdn.bootcss.com/vue/1.0.14/vue.js"></script>
<script>
Vue.component('task-app',{
template:'#task-template',
props:['list'],
methods:{
dianji:function(task){
task.completed = !task.completed;
},
delete:function(task){
this.list.$remove(task);
}
},
computed:{
remaining:function(){
return this.list.filter(function(task){
return !task.completed;
}).length;
}
}
});
new Vue({
el:"#app",
data:{
tasks:[
{body: '天津泰达',completed:false},
{body: '北京国安',completed:true},
{body: '上海申花',completed:false}
]
},
});
</script>
</body>
</html>