.row {
padding-bottom: 0.5rem;
}
button {
display: inline-block;
padding: 0 1rem;
color: #555;
text-align: center;
font-size: 0.75rem;
font-weight: 600;
line-height: 1.5rem;
letter-spacing: 1px;
text-transform: uppercase;
text-decoration: none;
white-space: nowrap;
background-color: transparent;
border-radius: 0.25rem;
border: 1px solid #bbb;
cursor: pointer;
box-sizing: border-box;
}
button:hover,
button:focus{
color: #333;
border-color: #888;
outline: 0;
}
<div id="app">
<temp />
</div>
<script src="https://cdn.jsdelivr.net/npm/flyd/flyd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script name="store">
const initialState = {
temperature: {
value: 22,
unit: "C"
}
};
const update = flyd.stream(state => state);
const getState = flyd.scan(
(state, patchFn) => patchFn(state),
initialState,
update,
);
const actions = {
changeTemp: amount => update(state => ({
...state,
temperature: {
...state.temperature,
value: state.temperature.value + amount
}
})),
changeTempUnit: () => update(state => ({
...state,
temperature: {
...state.temperature,
unit: state.temperature.unit === "C" ? "F" : "C",
value: state.temperature.unit === "C"
? Math.round((state.temperature.value * 9) / 5 + 32)
: Math.round(((state.temperature.value - 32) / 9) * 5)
}
}))
};
const store = {
getState,
actions
};
</script>
<script name="app">
new Vue({
el: "#app",
components: {
'temp': {
data() {
const initial = store.getState();
return {
temperature: initial.temperature.value,
unit: initial.temperature.unit
};
},
mounted() {
store.getState.map(s => {
this.temperature = s.temperature.value;
this.unit = s.temperature.unit;
});
},
methods: {
incTemp() {
store.actions.changeTemp(+1);
},
decTemp() {
store.actions.changeTemp(-1);
},
changeUnit() {
store.actions.changeTempUnit();
}
},
props: [],
template: `
<div class="temp-component">
<div class="row">
<button @click="incTemp">+</button>
<button @click="decTemp">−</button>
<span>The temperature is {{temperature}} °{{unit}}</span>
</div>
<div class="row">
<button @click="changeUnit">Change temperature unit</button>
</div>
</div>
`
}
}
});
</script>
exampleMethod
(例如,从一个带有this.$store
的 Vue 组件)? - tony19implementation
可以知道core
,但反之不行,让core
导出一个静态回调方法,用于传递对在implementation
实例化的store
的引用 - 类似于Core.setStore(storeInstance)
。 - Richard Matsen