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

网站有标题外贸网站建设应该怎样选择语言

网站有标题,外贸网站建设应该怎样选择语言,怎么样做网站代理商,公司怎么做网站需要多少钱文章目录Vuex概述安装单向数据流Vuex核心概念StatemapState 辅助函数扩展运算符GettermapGetters 辅助函数Mutation提交载荷提交载荷对象对象风格提交使用常量替代mutation事件类型Action异步分发Module命名空间Vuex 概述 Vuex 是一个状态管理库,用于管理 Vue.js …

文章目录

  • Vuex
    • 概述
    • 安装
    • 单向数据流
    • Vuex核心概念
      • State
        • mapState 辅助函数
        • 扩展运算符
      • Getter
        • mapGetters 辅助函数
      • Mutation
        • 提交载荷
        • 提交载荷对象
        • 对象风格提交
        • 使用常量替代mutation事件类型
      • Action
        • 异步分发
      • Module
        • 命名空间

Vuex

概述

Vuex 是一个状态管理库,用于管理 Vue.js 应用程序中的共享状态。它可以帮助你在应用程序中保持数据的一致性和可预测性。

Vuex包括以下几个核心概念:

  • state:存储应用程序的状态数据。
  • getters:提供一种计算派生状态的方式,类似于Vue.js中的计算属性。
  • mutations:用于修改状态的方法,但是只能进行同步操作。
  • actions:用于提交mutations,可以进行异步操作。
  • modules:将store拆分为模块,每个模块都有自己的state、getters、mutations和actions。

Vuex 官方文档

安装

npm install vuex@next --save

单向数据流

<script >
export default {data() {return {count: 0}},methods: {increment() {this.count++;}}
}
</script><template><button @click="count++">{{ count }}</button>
</template>

在这里插入图片描述

这是一个单向数据流:

  • 状态:驱动应用的数据源。
  • 视图:以声明方式将状态映射到视图。
  • 操作:响应在视图上的操作导致的状态变化。

但是,当我们的应用遇到多个组件共享状态时,单向数据流的简洁性很容易被破坏:

  • 多个视图依赖于同一状态。
  • 来自不同视图的行为需要变更同一状态。

Vuex 可以帮助我们管理共享状态,并附带了更多的概念和框架。这需要对短期和长期效益进行权衡。

如果您不打算开发大型单页应用,使用 Vuex 可能是繁琐冗余的。确实是如此——如果您的应用够简单,您最好不要使用 Vuex。一个简单的 store 模式就足够您所需了。但是,如果您需要构建一个中大型单页应用,您很可能会考虑如何更好地在组件外部管理状态,Vuex 将会成为自然而然的选择。

Vuex核心概念

在这里插入图片描述

每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state)

Vuex 和单纯的全局对象有以下两点不同:

  • Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。
  • 你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化,从而让我们能够实现一些工具帮助我们更好地了解我们的应用。

State

用于存储应用程序的状态数据。

  • 在Vue组件中,通过 this.$store 访问store实例。
  • 通过$store.state获取状态对象。
  • Vuex 的状态存储是响应式的。

支持Vuex

import store from "./store";const app = createApp(App);
// 将 store 实例作为插件安装
app.use(store);
app.mount("#app");

定义store对象

import { createStore } from "vuex";const store = createStore({state() {return {count: 0,};}
});
export default store;

使用store

<template><p>count: {{ $store.state.count }}</p><!-- 不推荐 --><button @click="$store.state.count++">修改count</button>
</template>

mapState 辅助函数

使用mapState辅助函数可以简化代码,如:将{{ $store.state.count }}简化为{{ count }}

<script >
import { mapState } from 'vuex'export default {//mapState传对象computed: mapState({//简化代码://方式一;使用箭头函数// count: state => state.count,//方式二:字符串参数,等价于`state => state.count`count: "count"})
}
</script><template><p>count: {{ $store.state.count }}</p><p>{{ count }}</p><button @click="$store.state.count++">修改count</button>
</template>

当映射的计算属性的名称与 state 的子节点名称相同时,也可以给 mapState 传一个字符串数组:

