鍍金池/ 問答/HTML/ vue中通過修改父組件props進而修改子組件的props,但修改后子組件沒有響

vue中通過修改父組件props進而修改子組件的props,但修改后子組件沒有響應,請問是為什么?

我想自己實現(xiàn)一個tabbar的組件,因此定義了兩個組件來組合實現(xiàn):

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并沒有發(fā)生修改,子組件中也watch不到變更,沒有搞懂這是為什么?

回答
編輯回答
瘋子范

為什么要用計算屬性啊,你這個計算屬性里也沒有與data下的任何內(nèi)容綁定啊。
你直接用 data 試試

2017年9月7日 19:04
編輯回答
忘了我

你的tabitem組件沒有傳數(shù)據(jù)啊 你只傳到了tabbar里邊 ,tabbar要向子組件tabitem也傳數(shù)據(jù)啊

2017年1月30日 01:40