1
如何在與VueX集成的同時使用VueJS中的計算屬性綁定樣式。使用計算屬性和VueX進行樣式綁定
我遇到的問題是關於我的樣式屬性在我的VueX Store中發生更改後未更新。
代碼示例:
//VueX Store
const store = new Vuex.Store({
\t state : {
\t \t div: [
\t \t \t {
\t \t \t \t offset: 0,
\t \t \t \t width: 1,
\t \t \t \t text : 'Hello World'
\t \t \t },
\t \t \t {
\t \t \t \t offset: 0,
\t \t \t \t width: 1,
\t \t \t \t text : 'Hello World As Well'
\t \t \t }
\t \t ]
}
});
//My component
<template>
\t <div v-bind:style="{ width: width, left: offset}">
\t \t <p>{{text}}</p>
\t </div>
</template>
<script>
export default {
\t \t name: 'divBox',
\t \t computed : {
\t \t \t text : function() {
\t \t \t \t return this.$store.state.div[this.Id].text;
\t \t \t },
\t \t \t width : function() {
\t \t \t \t return this.$store.state.div[this.Id].width;
\t \t \t },
\t \t \t offset : function() {
\t \t \t \t return this.$store.state.div[this.Id].offset;
\t \t \t }
\t \t },
\t \t props : ['Id']
}
</script>
This Works!非常感謝! 我有突變設置BTW - >但由於某些原因,他們不能正常工作。 –
@NathanielDeshpande如果這回答了這個問題,請不要忘記接受它作爲正確答案。如果你想出了更好的答案,一定要發佈它。 – qw3n