vue.js中生命周期与相关函数的使用总结

来源:互联网 发布:古龙的作品风格知乎 编辑:程序博客网 时间:2024/05/22 08:06

先上一张

Vue的生命周期图谱


**Vue生命周期

再上一段

主要代码

(代码为每个vue组件都会用到的模式一般放在页面的js代码块中):

export default {    components:{        Child,Child2    },    //变量初始化,数据类型定义,存放数据的属性    data () {        return {            firstName: 'Foo',            lastName: 'Bar',            fullName: 'Foo Bar',            routeId:null        }    },    //组数据和字符处理的计算属性    computed: {        //字符串首末顺序倒置        reversedMessage: function () {            return this.message.split('').reverse().join('')        }        fullName: {            // getter            get: function () {                  return this.firstName + ' ' + this.lastName            },            // setter            set: function (newValue) {                  var names = newValue.split(' ')                  this.firstName = names[0]                  this.lastName = names[names.length - 1]            }        }    }    //方法属性    methods:{         reversedMessage: function () {            return this.message.split('').reverse().join('')         },         // _.debounce 是一个通过 lodash 限制操作频率的函数。        // 在这个例子中,我们希望限制访问yesno.wtf/api的频率        // ajax请求直到用户输入完毕才会发出(异步执行)        getAnswer: _.debounce(            function () {                if (this.question.indexOf('?') === -1) {                    this.answer = 'Questions usually contain a question mark. ;-)'                    return                }                this.answer = 'Thinking...'                var vm = this                axios.get('https://yesno.wtf/api')                .then(function (response) {                    vm.answer = _.capitalize(response.data.answer)                })                .catch(function (error) {                vm.answer = 'Error! Could not reach the API. ' + error                })          },          // 这是我们为用户停止输入等待的毫秒数          500       ),         updateMessage: function () {              this.message = '更新完成'              console.log(this.$el.textContent) // => '没有更新'              this.$nextTick(function () {                  console.log(this.$el.textContent) // => '更新完成'              })    }    },    // 组件创建完后直接响应,组件复用情况下,页面的第二次操作就不会再次调用该函数    created () {        console.log("created")    },     //组件复用情况下,页面的第二次操作就不会再次调用该函数    mounted(){    },    //在数据变化时响应    watch: {        firstName: function (val) {          this.fullName = val + ' ' + this.lastName        },        lastName: function (val) {          this.fullName = this.firstName + ' ' + val        },        // 如果 question 发生改变,这个函数就会运行       question: function (newQuestion) {           this.answer = 'Waiting for you to stop typing...'           this.getAnswer()       },        '$route'(to,from){            this.routeId = "内容切换:"+this.$route.params.id;            //分别打印路由变化前后的值(路由是个对象)            console.log(from)            console.log(to)    }    }}

书写的时候大家要

注意写法的不同

,例如:methods与created的区别,methods相当于是一个函数大容器,里面放的是各种函数,它本身不是一个运行函数,所以是methods:{} 类似对象赋值;而created是组件加载之后自动执行的一个函数体,所以他的写法是created:function(){};
vue官方文档也对其类型有说明,
这里写图片描述这里写图片描述
类似的理解方式把常用的函数体和对象容器总结一下给大家:
函数体:data(),beforeCreate(),created(),beforeMount(),mounted(),updated()…
对象容器:components,computed,methods,watch,…

总结生命周期执行顺序

,按照执行顺序排列如下:
**beforeCreate
methods
created
beforeMount
mounted
beforeUpdate
updated
activated
deactivated
beforeDestroy
destroyed**
created中可以调用methods中的function,但是在beforeCreate中就会报错,因为此时methods还未加载,其中的function当然也还未载入!

新手学习,希望大家多多指正!多多交流

原创粉丝点击