v-on
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vue太难了</title>
</head>
<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
<input type="button" value="v-on简写" @click="doIt">
<input type="button" value="双击事件" @dblclick="doIt">
<h2 @click="changeFood">{{food}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
food:"西兰花炒蛋"
},
methods:{
doIt:function (){
alert("做It");//alert是js的函数,弹窗
},
changeFood:function (){
this.food+="好好吃!"
}
},
})
</script>
</body>
</html>
计数器的实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计数器</title>
<link rel="stylesheet" href="./css/index.css">
<!--偷偷自己加的样式,属于CSS的内容-->
</head>
<body>
<div id="app">
<div class="input-num">
<button @click="sub">-</button>
<span>{{num}}</span>
<button @click="add">+</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
num:1
},
methods:{
add:function (){
if(this.num<10)
{
this.num++;
}else{
alert("别点了,最大了")
}
},
sub:function (){
if(this.num>0)
{
this.num--;
}else{
alert("别点了,最小了")
}
}
}
})
</script>
</body>
</html>
v-show指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-show 指令</title>
</head>
<body>
<div id="app">
<input type="button" value="切换显示状态" @click="changeIsShow">
<input type="button" value="累加年龄" @click="addAge">
<img v-show="isShow" src="//www.runoob.com/images/logo.png" alt="">
<!--//本地插入图片有点小难-->
<img v-show="age>=18" src="//www.runoob.com/images/logo.png" alt="">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el:"#app",
data:{
isShow:false,
age:17
},
methods:{
changeIsShow:function (){
this.isShow = !this.isShow;
},
addAge:function (){
this.age++;
}
}
})
</script>
</body>
</html>
v-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-if</title>
</head>
<body>
<div id="app">
<input type="button" value="切换显示" @click="toggleIsShow">
<p v-if="isShow">西兰花</p>
<p v-show="isShow">西兰花-v-show</p>
<h2 v-if="temperature>=35">热死了</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false,
temperature:45
},
methods:{
toggleIsShow:function (){
this.isShow = !this.isShow;
}
}
})
</script>
</body>
</html>
v-bind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind</title>
<style>
.active{
border: 1px solid red;
}
</style>
</head>
<body>
<div id="app">
<img v-bind:src="imgSrc" alt="">
<br>
<img :src="imgSrc" alt="" :title="imgTitle+'!!!'" :class="isActive?'active':''" @click="toggleActive">
<br>
<img :src="imgSrc" alt="" :title="imgTitle" :class="{active:isActive}" @click="toggleActive">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
imgSrc:"//www.runoob.com/images/logo.png",
imgTitle:"菜鸟",
isActive:false
},
methods:{
toggleActive:function (){
this.isActive=! this.isActive;
}
}
})
</script>
</body>
</html>
有个图片切换的作业,因为没有CSS的样式 ,选择性放弃
v-for指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-for</title>
</head>
<body>
<div id="app">
<input type="button" value="添加数据" @click="add">
<input type="button" value="删除数据" @click="remove">
<ul>
<li v-for="(it,index) in arr">
{{index+1}} 黑马程序猿校区:{{it}}
</li>
</ul>
<h2 v-for="item in vegetable" v-bind:title="item.name">
{{item.name}}
</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
vegetable:[
{name:"西兰花炒蛋"},
{name:"蛋炒西兰花"}
]},
methods:{
add:function (){
this.vegetable.push({name:"兰菜炒蛋"});
},
remove:function (){
this.vegetable.shift();
}
}
})
</script>
</body>
</html>
v-on补充
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-on</title>
</head>
<body>
<div id="app">
<input type="button" value="点击" @click="doIt(666,'老铁')">
<input type="text" @keyup.enter="sayHi">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
methods:{
doIt:function (p1,p2){
console.log("做it");
console.log(p1);
console.log(p2);
},
sayHi:function (){
alert("吃了没");
}
}
})
</script>
</body>
</html>
v-model
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model</title>
</head>
<body>
<div id="app">
<input type="button" value="修改message" @click="setM">
<input type="text" v-model="message" @keyup.enter="getM">
<h2>{{message}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"黑马程序猿"
},
methods:{
getM:function (){
alert(this.message);
},
setM:function (){
this.message ="苦丁鱼";
}
}
})
</script>
</body>
</html>
axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>axios基本使用</title>
</head>
<body>
<input type="button" value="get请求" class="get">
<input type="button" value="post请求" class="post">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
document.querySelector(".get").onclick = function (){
axios.get("https://autumnfish.cn/api/joke/list?num=6")
.then(function (response){
console.log(response);
},function(err){
console.log(err);
})
}
document.querySelector(".post").onclick=function (){
axios.post("https://autumnfish.cn/api/user/reg",{username:"侯开宇"})
.then(function (response){
console.log(response);
})
}
</script>
</body>
</html>
axios+vue
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue+axios</title>
</head>
<body>
<div id="app">
<input type="button" value="获取笑话" @click="getJoke">
<p>{{joke}}</p>
</div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app=new Vue({
el:"#app",
data:{
joke:"很好笑的笑话"
},
methods:{
getJoke:function (){
var that =this;
axios.get("https://autumnfish.cn/api/joke").then(
function (response){
console.log(response.data);
that.joke=response.data;
}
)
}
}
})
</script>
</body>
</html>