vue生命周期和vue-resource

1、什么是生命周期

从Vue实例创建、运行、到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期!

2、vue生命周期钩子函数

每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会。

生命周期函数=生命周期事件=生命周期钩子

3、vue生命周期

详解:

代码段

<!DOCTYPE html>
<html>
<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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id='app'>
        {{msg}}
        <input type="text" v-model="msg">
    </div>
    <script>
        // 什么是生命周期?
        // 从Vue实例创建、运行、到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期!
        const vm = new Vue({
            el: '#app',
            data: {
                msg: "さしぶり"
            },
            methods: {
                demo() {
                    console.log(demo);
                },
            },
            // 生命周期
            beforeCreate() {
                //初始化前
                // 第一个生命周期 vue实例刚创建,但是data数据没有挂载 ,所以拿不到 data和methods
                // 一般用来重定向
                console.log("beforeCreate");
            },
            Created() {
                // 初始化完成
                // 这是第一个能拿到data和methods 的生命周期   一般用于接口请求和数据初始化
                console.log("Created");
            },
            beforeMount() {
                // 挂载前
                console.log("beforeMount");
            },
            mounted() {
                // 挂载完成
                // 一般要求依赖dom的操作要放在这里
                console.log("mountd");
            },
            // 这两个生命周期执行 0次 或 更多次
            beforeUpdate() {
                // 更新前
                console.log("beforeUpdate");
                console.log(this.msg);
            },
            updated() {
                // 更新后
                console.log("updated");
                console.log(this.msg);
            },
            beforeDestory() {
                // vue销毁之前  一般用与清除计时器,清除数据监听和事件监听
                console.log("beforeDestory");
            },
            destoryed() {
                // 销毁之后
                console.log("destoryed");
            }
        })
    </script>
</body>

</html>

4. vue-resource的使用

  • 直接在页面中,通过script标签,引入vue-resource的脚本文件;

  • 注意:引用的先后顺序是 - 先引用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);
                    });
                }

 5.axios的使用

Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求。

Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中

示例

<!DOCTYPE html>
<html>

<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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
    <div id='app'>
        <div v-for="item in list" :key=item.id>
            {{item.category}}
        </div>
    </div>

    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                list: []
            },
            methods: {},
            created() {
                // GET
                axios.get("http://yapi.shangyuninfo.com/mock/36/web02/category").then(res => {
                    this.list = res.data.data
                })
                // post 只有两个参数 1 地址 2 请求体
                // 通过内置对象,创建对应格式的请求体
                //URLSearchParams对应的请求格式application/x-www-form-urlencoded
                let url = new URLSearchParams()
                url.append("type", "free")
                url.append("pageNum", 1)
                url.append("pageSize", 5)
                let form = new FormData()
                // append两个参数 1 属性名 2 属性值 中间是逗号
                form.append("type", "free")
                form.append("pageNum", 1)
                form.append("pageSize", 5)
                axios.post("http://wkt.myhope365.com/weChat/applet/course/list/type",form).then(res=>{
                    console.log(res);
                })
                let json = {"enable":1}
                axios.post("http://wkt.myhope365.com/weChat/applet/subject/list",json).then(res=>{
                    console.log(res);
                })
            }
        })
    </script>
</body>

</html>

6.Vue中的动画

示例

<!DOCTYPE html>
<html>
<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>
    <style>
        .v-enter-to,
        .v-leave {
            transform: translateX(0);
        }
        .v-enter-active,
        .v-leave-active {
            transition: all 7s;
        }
        .v-leave-to,
        .v-enter {
            transform: translateX(200px);
        }
        /* 如要效果不同  则要加不同的name */
        .leftenter-enter {
            transform: translateX(-100px);
        }
        .leftenter-enter-to {
            transform: translateX(0);
        }
        .leftenter-enter-active {
            transition: all 2s;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id='app'>
        <button @click="flag = !flag">点一点</button>
        <transition :duration="{enter: 5000, leave: 8000 }">
            <h3 v-if="flag">私は第二六道仙人</h3>
        </transition>
        <transition name="leftenter">
            <h3 v-if="flag">今は唯一の存在</h3>
        </transition>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                flag: true,
            },
            methods: {}
        })
    </script>
</body>

</html>

使用第三方css动画库

官方文档

 使用动画钩子函数

示例

<!DOCTYPE html>
<html>

<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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        .show {
            transition: all 7s ;
        }
    </style>
</head>
<body>
    <div id='app'>
        <input type="button" value="切换动画" @click="isshow = !isshow" />
        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
            <div v-if="isshow" class="show">始めた</div>
        </transition>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                isshow: true
            },
            methods: {
                beforeEnter(el) {
                    // 动画进入之前的回调
                    el.style.transfrom = "translateX(500px)"
                },
                enter(el, done) {
                    // 动画进入完成时候的回调   检测dom存在  el.offsetWidth
                    el.offsetWidth
                    el.style.transfrom = "translateX(0px)"
                    // done()方法调用表示完成
                    done()
                },
                afterEnter(el) {
                    // 动画进入完成之后的回调
                    // this.isshow = !this.isshow;
                }
            }
        })
    </script>
</body>

</html>

 v-for的列表过渡和排序过度

示例

<!DOCTYPE html>
<html>
<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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        .v-move {
            transition: all 7s ease;
        }
        .v-leave-active {
            position: absolute;
        }
    </style>
</head>
<body>
    <div id='app'>
        <button @click="flag=!flag">点一点</button>
        <transition-group>
            <h3 v-for="item in list" :key="item" v-if="flag">
              {{item}}私は第二六道仙人
            </h3>
          </transition-group>
    </div>

    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                flag:true,
                list: ["いち", "に", "さん", "よ", "ご", "ろく", "なな", "はち", "きゅう", "じゅう"]
            },
            methods: {}
        })
    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李时一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值