<script >
import { mapState } from 'vuex'export default {//mapState传数组computed: mapState(["count", "msg"])
}
</script><template><p>count: {{ $store.state.count }}</p><p>{{ count }}</p><p>{{ msg }}</p><button @click="$store.state.count++">修改count</button>
</template>

扩展运算符

computed属性在Vue组件中只能有一个,可以使用对象扩展运算符兼容局部计算属性。

<script >
import { mapState } from 'vuex'export default {data() {return {num: 10}//对象扩展运算符computed: {addNum() {return this.num + 5;},...mapState(["count", "msg"])}
}
</script><template><p>count: {{ $store.state.count }}</p><p>{{ count }}</p><p>{{ msg }}</p><p>{{ addNum }}</p><button @click="$store.state.count++">修改count</button>
</template>

Getter

提供一种计算派生状态的方式,类似于Vue.js中的计算属性,例如对列表进行过滤并计数。

从 Vue 3.0 开始,getter的结果不再像计算属性一样会被缓存起来。

定义store对象

import { createStore } from "vuex";const store = createStore({state() {return {msg: "hello world",};},getters: {reverseMsg(state) {return state.msg.split("").reverse().join("");},reverseMsgLength(state, getters) {return getters.reverseMsg.length;},}
});
export default store;

在Vue中使用

<template><p>{{ $store.getters.reverseMsg }}</p><p>{{ $store.getters.reverseMsgLength }}</p>
</template>

mapGetters 辅助函数

可以通过mapGetters辅助函数将getter映射到计算属性中。

<script >
import { mapState, mapGetters } from 'vuex'export default {data() {return {num: 10}},computed: {addNum() {return this.num + 5;},...mapState(["count", "msg"]),...mapGetters(["reverseMsg", "reverseMsgLength"])}
}
</script><template><p>{{ $store.getters.reverseMsg }}</p><p>{{ $store.getters.reverseMsgLength }}</p><p>{{ reverseMsg }}</p><p>{{ reverseMsgLength }}</p>
</template>

Mutation

用于修改状态的方法,但是只能进行同步操作。

  • 使用$store.commit()方法触发mutation函数。

定义store对象

import { createStore } from "vuex";const store = createStore({state() {return {count: 0};},mutations: {//修改状态的方法increment(state) {      state.count++;},},
});
export default store;

在Vue中使用

<script >
export default {methods: {increment() {//使用`$store.commit`触发方法this.$store.commit("increment");}}
}
</script><template><p>count: {{ $store.state.count }}</p><button @click="increment">修改count</button>
</template>

提交载荷

你可以向 store.commit 传入额外的参数,即 mutation 的载荷(payload)

在store对象中定义

mutations: {add(state, num) {state.count += num;},
},

使用

<script >
export default {data() {return {num: 10}},methods: {add() {this.$store.commit("add", 5);}}
</script><template><p>count: {{ $store.state.count }}</p> <button @click="add">修改num</button>
</template>

提交载荷对象

在store对象中定义

mutations: {add2(state, payload) {state.count += payload.num;},
}

使用

<script >
export default {methods: {add2() {this.$store.commit("add2", { num: 10 });}}
}
</script><template><p>count: {{ $store.state.count }}</p><button @click="add2">修改num</button>
</template>

对象风格提交

this.$store.commit({type: "add",num: 20
})

使用常量替代mutation事件类型

定义mutation-type.js文件

export const ADD = "add";

在store对象中使用

import { createStore } from "vuex";
import { ADD } from "../mutation-type";const store = createStore({state() {return {count: 0};},mutations: {[ADD](state, num) {state.count += num;}},
});
export default store;

Action

用于提交mutations,可以进行异步操作。

  • 使用$store.dispatch()方法触发actions中定义的函数。

在store对象中定义

import { createStore } from "vuex";const store = createStore({state() {return {count: 0};},mutations: {increment(state) {state.count++;}},actions: {increment(context) {context.commit("increment");},},
});
export default store;

在Vue中使用

<script >
export default {methods: {increment2() {this.$store.dispatch("increment");}}
}
</script><template><p>count: {{ $store.state.count }}</p><button @click="increment2">修改num(actions)</button>
</template>

**使用参数解构简化代码 **

actions: {increment({ commit }) {commit("increment");},
},

异步分发

在store对象中定义

import { createStore } from "vuex";const store = createStore({state() {return {count: 0,};},mutations: {increment(state) {state.count++;}},actions: {incrementAsync({ commit }) {setTimeout(() => {commit("increment");}, 1000);},},
});
export default store;

在Vue中使用

<script >
export default {methods: {incrementAsync() {this.$store.dispatch("incrementAsync")}}
}
</script><template><p>count: {{ $store.state.count }}</p><button @click="incrementAsync">修改num(异步)</button>
</template>

Module

将store拆分为模块,每个模块都有自己的state、getters、mutations和actions。

新建user模块

const user = {state() {return {userName: "xiaoming",};},getters: {userNameAge(state, getters, rootState) {return state.userName + " 18岁";},},mutations: {updateUserName(state) {state.userName = "小明";},},
};
export default user;

添加子模块

import { createStore } from "vuex";
import user from "./user";const store = createStore({modules: {user,},
});
export default store;

访问user模块

<script >
export default {methods: {changeUserName() {console.log(this.$store);this.$store.commit("updateUserName");}}
}
</script><template><p>{{ $store.state.user.userName }}</p><button @click="changeUserName">修改userName</button><p>{{ $store.getters.userNameAge }}</p>
</template>

命名空间

如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。

新建Student的store对象

const student = {namespaced: true, //开启命名空间state() {return {userName: "xiaohei",};},getters: {userNameAge(state, getters, rootState) {return state.userName + " 8岁";},},mutations: {updateStudentName(state) {state.userName = "小黑";},},
};
export default student;

在Vue3中使用

<script >
export default {methods: {changeStudentName() {this.$store.commit("student/updateStudentName");}}
</script><template><h2>student模块</h2><p>{{ $store.state.student.userName }}</p><button @click="changeStudentName">修改studentName</button><p>{{ $store.getters["student/userNameAge"] }}</p>
</template>
http://www.yayakq.cn/news/789759/

相关文章:

  • 好大夫网站开发单位做策划 都上什么网站
  • 制作国外网站辽宁建设厅
  • 布吉做棋牌网站建设哪家技术好软件开发外包费用评估
  • 怎样做化妆品公司网站国内炫酷的网站设计
  • 现在建设校园网站用什么软件哪个网站做视频有钱挣
  • 有的网站打不开 但别人电脑能打开建设门户网站都需要什么意思
  • 德州制作网站哪家最专业皮革材料做网站
  • 做网站月薪资多少钱建设银行公积金网站
  • 网站建设费用摊销多少年阿里云怎么做淘客网站
  • 使用top域名做网站建设网站弹出后加载不进去
  • 做淘宝网站做网站开发的经营范围
  • 专业定制网站需要什么技能什么是网络社交平台
  • 网站建设最新教程视频找人做的网站怎么
  • 上海网站开发开发好的公司网站服务器配置要求
  • 服装网站建设配色设计一个个人求职网站
  • 毕设网站代做一般预算多少钱关于网站设计的职业
  • 数据线东莞网站建设技术支持多媒体网站建设
  • 五合一小程序网站免费软件电视剧最全
  • 怎么把做的网页放入网站wordpress更换域名文章不存在
  • 山东大型网站建设php数据库的网站模板
  • 溧阳建设局网站龙岗网站建设推广
  • 自建站平台能浏览的海外网站
  • 网站建设学生选课系统学软件工程专业后悔了
  • SEO网站建设全方位部署seo站内优化和站外优化
  • 潍坊仿站定制模板建站东营网站优化
  • 网站开发形成收入怎么做帐做问卷调查的网站有哪些
  • 网站建设的合同书竞价托管服务公司
  • 企业网站seo外包 s海南网纹瓜
  • 网站建设 资质外贸网站如何推广出去
  • 个人如何做网站推广简述企业网站的网络营销功能