vue.js中生命周期与相关函数的使用总结
2017-08-26 15:44
901 查看
先上一张
**
再上一段
书写的时候大家要
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当然也还未载入!
新手学习,希望大家多多指正!多多交流
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当然也还未载入!
新手学习,希望大家多多指正!多多交流
相关文章推荐
- js日期相关函数总结分享
- WebView使用总结(应用函数与JS函数互相调用)
- WebView使用总结(应用函数与JS函数互相调用)
- WebView使用总结(应用函数与JS函数互相调用)
- 超全面的vue.js使用总结
- js日期相关函数总结分享
- js中几个正则表达式相关函数使用时g标志的作用
- 第五章 STL 基本概念、相关函数使用总结
- 总结js函数相关知识点
- 控制器View的生命周期及相关函数使用
- Vue.js 使用问题总结
- js 使用方法与函数 总结第1/4页
- Vue.js知识总结——生命周期
- Vue.js -- 过滤器使用总结
- WebView使用总结(应用函数与JS函数互相调用)
- vue.js的使用总结(1)基础篇
- js中几个正则表达式相关函数使用时g标志的作用
- Vue1.0学习总结(3)———vue1.0的生命周期、vue计算属性computed的使用、vue实例(vm)上方法总结、vue结合动画使用
- js中几个正则表达式相关函数使用时g标志的作用
- 关于zTree的使用心得以及相关的JS技术总结