vuex取值的两种方法

1.使用 mapState

<template>
	<view>
		<view>
			主页{{username}}
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex' //引入mapState
	export default {
		data() {
			return {}
		},
		onLoad() {
			this.$A.checkToken()
		},
		computed: mapState([
			// 从state中拿到数据 箭头函数可使代码更简练
			'username',
			'age',
			'count',
			"hasLogin"
		]),
		methods: {}
	}
</script>

2.使用 store.state.username

<template>
	<view>
		<view>欢迎您:
			<text style="color: #007AFF;">{{username}}</text>
		</view>
		
	</view>
</template>

<script>
	import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {

			}
		},
		computed: {
			username() {
				return store.state.username
			},
			hasLogin() {
				return this.$store.state.hasLogin
			}
		},
		onLoad() {
},
		methods: {
}
	}
</script>

<style>

</style>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注