web前端全系列 教程
1839个小节阅读:2244.2k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Vuex 使用单一状态树,用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 ”而存在。这也意味着,每个应用将仅仅包含一个 store 实例
xxxxxxxxxx
import { createStore } from 'vuex'
const store = createStore({
state:{
count:10
}
})
export default store
xxxxxxxxxx
<template>
<h3>Nums</h3>
<p>{{ $store.state.count }}</p>
<p>{{ count }}</p>
</template>
<script>
export default {
computed:{
count(){
return this.$store.state.count
}
}
}
</script>
xxxxxxxxxx
<template>
<h3>Count</h3>
<p>{{ $store.state.count }}</p>
<p>{{ count }}</p>
</template>
<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
const store = useStore()
const count = computed(() =>{
return store.state.count
})
</script>
mapState
辅助函数xxxxxxxxxx
<template>
<h3>Nums</h3>
<p>{{ $store.state.count }}</p>
<p>{{ count }}</p>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed:{
...mapState(["count"])
}
}
</script>
实时效果反馈
1. 下列代码,划横线处应该填写的代码是:
xxxxxxxxxx
<template>
<h3>Count</h3>
<p>{{ $store.state.count }}</p>
<p>{{ count }}</p>
</template>
<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
const store = ___
const count = computed(() =>{
return store.state.count
})
</script>
A vuex
B createStore
C store()
D useStore()
答案
1=>D