javascript - 刷新时的VueJs没有运行axios
问题描述
我的 VueJs 应用程序有一些问题。我从 ThemeForest 购买了 Vuexy 模板。所以我创建了新的组件调用者 CountryTable.vue,它实际上可以工作,但是当我刷新页面时它不起作用,只有当我登录或更改另一个页面时,但是当我刷新它时显示该表是空的。以下是 CountryTable 的样子:
<template>
<div class="vx-col w-full">
<vx-card title="Countries">
<div slot="no-body" class="mt-4">
<vs-table class="table-dark-inverted" :data="activeCountries">
<template slot="thead">
<vs-th sort-key="name" colspan="2">Country</vs-th>
<vs-th sort-key="sales">Sales</vs-th>
<vs-th sort-key="products">Products</vs-th>
<vs-th sort-key="accessories">Accessories</vs-th>
<vs-th sort-key="basket">Basket</vs-th>
<vs-th sort-key="deliveries">Deliveries</vs-th>
<vs-th sort-key="amount">Amount</vs-th>
</template>
<template slot-scope="{data}" class="text-center">
<vs-tr :data="tr" :key="indextr" v-for="(tr, indextr) in data" :style="{animation: 'flipInX ' + (indextr * 0.2) + 's'}">
<vs-td :data="data[indextr].name">
<img width="30" :src="data[indextr].flag" :alt="data[indextr].name">
</vs-td>
<vs-td :data="data[indextr].name">
{{ data[indextr].name }}
</vs-td>
<vs-td :data="data[indextr].sales">
{{ data[indextr].sales }}
</vs-td>
<vs-td :data="data[indextr].products">
{{ data[indextr].products }}
</vs-td>
<vs-td :data="data[indextr].accessories">
{{ data[indextr].accessories }}
</vs-td>
<vs-td :data="data[indextr].basket">
{{ data[indextr].basket }}
</vs-td>
<vs-td :data="data[indextr].deliveries">
{{ data[indextr].deliveries }}
</vs-td>
<vs-td :data="data[indextr].amount">
{{ data[indextr].amount }}
</vs-td>
</vs-tr>
</template>
</vs-table>
</div>
</vx-card>
</div>
</template>
<script>
import { mapState } from "vuex";
import Countries from "../../http/Countries";
export default {
data() {
return {
selected: [],
'tableList': [
'vs-th: Component',
'vs-tr: Component',
'vs-td: Component',
'thread: Slot',
'tbody: Slot',
'header: Slot'
]
}
},
computed: {
...mapState({
activeCountries: state => state.activeCountries
})
},
mounted() {
Countries.activeCountries().then(response => {
this.$store.commit("ACTIVE_COUNTRIES", response.data.countries);
});
}
}
</script>
国家文件如下所示:
activeCountries() {
return Api().get("/userCountries");
},
这是API:
import axios from "axios";
let BaseApi = axios.create({
baseURL: "http://127.0.0.1:8000/api/"
});
let Api = function() {
let token = localStorage.getItem("accessToken");
if (token) {
BaseApi.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
return BaseApi;
};
export default Api;
在控制台中我有如下错误:
TypeError:传播不可迭代实例的无效尝试
如您所见,我创建了一个简单的表,称为函数,它实际上是与 axios 请求的链接并映射了它们。挂载功能有问题吗?我尝试创建和更新但没有成功。
解决方案
看起来像 mapState 问题。
尝试改变
...mapState({
activeCountries: state => state.activeCountries
})
至
...mapState(['activeCountries'])
推荐阅读
- codenameone - 代号:通过 applink 打开应用时,iOS 上的 AppArg 为空
- selenium - 升级到 Selenium Grid 4.0.0 后,Chrome 节点未注册到 Selenium Hub
- angular - 角度路线与哈希不匹配
- r - 根据条件从上一行中提取值
- python - Python openpyxl 告诉我“没有这样的文件或目录:”即使 python 文件与 excel 文件位于同一目录中,等等
- python - 存储库模式:ptyhon 中的方法命名、singledispatch 和 **kwargs
- vim - 有没有办法用 print() 语句或 vim 中的任何函数包含变量,特别是使用 vim vs 代码扩展
- javascript - 将组件的道具传递给 useMemo 得到取消定义
- powershell - powershell如何设置不超过15个字符的字符限制,如果超过则提示重新输入名称
- ios - 如何将 CoreMotion 时间戳转换为正确的日期?