Vue.js 앱의 vuex 스토어를 사용하여 하위 구성 요소(설정 페이지)에서 값을 설정하는 방법은 무엇입니까?
선택한 값을 저장소에 저장하여 다른 모든 구성 요소가 해당 값을 사용하여 모양을 변경할 수 있도록 하는 "설정" 구성 요소를 만들려고 합니다.
[설정]vue:
[...]
<p>{{ themeColor }}</p>
<v-radio-group v-model="themeColor">
<v-radio label="light" value="light"></v-radio>
<v-radio label="dark" value="dark"></v-radio>
</v-radio-group>
[...]
<script>
export default {
data () {
return {
// default value
themeColor: 'light',
}
},
computed: {
themeColor () {
return this.$store.state.themeColor
}
},
methods: {
changeThemeColor() {
this.$store.commit('changeThemeColor')
},
}
}
</script>
이 설정의 선택된 값을 스토어에 올바르게 송신하는 방법을 모르기 때문에 메서드로 변환(기본값의 필요성 등)을 작성했을 뿐입니다.themeColor: 'light'
위와 같이 좀 더 헷갈리게 해주세요.)
저장/모듈/설정.js
const state = {
themeColor: ''
}
const mutations = {
changeThemeColor: state => {
state.themeColor = ''
}
}
export default {
state,
mutations
}
이 값을 모든 컴포넌트에 사용할 수 있도록 하려면 어떻게 해야 합니까?
이런 걸 써야 하나요?getters
또는actions
잘 모르겠어요
https://vuex.vuejs.org/en/forms.html,에서 getter와 setter를 사용하여 계산된 속성을 사용합니다.
export default {
computed: {
themeColor: {
get () {
return this.$store.state.themeColor
},
set (value) {
this.$store.commit('changeThemeColor', value)
}
}
}
}
주의해 주세요.data
또는methods
.
당신의 가게는 또한 더 닮아야 한다.
const state = {
themeColor: 'light' // default value
}
const mutations = {
changeThemeColor (state, themeColor) {
state.themeColor = themeColor
}
}
데모 ~ https://codepen.io/anon/pen/YYbPww?editors=1011
를 표시/읽고 싶은 경우themeColor
컴포넌트 상태, 도우미 사용을 권장합니다.
import { mapState } from 'vuex'
export default {
// ...
computed: mapState(['themeColor'])
}
언급URL : https://stackoverflow.com/questions/48412978/how-to-set-values-from-a-child-component-settings-page-using-vuex-store-in-a-v
'programing' 카테고리의 다른 글
"typedef"는 유형과 에일리어스 사이에 표준 적합성이 있습니까? (0) | 2022.08.11 |
---|---|
Graphql 아폴로 서버 + Vue = > 이미지 업로드 (0) | 2022.08.11 |
내 컴퓨터의 Java SDK 폴더는 어디에 있습니까?우분투 12.04 (0) | 2022.08.11 |
상위 구성 요소 Vue에서 하위 구성 요소의 메서드를 호출하는 방법 (0) | 2022.08.11 |
할당된 메모리에서 free()를 사용하지 않아도 될까요? (0) | 2022.08.11 |