index.vue

  1. <template>
  2. <view>
  3. {{title}}
  4. <hr />
  5. <btn color='blue' @AAA=CC> 自定义</btn> //使用props 传值
  6. </view>
  7. </template>
  8. <script>
  9. import btn from '../../component/btn.vue'
  10. export default {
  11. data() {
  12. return {
  13. title: 'Hello',
  14. }
  15. },
  16. components: {
  17. btn
  18. },
  19. onLoad() {
  20. setTimeout(() => {
  21. this.title = '数据改变'
  22. }, 1000)
  23. },
  24. methods: {
  25. CC(e) {
  26. console.log(e)
  27. }
  28. }
  29. }
  30. </script>

btn.vue 组件内

  1. <template>
  2. <view class="btn-box" v-bind:style="{color:color}" v-on:click="open">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. props:{
  9. color:{
  10. type:String,
  11. dafult:'#000'
  12. }
  13. },
  14. data() {
  15. return {
  16. }
  17. },
  18. methods:{
  19. open(){
  20. console.log(' btn 被点击了')
  21. this.$emit('AAA',this.color) //使用$emit 传值
  22. }
  23. }
  24. }
  25. </script>