ts引入公共方法_Vuex的this.$store.commit和在Vue项目中引用公共方法

1、在Vue项目中引用公共方法

作为一个新人小白,在使用vue的过程中,难免会遇到很多的问题,比如某个方法在很多组件中都能用的上,如果在每个组件上都去引用一次的话,会比较麻烦,增加代码量。怎么做比较好呢,话不多说直接看代码把

首先 要在main.js中引入公共js。然后,将方法赋在Vue的原型链上。像图中这样。

然后在需要的组件上去引入这个方法

mouted (){

//调用方法

this.common.login();

}

/**然后公共方法里写一段简单的代码*/

export default{

login:function(){

console.log('这是一段代码')

}

}

2、Vuex中的this.$store.commit

众所周知,在vue的项目里父子组件间可以用props 或者 $emit 等方式 进行数据传递,而如果项目稍微大一点的话有很多平行组件,这个时候在这些组件间传递数据,使用这些方法会比较麻烦,代码也会变得不利于服用。

我们可以vuex来解决这个问题

vuex其实官网上不是太好理解,可以直接看看代码怎么实现的。

首先还是要先安装vuex

可以创建一个单独的store文件目录,里面专门存放相关的文件

然后新建index.js文件。

import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({

state: {

// 初始化全局的一个变量

testTxt: {"tips":"这是一条vuex的数据","id":1}

}

})

export default store

然后在main.js/main.ts 中注册store

import Vue from 'vue'

import App from './App'

import router from './router'

import store from './../store/index'

/* eslint-disable no-new */

new Vue({

el: '#app',

router,

store,

template: '',

components: { App }

})

接下来在组件中使用

export default {

...

data(){

value : "这又是修改后的value",

},

computed: {

getTxt() {

return this.$store.state.testTxt.tips;

}

},

methods: {

modifyTxt: function() {

this.$store.commit('modifyTips', this.value)

}

}

...

}

然后在index.js文件里可以修改

const store = new Vuex.Store({

state: {

// 初始化全局的一个变量

testTxt: {"tips":"这是一条vuex的数据","id":1}

},

mutations: {

modifyTips(state,msg) {

state.testTxt.tips= msg;

}

}

})

export default store

当然了,vuex的版本肯定是越新越好

引用提到了在Vue3 TypeScript使用this.$store会报错。这是因为在Vue3Vuex的类型定义发生了一些改变,this.$store不再被认为是一个有效的属性。因此,直接使用this.$store会导致Property '$store' does not exist on type的错误。 引用介绍了this.$store.commit()和this.$store.dispatch()方法的区别。两个方法都是用来传递值给Vuex的mutation来改变state。this.$store.commit()是同步操作,通过this.$store.commit('方法名', 值)来存储值,然后通过this.$store.state.方法名来获取值。而this.$store.dispatch()是异步操作,通过this.$store.dispatch('方法名', 值)来存储值,然后通过this.$store.getters.方法名来获取值。 引用提到了解决报错this.$store.commit is not a function的方法。这可能是因为在代码没有正确导入Vuex或者没有正确配置Vuexstore。可以参考该引用方法来解决这个问题。 综上所述,this.$storeVue3 TypeScript不再被视为有效的属性。可以使用this.$store.commit()和this.$store.dispatch()来实现对Vuex的mutation和action的调用,具体取值方式也有所不同。如果遇到报错this.$store.commit is not a function,可以参考引用方法来解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3+ts使用this.$store报错Property ‘$store‘ does not exist on type](https://blog.csdn.net/qq_31755699/article/details/124213551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vuexthis.$store.commit和this.$store.dispatch的用法](https://blog.csdn.net/weixin_44582045/article/details/121852631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vuex 解决报错this.$store.commit is not a function的方法](https://download.csdn.net/download/weixin_38743119/12948476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值