vue生命周期和vue-resource
生命周期:
定义:
从Vue实例创建、运行、到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期!
vue生命周期详解:
<!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>Document</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<input type="button" value="修改msg" @click="msg='No'">
<h3 id="h3">{{ msg }}</h3>
</div>
<script>
// 创建 Vue 实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
msg: 'ok'
},
methods: {
show() {
console.log('执行了show方法')
}
},
beforeCreate() { // 这是我们遇到的第一个生命周期函数,表示实例完全被创建出来之前,会执行它
// console.log(this.msg)
// this.show()
// 注意: 在 beforeCreate 生命周期函数执行的时候,data 和 methods 中的 数据都还没有没初始化
},
created() { // 这是遇到的第二个生命周期函数
// console.log(this.msg)
// this.show()
// 在 created 中,data 和 methods 都已经被初始化好了!
// 如果要调用 methods 中的方法,或者操作 data 中的数据,最早,只能在 created 中操作
},
beforeMount() { // 这是遇到的第3个生命周期函数,表示 模板已经在内存中编辑完成了,但是尚未把 模板渲染到 页面中
// console.log(document.getElementById('h3').innerText)
// 在 beforeMount 执行的时候,页面中的元素,还没有被真正替换过来,只是之前写的一些模板字符串
},
mounted() { // 这是遇到的第4个生命周期函数,表示,内存中的模板,已经真实的挂载到了页面中,用户已经可以看到渲染好的页面了
// console.log(document.getElementById('h3').innerText)
// 注意: mounted 是 实例创建期间的最后一个生命周期函数,当执行完 mounted 就表示,实例已经被完全创建好了,此时,如果没有其它操作的话,这个实例,就静静的 躺在我们的内存中,一动不动
},
// 接下来的是运行中的两个事件
beforeUpdate() { // 这时候,表示 我们的界面还没有被更新【数据被更新了吗? 数据肯定被更新了】
/* console.log('界面上元素的内容:' + document.getElementById('h3').innerText)
console.log('data 中的 msg 数据是:' + this.msg) */
// 得出结论: 当执行 beforeUpdate 的时候,页面中的显示的数据,还是旧的,此时 data 数据是最新的,页面尚未和 最新的数据保持同步
},
updated() {
console.log('界面上元素的内容:' + document.getElementById('h3').innerText)
console.log('data 中的 msg 数据是:' + this.msg)
// updated 事件执行的时候,页面和 data 数据已经保持同步了,都是最新的
}
});
</script>
</body>
</html>
vue-resource的使用
引入vue-resource的脚本的文件
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
注意:引用的先后顺序是 - 先引用Vue
的脚本文件,再引用vue-resource
的脚本文件
get请求
getInfo() { // get 方式获取数据
this.$http.get('http://yapi.shangyuninfo.com/mock/36/web02/category').then(res => {
console.log(res.body);
})
}
post请求
postInfo() {
var url = ' http://yapi.shangyuninfo.com/mock/36/web02/list/goods/category';
// post 方法接收三个参数:
// 参数1: 要请求的URL地址
// 参数2: 要发送的数据对象
// 参数3: 指定post提交的编码类型为 application/x-www-form-urlencoded
this.$http.post(url, {categoryId: 'zs' }, { emulateJSON: true }).then(res => {
console.log(res.body);
});
}
JSON-》content-type : application/json
表单=》application/x-www-form-urlencoded
文件上传=> multipart/form-data
axios的使用
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
或
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
get请求
<body>
<div id='app'>
<ul>
<li v-for="(item,index) in list">{{item.createTime}}
<img :src="item.coverImgUrl" alt="">
</li>
</ul>
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
list: []
},
created() {
axios.get("https://showme.myhope365.com/api/shop/shopGoods/open/banner/list", {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
}).then(res => {
console.log(res);
this.list = res.data.rows;
})
}
})
</script>
</body>
post请求
<body>
<div id='app'>
<ul>
<li v-for="(item,index) in list">{{item.createTime}}
<img :src="item.coverImgUrl" alt="">
</li>
</ul>
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
list: []
},
created() {
const parmas = new URLSearchParams();
parmas.append("pageNum",1);
parmas.append("pageSize",5);
axios.post("https://showme.myhope365.com/api/shop/shopCategory/open/list", parmas
).then(res => {
console.log(res);
this.list = res.data.rows;
})
}
})
</script>
上传文件
<input type="file" @change="changefile"> e.target.files[0]