码迷,mamicode.com
首页 > 其他好文 > 详细

Vue.$emit 与Vue.$on的小例子

时间:2017-11-28 16:37:25      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:ble   div   项目   style   css   script   margin   hello   dem   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>

<script src=‘jquery-3.2.1.js‘></script>
<script src="vue.js"></script>
<style type="text/css">

<style>
body {
font-family: Menlo, Consolas, monospace;
color: #444;
}
.item {
cursor: pointer;
}
.bold {
font-weight: bold;
}
ul {
padding-left: 1em;
line-height: 1.5em;
list-style-type: dot;
}
.groupName{
width:300px;
height: 100px;
border: 1px solid red;
margin: 50px;
}
.red{
background: pink;
}
</style>
</head>
<body>
<!-- item template -->
<script type="text/x-template" id="item-template">
<li>
<div
:class="{bold: isFolder,red:showB}"
@click="toggle(model)"
@mouseenter=‘mouseEnter‘
@mouseleave=‘mouseLeave‘
@dblclick="changeType">
{{model.name}}
<span v-if="isFolder">[{{open ? ‘-‘ : ‘+‘}}]</span>
</div>
<ul v-show="open" v-if="isFolder">
<item
class="item"
v-for="model in model.children"
:model="model">
</item>
<li class="add" @click="addChild">+</li>
</ul>
</li>
</script>

<ul id="demo">
<item
class="item"
:model="treeData">
</item>
<div class="groupName">
<span>项目名称</span>
<span>{{currentGroup.name}}</span>
</div>
</ul>


<script type="text/javascript">
// demo data
var data = {
name: ‘My Tree‘,
children: [
{ name: ‘hello‘ },
{ name: ‘wat‘ },
{
name: ‘child folder‘,
children: [
{
name: ‘child folder‘,
children: [
{ name: ‘hello‘ },
{ name: ‘wat‘ }
]
},
{ name: ‘hello‘ },
{ name: ‘wat‘ },
{
name: ‘child folder‘,
children: [
{ name: ‘hello‘ },
{ name: ‘wat‘ }
]
}
]
}
]
}

// define the item component
Vue.component(‘item‘, {
template: ‘#item-template‘,
props: {
model: Object
},
data: function () {
return {
open: false,
showB:false
}
},
computed: {
isFolder: function () {
return this.model.children &&
this.model.children.length
}
},
methods: {
toggle: function (group) {
demo.$emit("changegroup",group)
if (this.isFolder) {
this.open = !this.open
}
},
mouseEnter:function(){
this.showB=true
},
mouseLeave:function(){
this.showB=false
},
changeType: function () {
if (!this.isFolder) {
Vue.set(this.model, ‘children‘, [])
this.addChild()
this.open = true
}
},
addChild: function () {
this.model.children.push({
name: ‘new stuff‘
})
}
}
})

// boot up the demo
var demo = new Vue({
el: ‘#demo‘,
data: {
treeData: data,
currentGroup:{},
},
created(){
this.$on(‘changegroup‘,(group)=>{
this.currentGroup = group
})
}
})
</script>
</body>
</html>

Vue.$emit 与Vue.$on的小例子

标签:ble   div   项目   style   css   script   margin   hello   dem   

原文地址:http://www.cnblogs.com/MR-cui/p/7910406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!