账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue中通过修改父组件props进而修改子组件的props,但修改后子组件没有响应,请问是为什么?
    25
    0

    我想自己实现一个tabbar的组件,因此定义了两个组件来组合实现:

    1. TabBar

    <template>
      <div class="tabbar">
        <tab-item v-for="item in tabItems"
          :key="item.index"
          v-bind="item"
          @tabItemOnClick="tabItemOnClick"
        >
        </tab-item>
      </div>
    </template>
    <script>
    import TabItem from './tabitem.vue'
    export default {
      name: 'tab-bar',
      components: {TabItem},
      props: {
        tabItems: {
          default: []
        }
      },
      data () {
        return {
          selectedIndex: 0
        }
      },
      created () {
        this.select(this.selectedIndex)
      },
      methods: {
        tabItemOnClick (params) {
          this.selectedIndex = params.index
          this.select(this.selectedIndex)
          this.toast(this.selectedIndex)
        },
        select (index) {
          for (let i = 0; i < this.tabItems.length; i++) {
            var tabItem = this.tabItems[i]
            if (i === index) {
              tabItem.selected = true
            } else {
              tabItem.selected = false
            }
          }
        }
      }
    }
    </script>
    <style>
      .tabbar {
        background-color: #ffffff;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 100px;
        flex-wrap: nowrap;
        flex-direction: row;
      }
    </style>
    

    2.TabBarItem

    <template>
      <div class="bar-item" @click="onTabItemClick">
        <text class="icon iconfont" :class="[ selected ? 'active' : '' ]">{{ icon }}</text>
        <text class="text" :class="[ selected ? 'active' : '' ]">{{ title }}</text>
        <div v-if="parseInt(dot) === 0">
          <text :class="[ needNum ? 'notice' : 'dot' ]">{{ dot }}</text>
        </div>
      </div>
    </template>
    <script>
    export default {
      name: 'tab-item',
      props: {
        index: {
          type: Number,
          required: true
        },
        icon: {
          type: String,
          default: ''
        },
        title: {
          type: String,
          default: ''
        },
        dot: {
          type: String,
          default: '0'
        },
        needNum: {
          type: Boolean,
          default: true
        },
        selected: {
          type: Boolean,
          default: false
        }
      },
      data () {
        return {
        }
      },
      methods: {
        setNum (num) {
          if (num <= 0) {
            this.dot = ''
          } else {
            this.dot = num + ''
          }
        },
        onTabItemClick () {
          var params = {
            index: this.index
          }
          this.$emit('tabItemOnClick', params)
        }
      },
      watch: {
        selected: function (val) {
          this.toast('selected: ' + this.selected + ', ' + val)
        }
      }
    }
    </script>
    
    <style>
      .bar-item {
        background-color: #afddff;
        flex: 1;
      }
      .text, .icon {
        color: #666666;
        text-align: center;
      }
      .iconfont {
        font-family: iconfont;
      }
      .icon {
        padding-top: 14px;
        font-size: 38px;
      }
      .text {
        font-size: 22px;
        padding-top: 2px;
      }
      .active {
        color: #b4282d
      }
      .notice {
        position: absolute;
        top: 10px;
        right: 30px;
        width: 30px;
        height: 30px;
        border-radius: 100%;
        font-size: 26px;
        line-height: 30px;;
        text-align: center;
        color: #ffffff;
        background-color: #ff0000;
      }
      .dot {
        position: absolute;
        top:15px;
        right: 40px;
        height: 15px;
        width: 15px;
        border-radius: 100%;
        background-color: #ff0000;
      }
    </style>
    

    3. 使用

    <tab-bar :tabItems="tabs"></tab-bar>

    tabs通过computed计算属性赋值:

    computed: {
        tabs: function () {
          return [{
            index: 0,
            icon: '&#xe660;',
            title: '首页',
            dot: '0',
            needNum: true,
            selected: true
          },
          {
            index: 1,
            icon: '&#xe605;',
            title: '分类',
            dot: '1',
            needNum: true,
            selected: false
          },
          {
            index: 2,
            icon: '&#xe61a;',
            title: '购物车',
            dot: '5',
            needNum: true,
            selected: false
          },
          {
            index: 3,
            icon: '&#xe639;',
            title: '我',
            dot: '1',
            needNum: false,
            selected: false
          }]
        }
      }

    当点击TabBarItem后,点击事件可以传递到父组件并且修改选中tabItem的selected属性,但修改后tabBarItem组件的style并没有发生修改,子组件中也watch不到变更,没有搞懂这是为什么?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部