Vue07

news2025/2/12 1:07:12

一、Vuex 概述

目标:明确Vuex是什么,应用场景以及优势

1.是什么

Vuex 是一个 Vue 的 状态管理工具,状态就是数据。

大白话:Vuex 是一个插件,可以管理 Vue 通用的数据 (多组件共享的数据)。例如:购物车数据 个人信息数据

2.使用场景

  • 某个状态 在 很多个组件 中使用 (个人信息)

  • 多个组件 共同维护 一份数据 (购物车)

3.优势

  • 共同维护一份数据,数据集中化管理

  • 响应式变化

  • 操作简洁 (vuex提供了一些辅助函数)

4.注意:

官方原文:

  • 不是所有的场景都适用于vuex,只有在必要的时候才使用vuex

  • 使用了vuex之后,会附加更多的框架中的概念进来,增加了项目的复杂度 (数据的操作更便捷,数据的流动更清晰)

Vuex就像《近视眼镜》, 你自然会知道什么时候需要用它~

二、需求: 多组件共享数据

目标:基于脚手架创建项目,构建 vuex 多组件数据共享环境,比如,根组件有一个数据是100,将来这个数据要在Son1子组件、和Son2子组件中也要用到。

效果是三个组件共享一份数据:

  • 任意一个组件都可以修改数据

  • 三个组件的数据是同步的

1.创建项目

vue create vuex-demo

2.创建三个组件, 目录如下

|-components
|--Son1.vue
|--Son2.vue
|-App.vue

3.源代码如下

App.vue在入口组件中引入 Son1 和 Son2 这两个子组件

<template>
  <div id="app">
    <h1>根组件</h1>
    <input type="text">
    <Son1></Son1>
    <hr>
    <Son2></Son2>
  </div>
</template>

<script>
import Son1 from './components/Son1.vue'
import Son2 from './components/Son2.vue'

export default {
  name: 'app',
  data: function () {
    return {

    }
  },
  components: {
    Son1,
    Son2
  }
}
</script>

<style>
#app {
  width: 600px;
  margin: 20px auto;
  border: 3px solid #ccc;
  border-radius: 3px;
  padding: 10px;
}
</style>

main.js

import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

Son1.vue

<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    从vuex中获取的值: <label></label>
    <br>
    <button>值 + 1</button>
  </div>
</template>

<script>
export default {
  name: 'Son1Com'
}
</script>

<style lang="css" scoped>
.box{
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

Son2.vue

<template>
  <div class="box">
    <h2>Son2 子组件</h2>
    从vuex中获取的值:<label></label>
    <br />
    <button>值 - 1</button>
  </div>
</template>

<script>
export default {
  name: 'Son2Com'
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

三、vuex 的使用 - 创建空仓库

多组件共享同一份数据,肯定要有一个位置存放通用的数据,将来任何组件只用找这个位置就可以拿到通用的数据了,所以需要先初始化一个仓库。

1.安装 vuex

安装vuex与vue-router类似,vuex是一个独立存在的插件,如果脚手架初始化没有选 vuex,就需要额外安装。

yarn add vuex@3 或者 npm i vuex@3

2.新建 store/index.js 专门存放 vuex相关的内容

为了维护项目目录的整洁,在src目录下新建一个store目录其下放置一个index.js文件。 (和 router/index.js 类似)

3.创建仓库 store/index.js

// 导入 vue
import Vue from 'vue'
// 导入 vuex
import Vuex from 'vuex'
// vuex也是vue的插件, 需要use一下, 进行插件的安装初始化
Vue.use(Vuex)

// 创建仓库 store
const store = new Vuex.Store()

// 导出仓库
export default store

4 在 main.js 中导入挂载到 Vue 实例上

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

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  store
}).$mount('#app')

此刻起, 就成功创建了一个 空仓库!!

5.测试打印Vuex

在组件中访问仓库,打印一下Vuex的仓库

App.vue

created(){
  console.log(this.$store)
}

四、核心概念 - state 状态

