1. 子组件
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.wrap {
calc(200px * 4 + 80px);
margin: 0 auto;
user-select: none;
}
.box {
200px;
height: 260px;
/*border: 1px solid black;*/
background-color: rgba(10, 200, 30, 0.5);
border-radius: 10px;
float: left;
margin: 10px;
}
.box img {
100%;
/*height: 200px;*/
border-radius: 50%;
}
.box p {
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<div class="wrap">
<tag></tag>
<tag></tag>
<tag></tag>
<tag></tag>
</div>
</div>
</body>
<script src="js/vue.js"></script>
<script>
let titleTag = {
template: `
<p>
<b>
这是一种纯二哈
</b>
</p>
`,
};
let tag = {
template: `
<div class="box">
<img src="img/001.jpg" alt="">
<title-tag />
<p @click="fn">
锤它:<b>{{ num }}下</b>
</p>
</div>
`,
// 能被复用的组件(除了根组件),数据都要做局部化处理,因为复用组件后,组件的数据是相互独立的
// data的值为绑定的方法的返回值,返回值是存放数据的字典
data () {
return {
num: 0
}
},
methods: {
fn() {
this.num ++
}
},
components: {
titleTag,
}
};
new Vue({
el: '#app',
components: {
tag,
}
});
`
class P:
num = 0
def __init__(n):
this.n = n
p1 = P()
p2 = P()
P.num = 10
p1.num
p2.num
`
</script>
</html>
2. 组件传参-父传子
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>父传子</title>
<style>
.wrap {
calc(200px * 4 + 80px);
margin: 0 auto;
user-select: none;
}
.box {
200px;
height: 260px;
/*border: 1px solid black;*/
background-color: rgba(10, 200, 30, 0.5);
border-radius: 10px;
float: left;
margin: 10px;
}
.box img {
/* 100%;*/
height: 160px;
border-radius: 50%;
margin: 0 auto;
display: block;
}
.box p {
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<div class="wrap">
<!--<div v-for="dog in dogs" class="box">-->
<!--<img :src="dog.img" alt="">-->
<!--<p>-->
<!--<b>-->
<!--{{ dog.title }}-->
<!--</b>-->
<!--</p>-->
<!--</div>-->
<tag v-for="dog in dogs" v-bind:dog="dog" :a="1" :b="2" />
</div>
</div>
</body>
<script src="js/vue.js"></script>
<script>
let dogs = [
{ title: '二哈1号', img: 'img/1.jpg', },
{ title: '二哈2号', img: 'img/2.jpg', },
{ title: '二哈3号', img: 'img/3.jpg', },
{ title: '二哈4号', img: 'img/4.jpg', },
{ title: '二哈1号', img: 'img/1.jpg', },
{ title: '二哈2号', img: 'img/2.jpg', },
{ title: '二哈3号', img: 'img/3.jpg', },
{ title: '二哈4号', img: 'img/4.jpg', },
];
let tag = {
// 在组件内部就可以通过设置的自定义属性,拿到外部选择子组件提供给属性的值
props: ['dog', 'a', 'b', 'z'],
template: `
<div class="box">
<img :src="dog.img" alt="">
<p>
<b>
{{ dog.title }}
</b>
</p>
<p @click="fn">
锤它:<b>{{ num }}下</b>
</p>
</div>
`,
data () {
return {
num: 0,
}
},
methods: {
fn() {
this.num ++
}
},
};
new Vue({
el: '#app',
data: {
dogs,
},
components: {
tag,
}
});
/** 总结
* 1.数据在父组件中产生
* 2.在父组件中渲染子组件,子组件绑定自定义属性,附上父组件中的数据
* 3.子组件自定义属性在子组件的props成员中进行声明(采用字符串发射机制)
* 4.在子组件内部,就可以用props声明的属性(直接作为变量)来使用父组件中的数据
*/
</script>
</html>
3. 组件传参-子传父
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>子传父</title>
<style>
ul {
list-style: none;
}
.d-btn {
font-size: 12px;
15px;
display: inline-block;
}
.d-btn:hover {
color: red;
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<input type="text" v-model="msg">
<button @click="send_comment">留言</button>
<ul>
<tag v-for="(v, i) in comments" :msg="v" :index="i" @f1="deleteMsg"/>
</ul>
</div>
</body>
<script src="js/vue.js"></script>
<script>
let tag = {
props: ['msg', 'index'],
template: `
<li>
<i class="d-btn" @click="fn">x</i>
<b>{{ msg }}</b>
</li>
`,
methods: {
fn () {
// 点击子集,要告诉父级删除第几条数据,因为comments在父级中
// 需要通知父级
this.$emit('f1', this.index);
}
}
};
new Vue({
el: '#app',
data: {
msg: '',
comments: localStorage.comments ? JSON.parse(localStorage.comments) : [],
},
components: {
tag,
},
methods: {
send_comment() {
if (this.msg) {
this.comments.push(this.msg);
this.msg = '';
localStorage.comments = JSON.stringify(this.comments);
}
},
deleteMsg(index) {
this.comments.splice(index, 1);
localStorage.comments = JSON.stringify(this.comments);
}
}
})
</script>
<script>
// localStorage,sessionStorage不能直接存储数组和对象,需要序列化为json
localStorage.arr = JSON.stringify([1, 2, 3]);
let res = JSON.parse(localStorage.arr);
console.log(res, res[2]);
</script>
</html>