2016-03-01 18 views
0

如何在單選按鈕上使用$dispatch()$broadcast()?我不能做這樣的事情(因爲我不能單選按鈕使用v-on:click):Vue.js使用v-on:使用單選按鈕單擊

HTML

<div class="radio radio-primary"> 
    <label> 
     <input type="radio" name="intern" id="intern" value="intern" 
       v-on:click="update" v-model="selectedrole"/> 
      Showall 
     </label>       
</div> 

JS

Vue.component('searchemployees', { 
    template: '#searchemployees', 
    data: function() 
    { 
     return { 
      selectedrole: '' 
     } 
    }, 
    methods: { 
     update: function() 
     { 
      this.$dispatch('selectedRole', this.selectedrole) 
     } 
    } 
}); 

Vue.component('employees', { 
    template: '#employees', 
    props:['list'], 
    data: function() 
    { 
     return { 
      role: '' 
     } 
    }, 
    created() { 
     this.list = JSON.parse(this.list); 
    }, 
    events: { 
     'selectedrole': function(role) { 
      this.role = role 
     } 
    } 
}); 

因爲我不能用單選按鈕上的v-on:click。我怎樣才能做到這一點? (我需要在兩個組件中選擇)。

請幫忙!

+0

這似乎是你問你原來​​的問題以不同的方式:http://stackoverflow.com/questions/35727169/vue-js-use-variable-within-a-component-in-another-component。你應該剛剛更新你的原始問題。 – drneel

回答

0

您可以隨時播放使用watchselectedrole變化的情況下:

Vue.component('searchemployees', { 
    template: '#searchemployees', 
    data: function() 
    { 
     return { 
     selectedrole: '' 
     } 
    }, 
    watch: { 
     selectedrole: function(newRole) 
     { 
     this.$dispatch('selectedRole', newRole) 
     } 
    } 
}); 

然後,只需刪除點擊監聽器,它不需要