1.目标

如何给仓库 提供 数据,如何 使用 仓库中的数据

2.如何提供数据

打开项目中的store.js文件,在创建Vuex.store仓库对象的时候,往里面添加一个叫state的对象,然后在state对象中添加要共享的数据。

// 创建仓库 store
const store = new Vuex.Store({
  state: {
    count: 101
  }
})

3.访问Vuex仓库中的数据

问题: 如何在组件中获取count?

  1. 通过$store直接访问 —> {{ $store.state.count }}

  2. 通过辅助函数mapState 映射计算属性 —> {{ count }}

4.通过$store访问的语法

都是先获取到仓库实例,再获取到里面的state对象,再拿到对应数据

获取 store:
 1.Vue模板中获取: this.$store
 2.js文件中获取: import 导入 store


在模板中:     {{ $store.state.xxx }}
在组件逻辑中:  this.$store.state.xxx
在JS模块中:   store.state.xxx

5.代码实现

5.1模板语法中使用

组件中可以使用 $store 获取到vuex中的store实例,然后通过state属性访问count属性, 如下

<h1>state的数据 - {{ $store.state.count }}</h1>
5.2组件逻辑中使用

将state属性定义在计算属性中 State | Vuex

<h1>state的数据 - {{ count }}</h1>

// 把state中数据,定义在组件内的计算属性中
  computed: {
    count () {
      return this.$store.state.count
    }
  }
5.3 js文件中使用
//main.js

import store from "@/store"

console.log(store.state.count)

每次都像这样一个个的提供计算属性, 太麻烦了,我们有没有简单的语法帮我们获取state中的值呢?

五、通过辅助函数 - mapState获取 state中的数据

mapState是辅助函数,可以把store中指定的数据映射到 组件的计算属性中, 然后就可以使用计算属性的访问方式,直接访问仓库中的数据了

用法 :

1.第一步:在组件中导入mapState辅助函数 (mapState是vuex中的一个函数)

import { mapState } from 'vuex'

2.第二步:采用数组形式引入state中的属性

mapState(['count']) 

上面代码的最终得到的是 类似于

computed:{
    count () {
        return this.$store.state.count
    }
}

3.第三步:利用展开运算符将导出的状态映射给计算属性

  computed: {
    ...mapState(['count'])
  }

直接通过计算属性的属性名访问: 

 <div> state的数据:{{ count }}</div>

六、开启严格模式及Vuex的单项数据流

1.目标

明确 vuex 同样遵循单向数据流,组件中不能直接修改仓库的数据

2.直接在组件中修改Vuex中state的值

Son1.vue

错误示例:

button @click="handleAdd">值 + 1</button>


methods:{
	 handleAdd (n) {
      // 错误代码(vue默认不会监测,监测需要成本)
       this.$store.state.count++
      // console.log(this.$store.state.count) 
    },
}

3.开启严格模式

通过 strict: true 可以开启严格模式,开启严格模式后,直接修改state中的值会报错 (有利于初学者,检测不规范的代码 => 上线时需要关闭)

state数据的修改只能通过mutations,并且mutations必须是同步的

七、核心概念-mutations

mutations的操作流程,来修改state数据(state数据的修改只能通过mutations)

1.定义mutations

在创建Vuex.store实例的时候往里面添加一个mutations的对象,然后可以在它里面定义一系列用于修改仓库中数据的方法

const store  = new Vuex.Store({
  state: {
    count: 0
  },
  // 定义mutations
  mutations: {
     
  }
})

2.格式说明

mutations是一个对象,对象中存放修改state的方法

mutations: {
    // 所有的mutations方法的 第一个参数都是当前store仓库的state属性
    // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
    addCount (state) {
      state.count += 1
    }
  },

3.在组件中提交调用 mutations

在this.$store.commit()方法中写mutations中的函数名,然后就会进行调用。

    handleAdd () {
      this.$store.commit('addCount')
    }

八、带参数的 mutations函数

1.目标:

