1、broadcast 事件广播

  遍历寻找所有子孙组件,假如子孙组件和componentName组件名称相同的话,则触发$emit的事件方法,数据为 params.
  如果没有找到 则使用递归的方式 继续查找孙组件,直到找到为止,否则继续递归查找,直到找到最后一个都没有找到为止。 

2、dispatch 查找所有父级,直到找到要找到的父组件,并在身上触发指定的事件。
 @param { componentName } 组件名称
@param { eventName } 事件名
@param { params } 参数

vue2.0

父传子:Props
子传父:子:$emit(eventName) 父$on(eventName)
父访问子:ref
非父子组件通信:https://vuefe.cn/guide/components.html#非父子组件通信
vue2.0 移除:1.$dispatch() 2.$broadcast() 3.events

vue1.0

<template>
<div id="app">
<p>{{title}}</p>
<p v-text="title"></p>
<p v-text="title2"></p>
<p v-html="title2"></p>
</div>
</template>
<script>
export default {
data () {
return {
title: 'this is a title!',
title2: '<span>?<span> this is a title!'
}
}
}
</script>
  • {{title}}和v-text="title"等同
  • export default最后生成 new vue({ 参数})
  • 新的ES6写法等同于旧的写法

    //新的ES6
    data () {
    return {
    title: 'this is a title!'
    }
    }
    //旧的写法
    data: function (){
    return {
    title: 'this is a title!'
    }
    }
  • v-html 解析渲染html标签

v-for 及v-bind控制class、v-on绑定事件、v-model双向绑定

<template>
<div id="app">
<p>{{title}}</p>
<!-- <p v-text="title"></p> -->
<!-- <p v-text="title2"></p> -->
<!-- <p v-html="title2"></p> -->
<input v-model="newItem" v-on:keyup.enter="addNew">
<ul>
<li v-for = "item in items" v-bind:class="{finished: item.isFinished}" v-on:click="toggleFinished(item)">
{{item.label}}
</li>
</ul>
</div>
</template>
<script>
import Store from './store'
export default {
data () {
return {
title: 'this is a todolist!',
title2: '<span>?<span> this is a todolist!',
items: Store.fetch(),
newItem: ''
}
},
watch: {
items: {
handler (items) {
Store.save(items)
},
deep: true
}
},
methods: {
toggleFinished (item) {
item.isFinished = !item.isFinished
},
addNew () {
this.items.push({
label: this.newItem,
isFinished: false
})
this.newItem = ''
}
}
}
</script> <style>
.finished{
text-decoration: underline;
} html {
height: 100%;
} body {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
} #app {
color: #2c3e50;
margin-top: -100px;
max-width: 600px;
font-family: Source Sans Pro, Helvetica, sans-serif;
text-align: center;
} #app a {
color: #42b983;
text-decoration: none;
} .logo {
width: 100px;
height: 100px
}
</style>

store.js

const STORAGE_KEY = 'todos-vuejs'
export default {
fetch () {
return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
},
save (items) {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items))
}
}
  • v-bind:class简写:class
  • v-on:click简写@click
  • v-on:keyup.enter简写@keyup.enter 回车keyup事件
  • v-model 双向绑定

JSON.parse()和JSON.stringify()

  • parse用于从一个字符串中解析出json 对象。例如
    var str='{"name":"cpf","age":"23"}'

经 JSON.parse(str) 得到:

Object: age:"23"
name:"cpf"
_proto_:Object

ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常

  • stringify用于从一个对象解析出字符串,例如

var a={a:1,b:2}

经 JSON.stringify(a)得到:

“{“a”:1,"b":2}”

自定义事件

  • 使用 $on() 监听事件;

  • 使用 $emit()在它上面触发事件;

  • 使用 $dispatch()派发事件,事件沿着父链冒泡;

  • 使用 $broadcast()广播事件,事件向下传导给所有的后代。

父组件向子组件传递

1、采用props

父组件

<component-a msgfromfather='you die!!!!'></component-a>

子组件

<template>
<div class="hello">
<h1>{{ msgfromfather }}</h1>
<button v-on:click="onClickMe">click!</button>
</div>
</template> <script>
export default {
data () {
return {
}
},
props: ['msgfromfather'],
methods: {
onClickMe () {
console.log(this.msgfromfather)
}
}
}
</script>
<style scoped>
h1 {
color: #42b983;
}
</style>
  • props监听父组件传递过来的信息
  • 传递过来后,可直接引用,就如已经传递过来数据塞到data
2、使用event,$broadcast()从父组件传递消息下去

父组件

<template>
<button v-on:click="talkToMyBoy('be a good boy')">talkToMyBoy</button>
</div>
</template>
<script>
import Store from './store'
import ComponentA from './components/componentA'
export default {
components: {
ComponentA
},
methods: {
talkToMyBoy (msg) {
//console.log(msg);
this.$broadcast('onAddnew',msg)
}
}
}
</script>

子组件

<template>
<div class="hello">
<h1>{{ listentomyfather }}</h1>
</div>
</template>
<script>
export default {
data () {
return {
listentomyfather: 'Hello from componentA!'
}
},
events: {
'onAddnew' (msg) {
//console.log(msg)
this.listentomyfather = msg
}
}
}
</script>

子组件向父组件传递

1.子组件$emit()触发,父组件$on()监听
<template>
<div class="hello">
<button v-on:click="onClickMe">telltofather</button>
</div>
</template> <script>
export default {
methods: {
onClickMe () {
this.$emit('child-tell-me-something',this.msg)
}
}
}
</script>

父组件

