vue3 在store的index.js

导入vuex,在store的index.js创建store对象

在main.js挂载store

import store from './store'

new Vue ({

/* 将store对象实例挂载到vue实例中
所有组件就可以直接从store中获取全局数据了*/
store,
render: h => h(App)
}).$mount('#app')

在store中的index.js进行声明state

/* 创建store对象并导出 */
export default new Vuex.Store({
  // 状态--数据
  state: {
    /* state 中存放的就是全局共享的数据 */
    count: 0
  }
})

使用state数据

方法1:直接获取$store.state.数据名称
  <div>
  <!-- 组件访问 State 中数据的第一种方式:
  this.$store.state.全局数据名称 -->
    <h3>当前最新的count值为:{{$store.state.count}}</h3>
    <button>+1</button>
  </div>

方法2:mapState辅助函数

  • 从 vuex 中按需导入 mapState 函数
  • 在计算属性节点computed下映射   ...mapState(['count'])

Mutation(修改state数据的唯一方法)

不可以直接操作 Store 中的数据,可以集中监控所有数据的变。

在组件内触发mutation

方法一 commit函数,可以传参
  • 无参:this.$store.commit('add', )
  • 有参:this.$store.commit('addN', 3)
第二种方式(mapMutations辅助函数,可以传参)
  • 先导入mapMutation函数,
  • 再在methods中使用 ...mapMutations(['sub', 'subN']),  
  • 在click绑定的事件直接调用sub和传参的sub(5)

Action(作用是操作mutations异步请求)

异步操作变更数据,必须通过 Action,不能使用 Mutation,但在 Action 中要通过触发 Mutation 的方式间接变更数据。


1.定义actions:


在action中,不能直接修改state中的数据,必须从context.commit()触发某个mutation才行

context是个对象

2.触发actions分发

方法一dispatch函数,可以传参

无参: this.$store.dispatch('addAsync')

有参:this.$store.dispatch('addNAsync', 5)

dispatch函数专门触发actions的方法。

方法二mapActions辅助函数,可以传参
先导入mapActions函数,
再在methods中使用 ...mapActions(['subASync', 'subNASync'])
在click绑定的事件直接调用subASync和传参的subNASync(9)

Getter (类似于计算属性,不改变state值只加工)

Getter 用于对 Store 中的数据进行加工处理形成新的数据。

① Getter 可以对 Store 中已有的数据加工处理之后形成新的数据,类似 Vue 的计算属性。

② Store 中数据发生变化,Getter 的数据也会跟着变化。

1.使用getters

方法一

直接使用this.$store.getters.名称 

方法二

mapGetters辅助函数

  • 从 vuex 中按需导入mapGetters函数
  • 在计算属性节点computed下映射 ...mapGetters(['showNum']

module(模块化)

把 Vuex 相关代码分割到模块中,将 store 分割成模块(module),每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块.

Vue3中,`router/index.js` 文件通常是用来初始化和配置路由器的地方。以下是创建这个文件的基本步骤: 1. **安装依赖**:首先,你需要安装 `vue-router` 和可能需要的其他插件,例如 `@vue/cli-plugin-router` 或者直接使用 `npm install vue-router` 或 `yarn add vue-router`。 2. **引入必要的包**:在 `index.js` 文件顶部,导入VueVue Router: ```javascript import Vue from &#39;vue&#39; import { createRouter, createWebHistory } from &#39;vue-router&#39; ``` 3. **创建路由实例**: ```javascript const routes = [ // 这里是一系列路由配置,例如: { path: &#39;/&#39;, component: () => import(&#39;./views/Home.vue&#39;), name: &#39;Home&#39;, meta: { requiresAuth: true } }, // ... 更多路由配置 ] ``` 4. **创建历史守卫** (如果需要): ```javascript const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes, scrollBehavior(to, from, savedPosition) { // 返回默认滚动位置或自定义滚动行为 } }) ``` 5. **全局导航守卫** (如需权限验证): ```javascript router.beforeEach((to, from, next) => { if (to.meta.requiresAuth && !store.state.user.authenticated) { // 跳转到登录页面 next(&#39;/login&#39;) } else { next() } }) ``` 6. **挂载到Vue实例上**: ```javascript Vue.use(router) ``` 7. **导出路由实例**: ```javascript export default router ``` 最后,要在App.vue或者其他合适的地方使用`<router-view>`标签来显示动态渲染的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值