Vuex
一、概述
二、State
存放状态的
1、创建state
const state = {
title: '我是一个标题',
content: '今天的天气是好的',
age: 2,
};
2、使用
-
直接在页面直接使用
//非模块化写法 <div>{ { $store.state.title }}</div> //模块化写法 <div>{ { $store.state.about.title }}</div>
-
通过计算属性获取
computed: { getTitle() { //非模块化写法 return this.$store.state.title //模块化写法:about是模块名 return this.$store.state.about.title } }
在页面使用
<div>{ { getTitle }}</div>
-
在计算属性中通过mapState辅助函数获取(推荐使用)
- 如果store不是模块化的:
computed: { ...mapState(['title', 'content', 'age']) }
- 如果store是模块化的:
computed: { ...mapState('about', [