image.png
image.png
image.png
image.png
image.png

自定义组件中的事件

image.png

源码

list.vue

  1. <template>
  2. <view class="list" @click="onClick">
  3. <text>{{name}}</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. props:{
  9. name:{
  10. type:String,
  11. defaule:""
  12. }
  13. },
  14. data() {
  15. return {
  16. };
  17. },
  18. methods:{
  19. onClick:function(){
  20. //自定义组件内调用 使用组件页面的 方法
  21. this.$emit('onClick',this.name);
  22. console.log("组件内容被点击"+this.name);
  23. }
  24. }
  25. }
  26. </script>
  27. <style>
  28. .list{
  29. height: 50px;
  30. line-height: 50px;
  31. border-bottom: 1px #F0AD4E solid;
  32. padding-left:15px ;
  33. }
  34. </style>

index.vue

  1. <template>
  2. <view >
  3. <list name="uni-app" @onClick="onClick"></list>
  4. <list name="js" @onClick="onClick"></list>
  5. <list name="html"></list>
  6. </view>
  7. </template>
  8. <script>
  9. /*
  10. 自定义组件使用部长
  11. 1. 项目根目录创建 components目录
  12. 2. 创建组件
  13. 3. 引入组件
  14. 4. 注册组件
  15. 5. 传参
  16. 6. 事件通讯
  17. */
  18. //引入组件
  19. import list from '@/components/list.vue'
  20. export default {
  21. //注册组件
  22. components:{
  23. list
  24. },
  25. data() {
  26. return {
  27. }
  28. },
  29. methods: {
  30. onClick:function(e){
  31. console.log("调用自定义组件的页面");
  32. console.log(e);
  33. }
  34. }
  35. }
  36. </script>
  37. <style>
  38. </style>

官网组件

image.png