这些内容是博主在学习过程中记录下来的,有一些不重要的点就跳过了,需要时自行查询文档,4.0
对比3.0
,区别不大。
VueX4.0官网
语法有所调整
import { onMounted } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
onMounted(() => {
console.log(Store.state.author)
})
}
}
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
return {
count: computed(() => Store.state.author)
}
}
}
语法有所调整
import { onMounted } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
onMounted(() => {
console.log(Store.getters.getCookie)
console.log(Store.getters.getToken('Param'))
})
}
}
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
return {
count1: computed(() => Store.getters.getCookie),
count2: computed(() => Store.getters.getToken('param'))
}
}
}
语法有所调整
<p @click="set">{{ cookie }}p>
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
const set = function() {
Store.commit('setCookie', 'param')
}
return {
set,
cookie: computed(() => Store.state.cookie)
}
}
}
<p @click="set">{{ cookie }}p>
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
return {
cookie: computed(() => Store.state.cookie),
set: () => Store.commit('setCookie', 'param')
}
}
}
语法有所调整
<p @click="set">{{ cookie }}p>
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
const set = function() {
Store.dispatch('set', 'param')
}
return {
cookie: computed(() => Store.state.cookie),
set
}
}
}
<p @click="set">{{ cookie }}p>
import { computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup() {
const Store = useStore()
return {
cookie: computed(() => Store.state.cookie),
set: () => Store.dispatch('set', 'param')
}
}
}
后记:Modules
就不做赘述了,具体用法可以参考【超详细!Vuex手把手教程】
如果看了觉得有帮助的,我是@鹏多多i,欢迎 点赞 关注 评论;END
PS:在本页按F12,在console中输入document.querySelectorAll('.tool-item-href')[0].click(),有惊喜哦~
公众号
往期文章
个人主页