1. vue简介
Vue (发音为 /vjuː/,类似 view) 是一款用于构建用户界面的 JavaScript 框架。它基于标准 HTML、CSS 和 JavaScript 构建,并提供了一套声明式的、组件化的编程模型,帮助你高效地开发用户界面。无论是简单还是复杂的界面,Vue 都可以胜任。
2. 在webstorm中创建工程
(1)引入vue.js文件
<script type="text/javascript" src="js/vue.js"></script>
(2)body创建一个div标签
<body>
<div id="app">
{{msg}}
</div>
</body>
(3)创建自己的js代码
<script type="text/javascript">
//创建一个vue对象
var app=new Vue({
//vue对象挂载到id为app的标签上,那么该标签下的所有元素都可以使用vue对象中的成员
el: "#app",
//数据
data:{
msg:"lht今天学习vue",
}
})
</script>
3.1 el属性:
把当前vue对象挂载到指定的标签元素上,使其vue生效。
4. vue指令
4.1 v-text 和v-html
设置标签的文本值(textContent)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<span>{{msg}}</span><hr>
<span v-text="msg">你喜欢什么?</span><hr>
<!--v-text:无法解析html标签 v-html可以解析html标签-->
<span v-html="msg">你喜欢什么?</span>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
msg:"<font color='red'>我喜欢打麻将</font>",
}
})
</script>
</html>
4.2 v-on基础
为元素绑定事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<span>{{msg}}</span><hr>
<button v-on:click="fun">点击</button>
<!--上面的v-on:缩写为@-->
<button @dblclick="fun">点击2</button>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
msg:"<font color='red'>我喜欢打麻将</font>",
},
//定义方法
methods:{
fun(){
//this表示vue对象
this.msg="我喜欢打篮球";
},
fun2(){
}
}
})
</script>
</html>
4.3 v-show 和v-if
根据表达值的真假,切换元素的显示和隐藏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<img src="imgs/sg.jpg" width="200" v-show="age>18&&age<36"/>
<hr>
<!--v-show通过style中disable来控制标签的显示和隐藏 v-if:通过删除和创建标签来控制-->
<img src="imgs/1.jpg" width="200" v-if="age>18&&age<36"/>
<button @click="fun">点击</button>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
age:18
},
methods:{
fun(){
this.age++;
}
}
})
</script>
</html>
4.4 v-bind
设置元素的属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
<style>
.a{
border: red solid 5px;
}
</style>
</head>
<body>
<div id="app">
<!--如何让img标签中src属性引用vue对象中的数据-->
<img v-bind:src="imgUrl" width="200" v-bind:title="title" v-bind:class="flag?'a':''"/><br>
<!--v-bind: 缩写为: -->
<img :src="imgUrl" width="200" :title="title" :class="flag?'a':''"/><br>
<button @click="fun">点击</button>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
age:18,
imgUrl:"imgs/sg.jpg",
title:"帅哥",
flag:true
},
methods:{
fun(){
this.imgUrl="imgs/1.jpg";
this.title="美女好看"
}
}
})
</script>
</html>
4.5 v-for
循环数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<!--for(数据类型 b:数组){}-->
<li v-for="(item,index) in hobby">
{{item}}--->{{index}}
</li>
</ul>
<table width="500px" border="1" cellspacing="0" class="0">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr v-for="item in users">
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>{{item.sex}}</td>
<td>
<button @click="deleteUser(item.name)">删除</button>
<button >编辑</button>
</td>
</tr>
</table>
<input type="text" @keyup.enter="fun()"/>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
hobby:["好吃","好喝","好玩","好乐"],
users:[
{"name":"gyn","age":16,"sex":"女"},
{"name":"lht","age":19,"sex":"男"},
{"name":"zyx","age":15,"sex":"男"}
]
},
methods:{
deleteUser(name){
alert(name);
},
fun(){
alert("触发了回车键");
}
}
})
</script>
</html>
4.6 v-model
获取和设置表单元素的值. input select textarea
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<!--v-model会实现双向绑定:表达元素内容改变时,vue中对于的数据也会改变,vue中的数据改变对于的表单元素也会改变-->
<input type="text" v-model="name" />
{{name}}
<button @click="dj">点击</button>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
name:"gyn"
},
methods:{
dj(){
this.name="lht";
}
}
})
</script>
</html>
回顾:
v-text: -----v-html
v-on:-----@
v-show:-----v-if
v-bind: 标签属性绑定----:
v-for: 循环
v-model:表单元素的双向绑定
5. vue结合axios以及后台代码
<!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>天知道</title>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/tianindex.css" />
</head>
<body>
<div class="wrap" id="app">
<div class="search_form">
<div class="logo"><img src="img/logo.png" alt="logo" /></div>
<div class="form_group">
<input
type="text"
class="input_txt"
placeholder="请输入查询的天气"
v-model="city"
/>
<button class="input_sub" @click="searchWeather">
搜 索
</button>
</div>
</div>
<table width="300" border="1">
<tr>
<td>姓名</td>
<td>薪水</td>
</tr>
<tr v-for="item in emps">
<td>{{item.ename}}</td>
<td>{{item.sal}}</td>
</tr>
</table>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../js/vue.js"></script>
<!-- 官网提供的 axios 在线地址 -->
<script src="../js/axios.min.js"></script>
<!-- 自己的js -->
<script>
let app=new Vue({
el:"#app",
data:{
city:"",
empVo:{},
emps:[],
},
methods:{
searchWeather(){
axios.post("http://localhost:8080/emp/list/1/5",this.empVo).then(result=>{
this.emps=result.data.data.records;
})
}
}
})
</script>
</body>
</html>
1.计数器
<!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>计数器</title>
<link rel="stylesheet" href="./css/index.css" />
</head>
<body>
<!-- html结构 -->
<div id="app">
<!-- 计数器功能区域 -->
<div class="input-num">
<button @click="deleteNum">
-
</button>
<span>{{num}}</span>
<button @click="addNum">
+
</button>
</div>
<img src="http://qnaaa.zzaaa.net/aaajy/logo.png" alt="" />
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../js/vue.js"></script>
<!-- 编码 -->
</body>
<script>
let app = new Vue({
el:"#app",
data:{
num:0
},
methods:{
addNum(){
this.num++
},
deleteNum(){
this.num--
}
}
})
</script>
</html>
2.图片切换
<!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>图片切换</title>
<link rel="stylesheet" href="./css/Index.css" />
</head>
<body>
<div id="mask">
<div class="center">
<h2 class="title">
<img src="images/logo.png" alt="">
深圳创维校区环境
</h2>
<!-- 图片 -->
<img :src="images[index]" alt="" />
<!-- 左箭头 -->
<a href="javascript:void(0)" class="left" @click="left"v-show="index!=0">
<img src="images/prev.png" alt="" />
</a>
<!-- 右箭头 -->
<a href="javascript:void(0)" class="right" @click="right" v-show="index!=images.length-1">
<img src="images/next.png" alt=""/>
</a>
</div>
</div>
<script src="../js/vue.js"></script>
</body>
<script>
let app=new Vue({
el:"#mask",
data:{
images:[
"./images/01.jpg",
"./images/02.jpg",
"./images/03.jpg",
"./images/04.jpg",
"./images/05.jpg",
"./images/06.jpg",
"./images/07.jpg",
"./images/08.jpg",
"./images/09.jpg",
"./images/10.jpg",
],
index:0,
},
methods:{
right(){
this.index++
this.index=this.index%this.images.length;
},
left(){
this.index--
this.index=this.index%this.images.length;
}
}
})
</script>
</html>
3.记事本
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>小黑记事本</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="googlebot" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="./css/Bookindex.css" />
</head>
<body>
<!-- 主体区域 -->
<section id="todoapp">
<!-- 输入框 -->
<header class="header">
<h1>小黑记事本</h1>
<input
autofocus="autofocus"
autocomplete="off"
placeholder="请输入任务"
class="new-todo"
v-model="content"
@keyup.enter="addMsg"
/>
</header>
<!-- 列表区域 -->
<section class="main">
<ul class="todo-list">
<li class="todo" v-for="(item,index) in msg">
<div class="view">
<span class="index">{{index+1}}.</span> <label>{{item}}</label>
<button class="destroy" @click="del(index)"></button>
</div>
</li>
</ul>
</section>
<!-- 统计和清空 -->
<footer class="footer">
<span class="todo-count"> <strong>{{msg.length}}</strong> items left </span>
<button class="clear-completed" @click="cls">
Clear
</button>
</footer>
</section>
<!-- 底部 -->
<footer class="info">
<p>
<a href="http://www.baidu.com"
><img src="./img/logo.png" alt=""
/></a>
</p>
</footer>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../js/vue.js"></script>
</body>
<script>
let app = new Vue({
el:"#todoapp",
data:{
msg:["吃饭饭","看美女"],
content:"",
},
methods:{
addMsg(){
this.msg.push(this.content)
},
del(index){
this.msg.splice(index,1);
},
cls(){
this.msg=[];
}
}
})
</script>
</html>
4.查询所有学生信息---并展示到表格中
<!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>天知道</title>
</head>
<body>
<div class="wrap" id="app">
<div class="search_form">
<div class="form_group">
<input
type="text"
class="input_txt"
placeholder="请输入查询的天气"
v-model="cid"
/>
<button class="input_sub" @click="searchWeather">
搜 索
</button>
</div>
</div>
<table width="300" border="1">
<tr>
<td>id</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr v-for="item in students">
<td>{{item.sid}}</td>
<td>{{item.sname}}</td>
<td>{{item.age}}</td>
</tr>
</table>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../js/vue.js"></script>
<!-- 官网提供的 axios 在线地址 -->
<script src="../js/axios.min.js"></script>
<!-- 自己的js -->
<script>
let app=new Vue({
el:"#app",
data:{
cid:"",
studentVo:{},
students:[],
},
methods:{
searchWeather(){
axios.post("http://localhost:8080/student/list/1/5",this.studentVo).then(result=>{
this.students=result.data.data.records;
})
}
}
})
</script>
</body>
</html>
标签:el,vue,app,Vue,images,data
From: https://www.cnblogs.com/gl0806/p/17335677.html