加载中...

1.2.8 组件之间通信


子-父 通信

子组件可以使用this.$dispatch([String type], [Object detail]) 方法传递消息给父组件。
第一个参数定义消息类型,第二个参数为消息对象。如果父组件中的任何子组件使用$on([String type], [Function callback])注册监听事件,则回调执行第一个参数,参数中的 detail属性是消息数据。

案例:

  1. <we-element name="foo">
  2. <template>
  3. <div>
  4. <image src="{{imageUrl}}" onclick="test"></image>
  5. <text>{{title}}</text>
  6. </div>
  7. </template>
  8. <script>
  9. module.exports = {
  10. data: {
  11. title: '',
  12. imageUrl: ''
  13. },
  14. methods: {
  15. test: function () {
  16. this.$dispatch('notify', {a: 1})
  17. }
  18. }
  19. }
  20. </script>
  21. </we-element>
  22.  
  23. <template>
  24. <foo title="..." image-url="..."></foo>
  25. </template>
  26.  
  27. <script>
  28. module.exports = {
  29. created: function () {
  30. this.$on('notify', function(e) {
  31. // when <foo> image tag be clicked ,the function will be executing.
  32. // e.detail is `{a: 1}`
  33. })
  34. }
  35. }
  36. </script>

父 - 子 通信

父组件可以使用 this.$([String id]) 来获取子组件的上下文。你可以使用上下文对象访问子组件的信息。

  1. <we-element name="foo">
  2. <template>
  3. <div>
  4. <image src="{{imageUrl}}"></image>
  5. <text>{{title}}</text>
  6. </div>
  7. </template>
  8. <script>
  9. module.exports = {
  10. data: {
  11. title: '',
  12. imageUrl: ''
  13. },
  14. methods: {
  15. setTitle: function (t) {
  16. this.title = t
  17. }
  18. }
  19. }
  20. </script>
  21. </we-element>
  22.  
  23. <template>
  24. <div>
  25. <text onclick="test">click to update foo</text>
  26. <foo id="fooEl" title="..." image-url="..."></foo>
  27. </div>
  28. </template>
  29.  
  30. <script>
  31. module.exports = {
  32. methods: {
  33. test: function (e) {
  34. var foo = this.$('fooEl')
  35. foo.setTitle('...')
  36. foo.imageUrl = '...'
  37. }
  38. }
  39. }
  40. </script>

父 - 子(多子)通信

父组件可以使用this.$broadcast([String type], [Object detail]) 广播消息给所有子组件。

案例:

  1. <we-element name="bar">
  2. <template>
  3. <div>
  4. <image src="{{imageUrl}}"></image>
  5. </div>
  6. </template>
  7. <script>
  8. module.exports = {
  9. data: {
  10. imageUrl: ''
  11. },
  12. created: function() {
  13. var self = this
  14. this.$on('changeImage', function(e) {
  15. self.imageUrl = e.detail.imageUrl
  16. })
  17. }
  18. }
  19. </script>
  20. </we-element>
  21.  
  22. <we-element name="foo">
  23. <template>
  24. <div>
  25. <bar></bar>
  26. <text>{{title}}</text>
  27. </div>
  28. </template>
  29. <script>
  30. module.exports = {
  31. data: {
  32. title: ''
  33. },
  34. created: function() {
  35. var self = this
  36. this.$on('changeTitle', function(e) {
  37. self.title = e.detail.title
  38. })
  39. }
  40. }
  41. </script>
  42. </we-element>
  43.  
  44. <template>
  45. <div>
  46. <text onclick="test">click to update foo</text>
  47. <foo></foo>
  48. <foo></foo>
  49. </div>
  50. </template>
  51.  
  52. <script>
  53. module.exports = {
  54. methods: {
  55. test: function (e) {
  56. this.$broadcast('changeTitle', {
  57. title: '...'
  58. })
  59. this.$broadcast('changeImage', {
  60. imageUrl: '...'
  61. })
  62. }
  63. }
  64. }
  65. </script>

兄弟间通信

兄弟组件间通过公共的父组件作为桥梁来传递消息。

案例:

  1. <we-element name="foo">
  2. <template>...</template>
  3. <script>
  4. module.exports = {
  5. methods: {
  6. callbar: function () {
  7. this.$dispatch('callbar', {a: 1})
  8. }
  9. }
  10. }
  11. </script>
  12. </we-element>
  13.  
  14. <we-element name="bar">
  15. <template>...</template>
  16. <script>
  17. module.exports = {
  18. created: function() {
  19. this.$on('callbar', function(e) {
  20. // e.detail.a
  21. })
  22. }
  23. }
  24. </script>
  25. </we-element>
  26.  
  27. <template>
  28. <div>
  29. <foo></foo>
  30. <bar></bar>
  31. </div>
  32. </template>
  33.  
  34. <script>
  35. module.exports = {
  36. created: function () {
  37. var self = this
  38. this.$on('callbar', function(e) {
  39. self.$broadcast('callbar', e.detail)
  40. })
  41. }
  42. }
  43. </script>

最后,你将学习怎么给Weex页面写 配置 & 数据


还没有评论.