ruby - Vuex 通过 slim (Ruby on rails) 获取数据
问题描述
如果我通过 Slim 接收数组,如何传输数据?
regions-list :region=@regions
regions-list
- 我的组件 vue
:region
- 带有项目的数组
@regions
- 带有来自后端的项目的变量
我是新手vuex
,我想,我需要这样的东西,但不知道如何用物品传达数组
解决方案
这就是组织 Vuex 工作的方式
export default new Vuex.Store({
state: {
reactions: [],
},
mutations: {
setReactions(state, segment) {
state.reactions = segment;
},
},
actions: {
async loadReactions({ commit }) {
try {
const reactions = '... response/request';
commit('setReactions', reactions); // Here we use mutation to put new data into state.
} catch (e) {
// ...
}
},
},
});
在您的组件 vue 区域列表中
<template>
<div>{{ reactions }}</div> <!-- Here you can display and look at the content -->
</template>
<script>
import { mapState, mapActions } from 'vuex';
export default {
name: 'RegionsList',
computed: {
...mapState(['reactions']), // This is get the state
},
created() {
this.loadReactions(); // Here you perform a function that receives data and puts it in state
},
methods: {
...mapActions(['loadReactions']),
},
};
</script>
<style scoped></style>
推荐阅读
- c# - 带有选项模式的依赖注入
- android - ModuleRegistryConsumer 弹出后出现裸反应原生错误
- angular - Angular 8 - 创建一个主服务来提供服务
- c# - 并行执行 ForEach - C#
- arrays - F#:n 个空列表的数组
- python - 在虚拟框中的 Ubuntu 18.04 中的共享目录中创建虚拟环境失败
- spring-boot - 如何使用 Spring-kafka @KafkaListener 从来自 kafka 的消息中读取特定数据?
- javascript - 有没有办法在给定的href链接上重定向我的页面而不加载
- spartacus-storefront - 如何使用自定义 API 连接 Spartacus
- android - 在android中禁用电源按钮不起作用