vue component created没有触发_Vue 全局数据管理-Vuex

Vuex就是专为 Vue.js 应用程序开发的数据读写工具

参考官方文档

开始 | Vuex​vuex.vuejs.org
  1. state就是数据
  2. mutations就是对数据的改动
  3. commit就是调用mutations里的操作(以字符串的形式)

与vue不同它需要通过commit来触发事件

Vue.use(Vuex)会在Vue的原型上绑定一个共用属性,把store绑到Vue.prototype上

Vue.prototype.$store = store

8f3ea3ed2e9e7b061f28e241f797272a.png

从Vue组件里获取Vuex的状态,使用计算属性

a6ca5bd4ffa0640037f8a29c09e02d71.png

b8bfe6bb36557afa584934dd764c5c15.png

63e18c63ff70137e59c5de1c4684cafc.png

Vuex小结

在组件里使用它时,读的时候分为使用对象语法还是类语法,对象语法就使用computed,

this.$store.state.count

使用类语法(JS/TS)

get 

如果要写这个API的时候使用$store配合commit

this.$store.commit('xxx',payload)//payload是一个普通值或者对象

此时无法获取commit的返回值,如何去获取?

this.$store.commit('setTag')

然后在

const store = new Vuex.Store({
  state:{
     count:0;
     tag:undefined;
  },
mutations:{
  addCount(state){state.count+=1}
}

再去读

store.state.tag

注意在安装的时候

1、Vue.use(Vuex)

2、要把store传进去

new Vue({
    store,
})

在ts中使用mixins参考vue class component

Extend and Mixins | Vue Class Component​class-component.vuejs.org
// mixins.js
import Vue from 'vue'
import Component from 'vue-class-component'

// You can declare mixins as the same style as components.
@Component
export class Hello extends Vue {
  hello = 'Hello'
}

@Component
export class World extends Vue {
  world = 'World'
}

使用

import Component, { mixins } from 'vue-class-component'
import { Hello, World } from './mixins'

// Use `mixins` helper function instead of `Vue`.
// `mixins` can receive any number of arguments.
@Component
export class HelloWorld extends mixins(Hello, World) {
  created () {
    console.log(this.hello + ' ' + this.world + '!') // -> Hello World!
  }
}

在Labels.vue和Tags.vue里都使用过了createTag因此可以使用mixins

d6c0acba1a5c2be70472a06078de4520.png

331fcdbfa9a11ee88ac9c11dd2b19827.png

5a4e0422c95f8827b0e23d01ca7ab29e.png

65882b147e5a61a2bd0137df2524f8e7.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值