当前位置: 首页 > news >正文

vuex自存例子

src/
├── router/              # 路由配置
│   └── index.js         # 路由主文件
├── store/               # Vuex状态管理
│   ├── index.js         # Store主入口
├── views/               # 路由页面组件
│   ├── HomeView.vue
│   ├── AboutView.vue
├── App.vue              # 根组件
└── main.js              # 应用入口文件

 store/index.js :

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0, //变量
  },
  getters: {},
  mutations: {
    increment(state) {
      //修改状态-加
      state.count += 1;
    },
    decrement(state) {
      state.count -= 1;
    },
  },
  actions: {
    increment({ commit }) {
      commit("increment"); //调用mutation
    },
    decrement({ commit }) {
      commit("decrement"); // 调用 mutation
    },
  },
  modules: {},
});

main.js:

import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: function (h) { return h(App) }
}).$mount('#app')

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: function () {
      return import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

 App.vue:

<template>
  <div id="app"><router-view></router-view></div>
</template>

<script></script>

<style lang="scss"></style>

/views/HomeView.vue:

<template>
  <div>
    <p>count now {{ count }}</p>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <router-link to="/about">Go to About</router-link>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  computed: {
    //使用mapState映射state
    ...mapState(["count"]),
  },
  methods: {
    //使用mapActions映射actions
    ...mapActions(["increment", "decrement"]),
  },
};
</script>

<style lang="scss"></style>

/views/AboutView.vue:

<template>
  <div class="about">
    <h1
      style="
        font-style: italic;
        font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande',
          'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
      "
    >
      THE COUNTER {{ count }}
    </h1>
    <router-link to="/">Back to Home</router-link>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["count"]),
  },
};
</script>

http://www.dtcms.com/a/109400.html

相关文章:

  • linux_sysctl_fs_file_nr监控项
  • LeetCode刷题 -- 48. 旋转图像
  • 2025身份证号码前六位地区代码对照表
  • Android A/B 分区 OTA 如何查看升级的 img 分区
  • zk基础—4.zk实现分布式功能二
  • Ansible:playbook的高级用法
  • Unity中 JobSystem使用整理
  • LeetCode 接雨水问题详解 - 动态规划解法
  • CentOS 7安装hyperscan
  • LLM驱动的智能体:基于GPT的对话智能体开发指南
  • 如何学习一门编程语言
  • flux绘画模型介绍
  • Java学习总结-字符集
  • 项目之Boost搜索引擎
  • 六种光耦综合对比——《器件手册--光耦》
  • JavaWeb学习--MyBatis-Plus整合SpringBoot的ServiceImpl方法(查找部分)
  • Java在体育比分直播系统搭建中的应用
  • py文件打包为exe可执行文件,涉及mysql连接失败
  • leetcode76.最小覆盖子串
  • podman和与docker的比较 及podman使用
  • Linux红帽:RHCSA认证知识讲解(九)标准输入输出、重定向、过滤器与管道
  • PyTorch的dataloader制作自定义数据集
  • Golang改进后的任务调度系统分析
  • MySQL的进阶语法12(MySQL管理)
  • [250403] HuggingFace 新增检查模型与电脑兼容性的功能 | Firefox 发布137.0 支持标签组
  • 数据库系统-数据库模式
  • UART双向通信实现(序列机)
  • (三十)导入系统内置库 math库
  • 基于大模型预测升主动脉瘤的多维度诊疗研究报告
  • 抖音短视频安卓版流畅度测评 - 真实