掌握 mutations 传参语法

2.语法

看下面这个案例,每次点击不同的按钮,加的值都不同,每次都要定义不同的mutations处理吗?

提交 mutation 是可以传递参数的 this.$store.commit('xxx', 参数)

2.1 提供带参数的mutation函数
//所有mutation函数,第一个参数,都是 state
mutations: {
  addCount (state, count) {
    state.count = count
  }
},
2.2 在组件中提交调用mutation函数
handle ( ) {
  this.$store.commit('addCount', 10)
}

小tips: 提交的参数只能是一个, 如果有多个参数要传, 可以传递一个对象

store/index.js中的mutations函数

mutations: {
    addCount (state, obj) {
      console.log(obj)
      // 修改数据
      state.count += obj.count
    }
  }

在组件中提交调用mutations函数 

      this.$store.commit('addCount', {
        count: n,
        msg: '哈哈'
      })

九、练习-mutations函数的减法功能

1.步骤

2.代码实现

Son2.vue

    <button @click="subCount(1)">值 - 1</button>
    <button @click="subCount(5)">值 - 5</button>
    <button @click="subCount(10)">值 - 10</button>

    export default {
        methods:{
             subCount (n) { 
                this.$store.commit('addCount', n)
        },
        }
    }

store/index.js

mutations:{
    subCount (state, n) {
      state.count -= n
    },
}

十、练习-Vuex中的值和组件中的input双向绑定

1.目标

实时输入,实时更新,巩固 mutations 传参语法

2.实现步骤

3.代码实现

App.vue

<input :value="count" @input="handleInput" type="text">

export default {
  methods: {
    handleInput (e) {
      // 1. 实时获取输入框的值
      const num = +e.target.value
      // 2. 提交mutation,调用mutation函数
      this.$store.commit('changeCount', num)
    }
  }
}

store/index.js

mutations: { 
   changeCount (state, newCount) {
      state.count = newCount
   }
},

十一、mapMutations辅助函数

mapMutations和mapState很像,它把位于mutations中的方法提取出来,映射到组件的methods配置项中。

实例:

在store/index.js中有如下mutations方法:

mutations: {
    subCount (state, n) {
      state.count -= n
    }
}

在组件中就可以使用mapMutations辅助函数向methods中映射某个mutations方法:

import  { mapMutations } from 'vuex'
methods: {
    ...mapMutations(['subCount'])
}

上面代码的含义是将mutations的方法导入了methods中,等价于给组件的methods中添加了一个方法

methods: {
      // commit(方法名, 载荷参数)
      subCount(n) {
          this.$store.commit('subCount',n)
      }
 }

此时,在组件中就可以直接通过this.subCount调用了:

this.subCount(10)

但是请注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中

十二、核心概念 - actions

state是存放数据的,mutations是同步更新数据的 (便于监测数据的变化, 更新视图等, 方便于调试工具查看变化),actions则负责处理异步更新state中的数据

说明:mutations必须是同步的

需求: 比如一秒钟之后, 要给一个数 去修改state

1.定义actions

在仓库实例中添加actions对象

// 这里面存放的就是 vuex 相关的核心代码
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import setting from './modules/setting'

// 插件安装
Vue.use(Vuex)

// 创建仓库
const store = new Vuex.Store({
  // 严格模式 (有利于初学者,检测不规范的代码 => 上线时需要关闭)
  strict: true,
  // 1. 通过 state 可以提供数据 (所有组件共享的数据)
  state: {
    title: '仓库大标题',
    count: 100,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  },

  mutations: {
    changeCount (state, newCount) {
      state.count = newCount
    }
  },

  // 3. actions 处理异步
  // 注意:不能直接操作 state,操作 state,还是需要 commit mutation
  actions: {
    // context 上下文 (此处未分模块,可以当成store仓库)
    // context.commit('mutation名字', 传递的参数)
    changeCountAction (context, num) {
      // 这里是setTimeout模拟异步,以后大部分场景是发请求
      setTimeout(() => {
        context.commit('changeCount', num)
      }, 1000)
    }
  }
})

