父组件给子组件传值
    index.vue

    1. <template>
    2. <view class="content">
    3. <test v-if="flag" :title='title' @myEvent='getNum'></test>
    4. <button type="default" @click="checkTest">切换test组件</button>
    5. 这是子组件传递过来的数据{{num}}
    6. <test-a></test-a>
    7. <test-b></test-b>
    8. </view>
    9. </template>
    10. <script>
    11. import test from '../../components/test.vue'
    12. import testa from '../../components/a.vue'
    13. import testb from '../../components/b.vue'
    14. export default {
    15. data() {
    16. return {
    17. title: 'Hello',
    18. flag:true,
    19. num:0
    20. }
    21. },
    22. onLoad(options) {
    23. console.log('页面加载了',options)
    24. },
    25. onShow(){
    26. console.log('页面显示了')
    27. },
    28. onReady() {
    29. console.log('页面初次渲染完成了')
    30. },
    31. onHide() {
    32. console.log('页面隐藏了')
    33. },
    34. methods: {
    35. checkTest(){
    36. this.flag = !this.flag
    37. }
    38. },
    39. components:{
    40. test,
    41. "test-a":testa,
    42. "test-b":testb
    43. },
    44. methods:{
    45. getNum(num){
    46. this.num = num
    47. }
    48. }
    49. }
    50. </script>
    51. <style>
    52. .content {
    53. display: flex;
    54. flex-direction: column;
    55. align-items: center;
    56. justify-content: center;
    57. }
    58. .logo {
    59. height: 200rpx;
    60. width: 200rpx;
    61. margin-top: 200rpx;
    62. margin-left: auto;
    63. margin-right: auto;
    64. margin-bottom: 50rpx;
    65. }
    66. .text-area {
    67. display: flex;
    68. justify-content: center;
    69. }
    70. .title {
    71. font-size: 36rpx;
    72. color: #8f8f94;
    73. }
    74. </style>

    test.vue

    1. <template>
    2. <view id='myView'>
    3. 这是test组件{{num}}
    4. 这是父组件传递过来的数据{{title}}
    5. <button type="default" @click="sendNum">给父组件传值</button>
    6. </view>
    7. </template>
    8. <script>
    9. export default {
    10. data() {
    11. return {
    12. num:3,
    13. intId:null
    14. };
    15. },
    16. props:['title'],
    17. beforeCreate(){
    18. console.log("实例已经开始初始化了")
    19. console.log(this.num) //这里没有定义
    20. },
    21. created() { //一般用来数据的初始化
    22. console.log('created',this.num)
    23. this.intId = setInterval(()=>{
    24. console.log('执行定时器')
    25. },1000)
    26. },
    27. beforeMount() {
    28. console.log('beforemount',document.getElementById('myView'))
    29. },
    30. mounted() {
    31. console.log('beforemount',document.getElementById('myView'))
    32. },
    33. destroyed() {
    34. console.log('组件销毁了')
    35. clearInterval(this.intId) //清除定时器
    36. },
    37. methods:{
    38. sendNum(){
    39. console.log('给父亲传值')
    40. this.$emit('myEvent',this.num)
    41. }
    42. }
    43. }
    44. </script>
    45. <style lang="scss">
    46. </style>

    a.vue

    1. <template>
    2. <view>
    3. 这是a组件:<button type="default" @click="addNum">修改B组件的数据</button>
    4. </view>
    5. </template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. };
    11. },
    12. methods:{
    13. addNum(){
    14. uni.$emit('updateNum',10)
    15. }
    16. }
    17. }
    18. </script>
    19. <style>
    20. </style>

    b.vue

    1. <template>
    2. <view>
    3. 这是B组件的数据:{{num}}
    4. </view>
    5. </template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. num:0
    11. };
    12. },
    13. created() {
    14. uni.$on('updateNum',num=>{
    15. this.num += num
    16. })
    17. },
    18. methods:{
    19. }
    20. }
    21. </script>
    22. <style>
    23. </style>
    • 父传子

    • 子传父

    • 同类传参

    $emit发送 $on监听
    image.png