首页 > 其他 > 详细

Vue-应用2

时间:2020-06-08 10:34:25      阅读:33      评论:0      收藏:0      [点我收藏+]


//计算属性:本质上是一个函数 实现某种功能或者得到某个结果;使用的时候当作数据属性一样来用


<!--

            更改数据后:计算、函数均会执行

         -->

        {{reverseStr}} {{reverseStr1()}} {{reverseStr}} {{reverseStr1()}}

<input type="text" v-model=‘firstName‘ id="">

<input type="text" v-model=‘lastName‘ id="">{{result}}

<input type="text" v-model.lazy=‘userName‘ id="">{{result}}

<div>{{comKey}}</div>

==============================================================

数据部分:

data() {

return {

msg: ‘hello‘,

firstName: ‘‘,

lastName: ‘‘,

result: ‘‘,

userName: ‘‘,

obj: {

a: 10,

b: 20

                    }

                }

            },

// 方法区域

methods: {

reverseStr1: function() {

console.log(‘methods‘);

return this.msg.split(‘‘).reverse().join(‘‘).toString();

                },

//检测用户名合法性

checkName: function(userName) {

setTimeout(() => {

if (userName === ‘admin‘) {

this.result = ‘right‘;

                        } else {

this.result = ‘wrong‘;

                        }

                    }, 2000);

                },

//计算属性:本质上是一个函数 实现某种功能或者得到某个结果;使用的时候当作数据属性一样来用

            },

//计算区域

computed: {

reverseStr: function() {

console.log(‘computed‘);

return this.msg.split(‘‘).reverse().join(‘‘).toString();

                },

comKey: function() {

return this.obj.a;

                }

            },

//侦听区域

watch: {

//侦听数据变化

firstName: function(newStr, oldStr) {

// console.log(newStr, oldStr);

this.result = this.firstName + "-" + this.lastName;

this.msg = this.firstName + "-" + this.lastName;

                },

lastName: function(newStr, oldStr) {

this.result = this.firstName + "-" + this.lastName;

this.msg = this.firstName + "-" + this.lastName;

                },

//第一个参数即对应最新的值:区别两个参数API的使用

userName: function(val) {

this.checkName(val);

this.result = ‘checking...‘;

                },

//浅响应

// obj: function() {

//     console.log(this.obj.a);

// }

//深响应

obj: {

handler: function() {

console.log(this.obj.a);

                    },

deep: true,

                }

            }

过滤器部分

{{info | upper | lower}} {{num | getSum(20,‘求和‘,‘秋茶‘)}}

=================================

//全局过滤器

Vue.filter(‘upper‘, function(val) {

return val.toUpperCase();

        });

Vue.filter(‘lower‘, function(val) {

return val.toLowerCase();

        });

===========================

let vm = new Vue({

el: "#app",

data() {

return {

info: ‘china‘,

num: 5

                }

            },

//过滤器局部

filters: {

upper(val) {

return val.toUpperCase();

                },

lower(val) {

return val.toLowerCase();

                },

getSum(num, str, str1) {

console.log(str, str1);

let sum = 0;

for (let index = 0; index <= num; index++) {

sum += index;

                    }

return sum;

                }

            }

        });

【3】生命周期钩子:用于组件、响应事件等

<div id="app">

        {{info}}

</div>

====================

let vm = new Vue({

//方法1

el: "#app",

template: ‘<h1>hello</h1>‘,

data() {

return {

info: ‘china‘,

                }

            },

beforeCreate() {

console.log(‘beforeCreated()‘);

            },

created() {

console.log(‘created()‘);

console.log(this);

console.log(this.info);

            },

beforeMount() {

console.log(‘beforeMount()‘);

console.log(this.$el);

            },

mounted() {

console.log(‘mounted()‘);

console.log(this.$el);

            },

beforeUpdate() {

console.log(‘beforeUpdate()‘);

console.log(this.info);

            },

updated() {

console.log(‘update()‘);

console.log(this.info);

            },

beforeDestroy() {

console.log(‘beforeDestroy()‘);

            },

destroyed() {

console.log(‘destroy()‘);

            }

        });

Vue-应用2

原文:https://www.cnblogs.com/macro-renzhansheng/p/13064083.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!