我正在学习Vuejs,在我的项目中,我有一些警告和没有显示,因为我试图使用商店(VUEX)
所以我有:
├── index.html └── src ├── views └── Feed.vue └──store └── index.ts ├── App.vue ├── main.ts
我的主。ts:
// initialisation des éléments pours VUE
import { createApp } from "vue";
import App from "./App.vue";
import "./registerServiceWorker";
import router from "./router";
import store from "./store";
import axios from 'axios'
import VueAxios from 'vue-axios'
// initialisation des imports pour Font Awesome
import { library } from "@fortawesome/fontawesome-svg-core";
import { faUserSecret } from "@fortawesome/free-solid-svg-icons";
import {
faSpinner,
faPaperPlane,
faHandPeace,
faSignOutAlt,
faShieldAlt,
faUserPlus,
faImage,
faEdit,
faUsersCog,
faTools,
faRss,
faTimesCircle,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
// initialisation des imports pour Bootstrap
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap";
import "bootstrap/dist/js/bootstrap.js";
// intégration des icones font awesome à la librairy
library.add(faUserSecret);
library.add(faSpinner);
library.add(faPaperPlane);
library.add(faHandPeace);
library.add(faSignOutAlt);
library.add(faShieldAlt);
library.add(faUserPlus);
library.add(faImage);
library.add(faEdit);
library.add(faEdit);
library.add(faUsersCog);
library.add(faTools);
library.add(faRss);
library.add(faTimesCircle);
// Création de l'app
const app = createApp(App);
app.use(store);
app.use(router);
app.use(VueAxios, axios);
app.provide('axios', app.config.globalProperties.axios) ;
app.component("font-awesome-icon", FontAwesomeIcon);
app.mount("#app");
我。/存储/index.ts
// Imports
import { createApp } from 'vue'
import { createStore } from 'vuex'
import axios from 'axios';
//Create Store :
export const store = createStore({
// Define state
state () {
return{
feedList: []
}
},
// Define Actions
actions: {
getPosts( { commit } ) {
axios.get('http://localhost:3001/api/feed')
.then(feedList => {
commit('setFeedList', feedList)
})
}
},
// Define mutations
mutations: {
setFeedList(state:any, feedList:any) {
state.data = feedList
}
},
})
我的。/视图/饲料。vue:
<template>
<div id="Feed">
<div id="profil" class="col-12 col-md-3">
<Cartridge />
</div>
<div id="feedcontent" class="col-12 col-md-9">
<SendPost />
<div id="testlist" v-for="thePost in feedList" :key="thePost">
{{ thePost }}
</div>
</div>
</template>
<script lang="ts">
import Cartridge from '@/components/Cartridge.vue';
import SendPost from '@/components/SendPost.vue';
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Feed',
components: {
Cartridge,
SendPost
},
setup () {
const store = useStore();
const feedList = computed(() => store.state.data.feedList);
console.log("feedList > " + feedList.value);
return {
feedList
}
},
}
</script>
<style lang="scss">
@import "../scss/variables.scss";
#profil {
position: fixed;
margin: 0;
height: 100%;
}
#feedcontent {
position: fixed;
height: 100%;
right: 0;
background-color: $groupo-colorLight1;
overflow-y: scroll;
::-webkit-scrollbar {
width: 8px;
color: $groupo-color1;
}
::-webkit-scrollbar-track {
background: $groupo-colorLight2;
width: 20px;
-webkit-box-shadow: inset 0 0 6px rgba($groupo-color4, 0.3);
}
::-webkit-scrollbar-thumb {
background: $groupo-color4;
}
::-webkit-scrollbar-thumb:hover {
background: $groupo-color1;
}
}
</style>
看起来我不懂Vuex的一些东西,但我不知道是什么。你能告诉我怎么了吗?(我希望不是很多事情:))/有人能帮我吗?请。
感谢看起来您正在从./store/index.ts
中获取错误,您试图导出将函数调用应用于该行上的存储实例的结果:
export default store();
存储实例实际上不是一个函数,并且您在main.ts
中导入结果。尝试只导出存储实例:
export default store
可能你不需要后面的行:
const app = createApp({})
app.use(store)
你得到这个错误是因为你试图调用一个对象,而不是一个函数。
您的错误位于./store/index.ts文件中。你犯了两个错误。第一个是您试图调用createStore
两次。第二种是你试图从你的商店创建另一个应用。
在这里我分享给你的修复。
// createStore() retuns an object that you the pass it in your main.ts.
const store:any = createStore({
// all your store definitions...
})
在同一个文件的末尾,您应该只导出createStore()
返回的对象,这样您就可以在app.use()
(位于main.ts中)注册它。
export default store
// remove the code bellow...
编辑:您还在setup选项中犯了一个错误。您应该导入已经创建的存储。
import { store } from 'path/to/your/store/index.ts'
setup () {
const myStore = store
// property data doesn't exist in your state.
const feedList = computed(() => myStore.state.feedList);
console.log("feedList > " + feedList.value);
return {
feedList
}
除了卢卡斯·大卫·费雷罗的精彩回答,我还忘了一件事....在我的Feed里。Vue:安装商店!!所以我的文件现在是:
<template>
<div id="Feed">
<div id="profil" class="col-12 col-md-3">
<Cartridge />
</div>
<div id="feedcontent" class="col-12 col-md-9">
<SendPost />
<div id="testlist" v-for="thePost in feedList" :key="thePost">
{{ thePost }}
</div>
</div>
</div>
</template>
<script lang="ts">
import Cartridge from '@/components/Cartridge.vue';
import SendPost from '@/components/SendPost.vue';
import { computed } from 'vue'
import { store } from '../store/index'
export default {
name: 'Feed',
components: {
Cartridge,
SendPost,
},
setup() {
const myStore: any = store
const feedList = computed(() => myStore.state.feedList);
console.log("feedList > " + feedList.value);
return {
feedList
};
},
mounted() {
const myStore: any = store
myStore.dispatch("getPosts");
}
}
</script>
<style lang="scss">
@import "../scss/variables.scss";
#profil {
position: fixed;
margin: 0;
height: 100%;
}
#feedcontent {
position: fixed;
height: 100%;
right: 0;
background-color: $groupo-colorLight1;
overflow-y: scroll;
::-webkit-scrollbar {
width: 8px;
color: $groupo-color1;
}
::-webkit-scrollbar-track {
background: $groupo-colorLight2;
width: 20px;
-webkit-box-shadow: inset 0 0 6px rgba($groupo-color4, 0.3);
}
::-webkit-scrollbar-thumb {
background: $groupo-color4;
}
::-webkit-scrollbar-thumb:hover {
background: $groupo-color1;
}
}
</style>
非常感谢我的帮手!!