1
Vue.jsの使用を開始したばかりですが、しばらくしてから、何かをコンポーネントからvueインスタンスに伝達する方法を理解できません。 私は評価システムとして、このコンポーネントを使用していますが、私は私のメインインスタンス (https://fiddle.jshell.net/swyuarc9/)vueコンポーネントとvueインスタンス間の通信
Vue.component('star-rating', {
props: {
'name': String,
'value': null,
'id': String,
'disabled': Boolean,
'required': Boolean
},
template: '<div class="star-rating">\
<label class="star-rating__star" v-for="rating in ratings" \
:class="{\'is-selected\': ((value >= rating) && value != null), \'is-disabled\': disabled}" \
v-on:click="set(rating)" v-on:mouseover="star_over(rating)" v-on:mouseout="star_out">\
<input class="star-rating star-rating__checkbox" type="radio" :value="rating" :name="name" \
v-model="value" :disabled="disabled">★</label></div>',
/*
* Initial state of the component's data.
*/
data: function() {
return {
temp_value: null,
ratings: [1, 2, 3, 4, 5]
};
},
methods: {
/*
* Behaviour of the stars on mouseover.
*/
star_over: function(index) {
var self = this;
if (!this.disabled) {
this.temp_value = this.value;
return this.value = index;
}
},
/*
* Behaviour of the stars on mouseout.
*/
star_out: function() {
var self = this;
if (!this.disabled) {
return this.value = this.temp_value;
}
},
/*
* Set the rating of the score
*/
set: function(value) {
var self = this;
if (!this.disabled) {
// Make some call to a Laravel API using Vue.Resource
this.temp_value = value;
return this.value = value;
}
}
}
});
new Vue({
el: '#app'
});
(更新されたフォークリンク、SOZ) –
リンクしたリンクが正常に機能していますか?私が星にマウスを乗せると何も起こりません。 – Tom00
私は先に進んでおらず、あなたのソリューションを実装していませんでした(どのように正確に動作するのかもわかりません)、私はちょうどcomps間でデータを共有する方法を示す例を追加しました。正しい振る舞いを実装するのはあなた次第です:-) –