parent
c8fca257b6
commit
444562e794
@ -0,0 +1,22 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
|
||||
// 静态路由
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
component: () => import('@/views/index.vue'),
|
||||
},
|
||||
];
|
||||
|
||||
// 动态模块
|
||||
const modules = import.meta.globEager('./modules/*.js');
|
||||
Object.values(modules).forEach((mod) => {
|
||||
routes.push(...mod.default);
|
||||
});
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
});
|
||||
|
||||
export default router;
|
@ -0,0 +1 @@
|
||||
export default [];
|
@ -0,0 +1,21 @@
|
||||
import { createStore } from 'vuex';
|
||||
|
||||
const modules = Object.fromEntries(
|
||||
Object.entries(import.meta.globEager('./modules/*.js')).map((entry) => {
|
||||
let arr = entry[0].split('/').pop().split('.');
|
||||
arr.pop();
|
||||
let moduleName = _.camelCase(arr.join('-'));
|
||||
return [
|
||||
moduleName,
|
||||
{
|
||||
...entry[1].default,
|
||||
namespaced: true,
|
||||
},
|
||||
];
|
||||
})
|
||||
);
|
||||
const store = createStore({
|
||||
strict: process.env.NODE_ENV !== 'production',
|
||||
modules,
|
||||
});
|
||||
export default store;
|
@ -0,0 +1,20 @@
|
||||
const state = () => ({
|
||||
count: 0,
|
||||
});
|
||||
const getters = {
|
||||
doubleCount: (state) => state.count * 2,
|
||||
};
|
||||
const mutations = {
|
||||
add: (state, num = 1) => (state.count += num),
|
||||
};
|
||||
const actions = {
|
||||
clear: ({ state, commit }) => {
|
||||
commit('add', state.count * -1);
|
||||
},
|
||||
};
|
||||
export default {
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
actions,
|
||||
};
|
@ -0,0 +1,27 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<h1>
|
||||
<span>马士兵严选</span>
|
||||
<XIcon name="vue" svg color="red" size="30" />
|
||||
<XIcon name="msb" svg size="30"></XIcon>
|
||||
<XIcon name="app-store" size="30" />
|
||||
</h1>
|
||||
<p>count:{{ count }}, double count:{{ doubleCount }}</p>
|
||||
<el-button type="primary" @click="handleAdd">add</el-button>
|
||||
<el-button type="danger" @click="handleClear">clear</el-button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
const store = useStore();
|
||||
const count = computed(() => store.state.demo.count);
|
||||
const doubleCount = computed(() => store.getters['demo/doubleCount']);
|
||||
const handleAdd = () => {
|
||||
store.commit('demo/add');
|
||||
};
|
||||
const handleClear = () => {
|
||||
store.dispatch('demo/clear');
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="less"></style>
|
Loading…
Reference in new issue