<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{ message }}</p>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message : "xuxiao is boy"
},
beforeCreate: function () {
console.group('beforeCreate 创建前状态===============》');
console.log("%c%s", "color:red" , "el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //undefined
console.log("%c%s", "color:red","message: " + this.message)
},
created: function () {
console.group('created 创建完毕状态===============》');
console.log("%c%s", "color:red","el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
console.log("%c%s", "color:red","message: " + this.message); //已被初始化
},
beforeMount: function () {
console.group('beforeMount 挂载前状态===============》');
console.log("%c%s", "color:red","el : " + (this.$el)); //已被初始化
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
console.log("%c%s", "color:red","message: " + this.message); //已被初始化
},
mounted: function () {
console.group('mounted 挂载结束状态===============》');
console.log("%c%s", "color:red","el : " + this.$el); //已被初始化
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
console.log("%c%s", "color:red","message: " + this.message); //已被初始化
},
beforeUpdate: function () {
console.group('beforeUpdate 更新前状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
updated: function () {
console.group('updated 更新完成状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
beforeDestroy: function () {
console.group('beforeDestroy 销毁前状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
destroyed: function () {
console.group('destroyed 销毁完成状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message)
}
})
</script>
</body>
</html>
当我们在F12里面修改app.message数据的时候,上述代码执行以下结果。
<home-swiper :banners="banners" @swiperImageLoad="swiperImage"></home-swiper>
props: {
banners: {
type: Array,
required: true
}
},
<swiper-item v-for="(item, index) in banners" :key="index">
props 数据验证
// 1.通过数组传值
props:[‘cmessage’,‘cmovies’],
//2.类型限制传值
props:{
//类型限制
cmovies:Array,
cmessage:String
},
//3.类型限制+ 提供默认值 + required : 必须传入的值
//如果类型是对象或者数组的时候,默认值必须是一个函数
props:{
cmovies:{
type:Array,
default(){
return ['宝鸡市','西安市','玉玲市','四川市']
}
},
cmessage:{
type:String,
default:'北京欢迎你'
}
},
props 中的驼峰标识问题
v-bind 绑定的时候 不支持驼峰标识 如果是 cMovies 需要写成 c-movies 这样才可以达到我们想要的效果.
<!-- 子组件定义事件change(item)并且将要传递的数据当作参数传递过去-->
<button v-for='item in shopList' @click='change(item)'>{{ item.name }}</button>
<!--子组件发射自定义的函数,并且传递参数item-->
change(item){
this.$emit('itemclick',item)
}
<!-- 父组件监听子组件发送的事件-->
<cpn @itemclick='shopclick'></cpn>
<!-- 父组件定义函数拿到数据-->
shopclick(item){
console.log(item.id + item.name)
}
下一篇:Vue重点学习经验2
文章参考1:https://segmentfault.com/a/1190000016344599?utm_source=tag-newest
作者:沉静的闪光
文章参考2:https://segmentfault.com/a/1190000008010666
作者:城南