// 导出给main.js使用
export default store

2.组件中定义一个函数,通过dispatch去调用actions中的方法

第一个形参是actions中的方法名,第二个是传递的参数(定义的函数也是在methods配置项中的)

setAsyncCount () {
  this.$store.dispatch('setAsyncCount', 666)
}

十三、辅助函数 -mapActions

1.目标:掌握辅助函数 mapActions,映射方法

mapActions 是把位于 actions中的方法提取了出来,映射到组件methods中

假设actions中已经有了一个方法,如何将其映射到组件中:

  actions: {
    changeCountAction (context, num) {
      setTimeout(() => {
        context.commit('changeCount', num)
      }, 1000)
    }
  }

Son2.vue

import { mapActions } from 'vuex'
methods: {
   ...mapActions(['changeCountAction'])
}

等价于向methods配置项中新增了这个方法:

methods: {
  changeCountAction (n) {
    this.$store.dispatch('changeCountAction', n)
  }
}

然后就可以直接调用了:

<button @click="changeCountAction(200)">+异步</button>

十四、核心概念 - getters

getters函数用于从state中筛选出一些符合条件的数据,这些数据是依赖state的,此时会用到getters

例如,state中定义了叫list,list为1-10的数组:

state: {
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

在组件中,需要显示所有大于5的数据,getters可以帮助我们实现它

1.定义getters

在参数实例中添加getters对象,可以在里面定义一些getters函数

  //getters 类似于计算属性
  getters: {
    // 注意点:
    // 1. 形参第一个参数,就是state,在getters函数中可以使用这个state拿到数据
    // 2. 必须有返回值,返回值就是getters的值
    filterList (state) {
      return state.list.filter(item => item > 5)
    }
  }

2.使用getters

2.1原始方式-$store
<div>{{ $store.getters.filterList }}</div>
2.2辅助函数 - mapGetters

mapMutations和mapActions都是在映射方法,而mapState和mapGetters都是在映射属性,所以映射属性需要写在computed配置项中

computed: {
    ...mapGetters(['filterList'])
}

后面直接使用即可:

 <div>{{ filterList }}</div>

十五、使用小结

十六、Vuex分模块 - module

1.目标

掌握核心概念 module 模块的创建

2.问题

由于vuex使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

这句话的意思是,如果把所有的状态都放在state中,当项目变得越来越大的时候,Vuex会变得越来越难以维护

由此,又有了Vuex的模块化

3.模块定义 - 准备 state

先在store目录下新建一个modules文件夹,然后在modules下新建模块文件,每个modules下的每个js文件就是一个模块,并且每个模块都有自己对应的vuex的核心概念。

定义两个模块 usersetting

user中管理用户的信息状态 userInfo modules/user.js

const state = {
  userInfo: {
    name: 'zs',
    age: 18
  }
}

const mutations = {}

const actions = {}

const getters = {}

export default {
  state,
  mutations,
  actions,
  getters
}

setting中管理项目应用的 主题色 theme,描述 desc, modules/setting.js

const state = {
  theme: 'dark'
  desc: '描述真呀真不错'
}

const mutations = {}

const actions = {}

const getters = {}

export default {
  state,
  mutations,
  actions,
  getters
}

然后在store/index.js文件中的modules配置项中,挂载这两个模块到store

import user from './modules/user'
import setting from './modules/setting'

const store = new Vuex.Store({
    modules:{
        user,
        setting
    }
})

使用模块中的数据, 可以直接通过模块名访问 $store.state.模块名.xxx => $store.state.setting.desc也可以通过 mapState 映射

十七、获取模块内的state数据

1.目标:

掌握模块中 state 的访问语法

尽管已经分模块了,但子模块的state,还是会挂到根级别的 state 中,属性名就是模块名(其实就是一个object)

2.访问模块中的数据

  1. 直接通过模块名访问 $store.state.模块名.xxx

  2. 通过 mapState 映射:

    1. 默认根级别的映射 mapState([ 'xxx' ])

    2. 子模块的映射 :mapState('模块名', ['xxx']) - 需要开启命名空间 namespaced:true

案例:

有user模块:modules/user.js

const state = {
  userInfo: {
    name: 'zs',
    age: 18
  },
  myMsg: '我的数据'
}

const mutations = {
  updateMsg (state, msg) {
    state.myMsg = msg
  }
}

const actions = {}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

在Son1.vue中:

    <!-- 测试访问user模块中的state - 原生 -->
    <div>{{ $store.state.user.userInfo.name }}</div>

在Son2.vue中,mapState辅助函数访问:

<template>
  <div class="box">
    <!-- 访问模块中的state -->
    <div>user模块的数据:{{ userInfo }}</div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'Son2Com',
  computed: {
    ...mapState('user', ['userInfo'])
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

需要在store/mudoles/user.js中开启命名空间 namespaced:true

// user模块
const state = {
  userInfo: {
    name: 'zs',
    age: 18
  },
  score: 80
}
const mutations = {
  setUser (state, newUserInfo) {
    state.userInfo = newUserInfo
  }
}
const actions = {
  setUserSecond (context, newUserInfo) {
    // 将异步在action中进行封装
    setTimeout(() => {
      // 调用mutation   context上下文,默认提交的就是自己模块的action和mutation
      context.commit('setUser', newUserInfo)
    }, 1000)
  }
}
const getters = {
  // 分模块后,state指代子模块的state
  UpperCaseName (state) {
    return state.userInfo.name.toUpperCase()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

十八、获取模块内的getters数据

1.目标:

掌握模块中 getters 的访问语

2.语法:

使用模块中 getters 中的数据:

  1. 直接通过模块名访问$store.getters['模块名/xxx ']

  2. 通过 mapGetters 映射

    1. 默认根级别的映射 mapGetters([ 'xxx' ])

    2. 子模块的映射 mapGetters('模块名', ['xxx']) - 需要开启命名空间

3.代码演示

modules/user.js

// user模块
const state = {
  userInfo: {
    name: 'zs',
    age: 18
  },
  score: 80
}
const mutations = {
  }
}

const actions = {
}

const getters = {
  // 分模块后,这个state指代子模块的state
  UpperCaseName (state) {
    return state.userInfo.name.toUpperCase()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

在Son1.vue 直接访问getters

<!-- 测试访问模块中的getters - 原生 -->
<div>{{ $store.getters['user/UpperCaseName'] }}</div>

在Son2.vue 通过命名空间访问

<template>
  <div class="box">
    <!-- 访问模块中的getters -->
    <div>{{ UpperCaseName }}</div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'Son2Com',
  computed: {
    ...mapGetters('user', ['UpperCaseName'])
  },
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

十九、调用模块内的mutations方法

1.目标:

掌握分模块中的 mutation 的调用语法

2.注意:

默认模块中的 mutation 和 actions 会被挂载到全局,需要开启命名空间,才会挂载到子模块。

3.调用方式:

  1. 直接通过 store 调用 $store.commit('模块名/xxx ', 额外参数)

  2. 通过 mapMutations 映射

    1. 默认根级别的映射 mapMutations([ 'xxx' ])

    2. 子模块的映射 mapMutations('模块名', ['xxx']) - 需要开启命名空间

4.代码实现

modules/user.js

// user模块
const state = {
  userInfo: {
    name: 'zs',
    age: 18
  },
  score: 80
}
const mutations = {
  setUser (state, newUserInfo) {
    state.userInfo = newUserInfo
  }
}

const actions = {
}

const getters = {
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

modules/setting.js

// setting模块
const state = {
  theme: 'light', // 主题色
  desc: '测试demo'
}
const mutations = {
  setTheme (state, newTheme) {
    state.theme = newTheme
  }
}
const actions = {}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

Son1.vue

<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    <hr>
    <!-- 测试访问user模块中的state - 原生 -->
    <div>{{ $store.state.user.userInfo.name }}</div>
    <button @click="updateUser">更新个人信息</button>

    <div>{{ $store.state.setting.theme }}</div>
    <button @click="updateTheme">更新主题色</button>
  </div>
</template>

<script>
export default {
  name: 'Son1Com',
  methods: {
    updateUser () {
      // $store.commit('模块名/mutation名', 额外传参)
      this.$store.commit('user/setUser', {
        name: 'xiaowang',
        age: 25
      })
    },
    updateTheme () {
      this.$store.commit('setting/setTheme', 'pink')
    }
  }
}
</script>

<style lang="css" scoped>
.box{
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

Son2.vue

<template>
  <div class="box">
    <!-- 访问模块中的state -->
    <div>user模块的数据:{{ userInfo }}</div>
    <button @click="setUser({ name: 'xiaoli', age: 80 })">更新个人信息</button>

    <div>setting模块的数据:{{ theme }} - {{ desc }}</div>
    <button @click="setTheme('skyblue')">更新主题</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'Son2Com',
  computed: {
    // mapState 和 mapGetters 都是映射属性
    ...mapState(['count', 'user', 'setting']),
    ...mapState('user', ['userInfo']),
    ...mapState('setting', ['theme', 'desc']),
    ...mapGetters(['filterList']),
    ...mapGetters('user', ['UpperCaseName'])
  },
  methods: {
    // mapMutations 和 mapActions 都是映射方法
    // 全局级别的映射
    ...mapMutations(['subCount', 'changeTitle']),
    ...mapActions(['changeCountAction']),

    // 分模块的映射
    ...mapMutations('setting', ['setTheme']),
    ...mapMutations('user', ['setUser']),
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

二十、获取模块内的actions方法

1.目标:

掌握模块中 action 的调用语法 (同理 - 直接类比 mutation 即可)

2.注意:

默认模块中的 mutation 和 actions 会被挂载到全局,需要开启命名空间,才会挂载到子模块。

3.调用语法:

  1. 直接通过 store 调用 $store.dispatch('模块名/xxx ', 额外参数)

  2. 通过 mapActions 映射

    1. 默认根级别的映射 mapActions([ 'xxx' ])

    2. 子模块的映射 mapActions('模块名', ['xxx']) - 需要开启命名空间

4.代码实现

需求:

modules/user.js

// user模块
const state = {
  userInfo: {
    name: 'zs',
    age: 18
  },
  score: 80
}
const mutations = {
  setUser (state, newUserInfo) {
    state.userInfo = newUserInfo
  }
}
const actions = {
  setUserSecond (context, newUserInfo) {
    // 将异步在action中进行封装
    setTimeout(() => {
      // 调用mutation   context上下文,默认提交的就是自己模块的action和mutation
      context.commit('setUser', newUserInfo)
    }, 1000)
  }
}
const getters = {
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

Son1.vue 直接通过store调用

<template>
  <div class="box">
    <button @click="updateUser2">一秒后更新信息</button>
  </div>
</template>

<script>
export default {
  name: 'Son1Com',
  methods: {
    updateUser2 () {
      // 调用action dispatch
      this.$store.dispatch('user/setUserSecond', {
        name: 'xiaohong',
        age: 28
      })
    },
  }
}
</script>

<style lang="css" scoped>
.box{
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

Son2.vue mapActions映射

<template>
  <div class="box">
    <h2>Son2 子组件</h2>
    <button @click="setUserSecond({ name: 'xiaoli', age: 80 })">一秒后更新信息</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'Son2Com',
  methods: {
    // mapMutations 和 mapActions 都是映射方法
    // 全局级别的映射
    ...mapMutations(['subCount', 'changeTitle']),
    ...mapActions(['changeCountAction']),

    // 分模块的映射
    ...mapActions('user', ['setUserSecond'])
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2296580.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Godot开发框架探索#2

前言 距离上次发文又又又隔了很长一段时间。主要原因还是因为思绪在徘徊&#xff0c;最近纠结的点有以下几个&#xff1a;1.渴求一个稳定的Godot开发框架&#xff1b;2.要不要使用更轻量的开发框架&#xff0c;或者直接写引擎&#xff1b; 3.对自己想做的游戏品类拿不定主意。…

deepseek实现私有知识库

前言 之前写了如何本地部署deepseek&#xff0c;已经可以私有化问答了&#xff0c;本地搭建deepseek实操&#xff08;ollama搭建&#xff0c;docker管理&#xff0c;open-webui使用&#xff09; 其中我觉得最厉害的还是这个模型蒸馏&#xff0c;使我们可以用很低的代价使用大模…

【探索未来科技】2025年国际学术会议前瞻

【探索未来科技】2025年国际学术会议前瞻 【探索未来科技】2025年国际学术会议前瞻 文章目录 【探索未来科技】2025年国际学术会议前瞻前言1. 第四届电子信息工程、大数据与计算机技术国际学术会议&#xff08; EIBDCT 2025&#xff09;代码示例&#xff1a;机器学习中的线性回…

大模型基本原理(四)——如何武装ChatGPT

传统的LLM存在几个短板&#xff1a;编造事实、计算不准确、数据过时等&#xff0c;为了应对这几个问题&#xff0c;可以借助一些外部工具或数据把AI武装起来。 实现这一思路的框架包括RAG、PAL、ReAct。 1、RAG&#xff08;检索增强生成&#xff09; LLM生成的内容会受到训练…

开发完的小程序如何分包

好几次了&#xff0c;终于想起来写个笔记记一下 我最开始并不会给小程序分包&#xff0c;然后我就各种搜&#xff0c;发现讲的基本上都是开发之前的小程序分包&#xff0c;可是我都开发完要发布了&#xff0c;提示我说主包太大需要分包&#xff0c;所以我就不会了。。。 好了…

java配置api,vue网页调用api从oracle数据库读取数据

一、主入口文件 1&#xff1a;java后端端口号 2&#xff1a;数据库类型 和 数据库所在服务器ip地址 3&#xff1a;服务器用户名和密码 二、映射数据库表中的数据 resources/mapper/.xml文件 1&#xff1a;column后变量名是数据库中存储的变量名 property的值是column值的…

iOS三方登录 - Facebook登录

引言 在出海APP的开发中&#xff0c;集成主流社交平台的三方登录已成为必不可少的一环。Facebook 作为全球最大的社交网络平台之一&#xff0c;其提供的 Facebook 登录功能能够大大简化用户注册和登录流程&#xff0c;提高用户体验&#xff0c;减少流失率。对于开发者而言&…

机器学习 - 理解偏差-方差分解

为了避免过拟合&#xff0c;我们经常会在模型的拟合能力和复杂度之间进行权衡。拟合能力强的模型一般复杂度会比较高&#xff0c;容易导致过拟合。相反&#xff0c;如果限制模型的复杂度&#xff0c;降低其拟合能力&#xff0c;又可能会导致欠拟合。因此&#xff0c;如何在模型…

深度学习01 神经网络

目录 神经网络 ​感知器 感知器的定义 感知器的数学表达 感知器的局限性 多层感知器&#xff08;MLP, Multi-Layer Perceptron&#xff09; 多层感知器的定义 多层感知器的结构 多层感知器的优势 偏置 偏置的作用 偏置的数学表达 神经网络的构造 ​神经网络的基本…

自动化xpath定位元素(附几款浏览器xpath插件)

在 Web 自动化测试、数据采集、前端调试中&#xff0c;XPath 仍然是不可或缺的技能。虽然 CSS 选择器越来越强大&#xff0c;但面对复杂 DOM 结构时&#xff0c;XPath 仍然更具灵活性。因此&#xff0c;掌握 XPath&#xff0c;不仅能提高自动化测试的稳定性&#xff0c;还能在爬…

PromptSource官方文档翻译

目录 核心概念解析 提示模板&#xff08;Prompt Template&#xff09; P3数据集 安装指南 基础安装&#xff08;仅使用提示&#xff09; 开发环境安装&#xff08;需创建提示&#xff09; API使用详解 基本用法 子数据集处理 批量操作 提示创建流程 Web界面操作 手…

2025年软件测试五大趋势:AI、API安全、云测试等前沿实践

随着软件开发的不断进步&#xff0c;测试方法也在演变。企业需要紧跟新兴趋势&#xff0c;以提升软件质量、提高测试效率&#xff0c;并确保安全性&#xff0c;在竞争激烈的技术环境中保持领先地位。本文将深入探讨2025年最值得关注的五大软件测试趋势。 Parasoft下载https://…

js的DOM一遍过

一、获取元素 1.根据id获取 document.getElementById(id);2.根据标签名获取 使用 getElementsByTagName() 方法可以返回带有指定标签名的对象的集合。 document.getElementsByTagName(标签名);获取某个元素(父元素)内部所有指定标签名的子元素。 element.getElementsByTag…

Machine Learning:Introduction

文章目录 Machine LearningTrainingStep 1.Contract Function with Unknown ParametersStep 2.Define Loss from Training DataStep 3.Optimization Linear ModelPiecewise Linear CurveBeyond Piecewise Liner?FunctionLossOptimization Model Deformation Machine Learning …

Excel 笔记

实际问题记录 VBA脚本实现特殊的行转列 已知&#xff1a;位于同一Excel工作簿文件中的两个工作表&#xff1a;Sheet1、Sheet2。 问题&#xff1a;现要将Sheet2中的每一行&#xff0c;按Sheet1中的样子进行转置&#xff1a; Sheet2中每一行的黄色单元格&#xff0c;为列头。…

基于 GEE 利用插值方法填补缺失影像

目录 1 完整代码 2 运行结果 利用GEE合成NDVI时&#xff0c;如果研究区较大&#xff0c;一个月的影像覆盖不了整个研究区&#xff0c;就会有缺失的地方&#xff0c;还有就是去云之后&#xff0c;有云量的地区变成空值。 所以今天来用一种插值的方法来填补缺失的影像&#xf…

如何在浏览器中搭建开源Web操作系统Puter的本地与远程环境

文章目录 前言1.关于Puter2.本地部署Puter3.Puter简单使用4. 安装内网穿透5.配置puter公网地址6. 配置固定公网地址 前言 嘿&#xff0c;小伙伴们&#xff01;是不是每次开机都要像打地鼠一样不停地点击各种网盘和应用程序的登录按钮&#xff0c;感觉超级麻烦&#xff1f;更让…

使用EVE-NG-锐捷实现单臂路由

一、基础知识 1.三层vlan vlan在三层环境中通常用作网关vlan配上ip网关内部接口ip 2.vlan创建步骤 创建vlan将接口划分到不同的vlan给vlan配置ip地址 二、项目案例 1、项目拓扑 2、项目实现 PC1配置 配置PC1IP地址为192.168.1.10/24网关地址为192.168.1.1 ip 192.168.1…

二、通义灵码插件保姆级教学-IDEA(使用篇)

一、IntelliJ IDEA 中使用指南 1.1、代码解释 选择需要解释的代码 —> 右键 —> 通义灵码 —> 解释代码 解释代码很详细&#xff0c;感觉很强大有木有&#xff0c;关键还会生成流程图&#xff0c;对程序员理解业务非常有帮忙&#xff0c;基本能做到哪里不懂点哪里。…

HAL库外设宝典:基于CubeMX的STM32开发手册(持续更新)

目录 前言 GPIO&#xff08;通用输入输出引脚&#xff09; 推挽输出模式 浮空输入和上拉输入模式 GPIO其他模式以及内部电路原理 输出驱动器 输入驱动器 中断 外部中断&#xff08;EXTI&#xff09; 深入中断&#xff08;内部机制及原理&#xff09; 外部中断/事件控…