vue3.0 中使用vuex 和 vue-router4

本文档展示了如何在Vue3.0项目中集成Vuex状态管理和Vue-Router4路由配置。通过创建App.vue、main.js、router/index.js和store/index.js文件,实现了基本的页面路由和状态存储。在Home.vue组件中,使用了Vuex的mutations和actions进行数据操作,并展示在页面上。项目结构清晰,适合初学者理解Vue3、Vuex和Vue-Router的协同工作。

vue3.0 中使用vuex 和 vue-router4

目录结构:

|-- babel_webpack
    |-- .gitignore
    |-- index.html
    |-- package.json
    |-- vite.config.js 
    |-- public
    |   |-- favicon.ico
    |-- src
        |-- App.vue  // 需要用到
        |-- main.js  // 需要用到
        |-- assets
        |   |-- logo.png
        |-- components
        |   |-- HelloWorld.vue
        |-- router
        |   |-- index.js  // 需要用到
        |-- store
        |   |-- index.js  // 需要用到
        |-- views
            |-- Home.vue  // 需要用到

App.vue

<template>
  <router-view />
</template>

main.js

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

const app = createApp(App);
app.use(router);
app.use(store);
app.mount('#app');

router/index.js

import { createRouter, createWebHashHistory } from 'vue-router';
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        { path: '/', component: () => import('../views/Home.vue') }
    ]
});
export default router;

store/index.js

import { createStore } from 'vuex';

export default createStore({
    state: {
        count: 0
    },
    mutations: {
        getIncrement(state, val = 0) {
            state.count += val
        }
    },
    actions: {
        actionsIncrement(content, val) {
            content.commit('getIncrement', val)
        }
    }
})

views/Home.vue

<template>
    <div>Home....</div>
    <div>store:{{ $store.state.count }}</div>
    <div>num:{{ num }}</div>
    <br />
    <button @click="mutationsAdd">mutations-加加</button>
    <button @click="actionsAdd">actions-加加</button>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from "vuex";
export default {
    setup() {
        let num = reactive({
            num: 0
        })
        const store = useStore(); // 使用useStore方法
        function mutationsAdd() {
            store.commit('getIncrement', 1000)
            num.value = store.state.count;
            console.log(num.value);
        };
        function actionsAdd() {
            store.dispatch('actionsIncrement', '66')
            num.value = store.state.count;
        }
        return { mutationsAdd, actionsAdd, num };
    }
}
</script>

pages.json

{
  "name": "03-vite",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  },
  "dependencies": {
    "mddir": "^1.1.1",
    "vue": "^3.0.5",
    "vue-router": "^4.0.11",
    "vuex": "^4.0.2"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^1.3.0",
    "@vue/compiler-sfc": "^3.0.5",
    "vite": "^2.4.4"
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端酱紫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值