<template>
<div id="app">
<p>child tell me: {{childWords}}</p>
<component-a v-on:child-tell-me-something='listenToMyBoy'></component-a>
</div>
</template> <script>
import Store from './store'
import ComponentA from './components/componentA'
export default {
components: {
ComponentA
},
data () {
return {
childWords: ''
}
},
methods: {
listenToMyBoy (msg) {
this.childWords = msg
}
}
}
</script>
2.不使用v-on,使用event ,子组件$dispatch(),从子组件传递消息上去

子组件

<template>
<div class="hello">
<button v-on:click="onClickMe">talktomyfather</button>
</div>
</template> <script>
export default {
methods: {
onClickMe () {
this.$dispatch('child-tell-me-something',this.msg)
}
}
}
</script>

父组件

<template>
<div id="app">
<p>child tell me: {{childWords}}</p>
<component-a></component-a>
</div>
</template> <script>
import Store from './store'
import ComponentA from './components/componentA'
export default {
components: {
ComponentA
},
data () {
return {
childWords: ''
}
},
events: {
'child-tell-me-something' (msg) {
this.childWords = msg
}
}
}
</script>

作者:俊瑶先森
链接:http://www.jianshu.com/p/240125faeb79
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

dispatch emit broadcast的更多相关文章

  1. vue之自行实现派发与广播-dispatch与broadcast

    要解决的问题 主要针对组件之间的跨级通信 为什么要自己实现dispatch与broadcast? 因为在做独立组件开发或库时,最好是不依赖第三方库 为什么不使用provide与inject? 因为它的 ...

  2. 如何实现Vue已经弃用的$dispatch和$broadcast方法?

    对于父子(含跨级)传递数据的通信方式,Vue.js 并没有提供原生的 API 来支持,而是推荐使用大型数据状态管理工具 Vuex,但 Vuex 对于小型项目来说用起来真的很麻烦. 在 Vue.js 1 ...

  3. AngularJS里面$emit, $broadcast,$on,$http.Jsonp,constant是使用笔记

    本片主要介绍$emit, $broadcast,$on经常开发的用法!

  4. Angular中Controller之间的信息传递(第二种办法):$emit,$broadcast,$on

    $emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...

  5. AngularJS 事件广播与接收 $emit $broadcast $on

    AngularJS中的作用域scope有一个非常有层次和嵌套分明的结构. 其中它们都有一个主要的$rootScope(也就说对应的Angular应用或者ng-app),然后其他所有的作用域部分都是继承 ...

  6. $scope绑定事件之$on方法和$emit,$broadcast

    function DemoCtrl($scope){ $scope.count = 0; $scope.$on('myevent',function(){ $scope.count++; }) } 视 ...

  7. 父子间通信四 ($dispatch 和 $broadcast用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 理解angularJs中的$on,$broadcast,$emit

    $emit作用是将一个事件从子作用域广播至父作用域,直至根作用域.(包括自己) $emit有两个参数name和args,name就是需要广播的名字,args是一个或者多个参数. $broadcast的 ...

  9. angular之$broadcast、$emit、$on传值

    文件层级 index.html <!DOCTYPE html> <html ng-app="nickApp"> <head> <meta ...

随机推荐

  1. 手把手教小白如何用css+js实现页面中图片放大展示效果

    1.前言      很多童鞋会在项目中遇到一些上传图片,展示图片的操作,但是图片呢有大有小,为了页面的美观,有时候我们需要将图片展示成固定宽高度,但是呢,领导就会说,我想看大图片,怎么办?想看就看呀, ...

  2. opencc 繁体简体互转 (C++示例)

         繁体字通常采用BIG5编码,简体字通常采用GBK或者GB18030编码,这种情况下,直接使用iconv(linux下有对应的命令,也有对应的C API供编程调用)就行.对于默认采用utf-8 ...

  3. 抵制克苏恩[Lydsy2017年4月月赛]

    题目描述 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 30 点血量的英 ...

  4. 《撸轮子系列》之LoadPE

    前言 我新书<Python爬虫开发与项目实战>出版了. 这本书包括基础篇,中级篇和深入篇三个部分,不仅适合零基础的朋友入门,也适合有一定基础的爬虫爱好者进阶,如果你不会分布式爬虫,不会千万 ...

  5. Eclipse的操作技巧

    网上已经有很多这类文章了,而且写的很全. 我最近刚开始使用Eclipse,写这篇文章的目的是在熟悉的过程中慢慢添加我用着顺手的一些技巧,以免因为某些原因更换开发环境后会忘记. 一.设置:窗口-首选项- ...

  6. iOS 折线图实现

    图表绘制的过程实际上是坐标位置的计算过程,至于画线只要有了position,通过CAShapeLayer+BezierPath很快就可以画出来,这里提供一个绘制折线的demo,贵在思路,有需要的可以参 ...

  7. DataGuard开启延时应用的测试

    DataGuard开启延时应用的测试 实验环境:RHEL 6.5 + Oracle 11.2.0.4 GI.DB + Primary RAC(2 nodes)+ Standby RAC(2 nodes ...

  8. 前端到后台ThinkPHP开发整站(4)

    今晚继续我的这个项目的开发,今晚也是写的不多,主要写了一个菜单管理功能的CURD方法,前端界面还没有进行编写. 菜单管理Model层的代码: <?php namespace Common\Mod ...

  9. URL的概念

      URL(Uniform Resource Locator)直接翻译为"统一资源定位符" URL的作用:描述一个在网络上的资源   schema://host[:port#]/path/.../[? ...

  10. Run Performance Testing Which Was Distributed To Multiple Test Agents

    How to solve the VS installed machine cannot run performance testing by .testsettings file, which wi ...