我是 vueJS 的新手,我正在尝试submenu
在“标题”组件中创建动态。
应用程序.vue
<template>
<div>
<Header />
<router-view />
<Footer />
</div>
</template>
我使用 store fromVuex
为我的submenu
物品设置全局状态
存储/index.js:
import { createStore } from 'vuex'
export default createStore({
state: {
cats_sub_menu: []
},
mutations: {
SET_SUB_MENU(state, payload) {
state.cats_sub_menu = payload;
}
},
actions: {
setSubMenu({ commit }, payload) {
commit("SET_SUB_MENU", payload)
}
},
})
我设置了我的 Header 组件:
<template>
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<router-link class="navbar-brand" to="/">Test Website</router-link>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li>
<router-link
class="nav-link"
active-class="active"
to="/page/about-us"
>About</router-link
>
</li>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Blog
</a>
<div
class="dropdown-menu"
aria-labelledby="navbarDropdown"
>
<router-link
class="dropdown-item"
active-class="active"
v-for="{ blogCat, i } in blog_sub_menu"
:key="i"
to="blog"
>{{ blogCat.title_el }}</router-link
>
</div>
</li>
<li>
<router-link
class="nav-link"
active-class="active"
to="/page/contact"
>Contact</router-link
>
</li>
</ul>
</div>
</nav>
</header>
</template>
<script>
import { computed } from "vue";
import axios from "axios";
import store from "../../../store";
export default {
name: "Header",
setup() {
function setSubCatsMenu() {
axios
.get(`http://127.0.0.1:5000/api/blog_category`)
.then((response) =>
store.dispatch("setSubMenu", response.data)
);
}
const blog_sub_menu = computed(() => store.state.cats_sub_menu);
return {
store,
blog_sub_menu,
setSubCatsMenu,
};
},
mounted() {
this.setSubCatsMenu();
},
};
</script>
我检查了 api 调用的有效性,没有任何问题,但仍然blog_sub_menu
没有更新。有没有人有任何想法?
干杯!