how to get access to vuex inside other js files like plugins in nuxt

a 夏天 提交于 2021-01-29 10:44:04

问题


i have a problem. i wanna get access to one of my getters inside my vee-validate.js file. how can i do that?

in my pages and components, (in <script>...</script> part, outside export default{...}) i used this function:

component.vue

<script>
let lang;
function getLang({store}) {
    lang = store.state.lang
}

export default{
   ...
}

but it is not working!

i'm trying to access my custom lang file (for translation purpose) that is stored in lang state in my vuex, and use it in vee-validate.js file for custom message.

i tried to import store but not working. veevalidate.js:

import Vue from 'vue'
import { required } from 'vee-validate/dist/rules'
import { extend, ValidationObserver, ValidationProvider, setInteractionMode } from 'vee-validate'

import {store} from '../store'
let langFile = store

setInteractionMode('eager')

extend('required', {
    ...required,
    message: ''
})

Vue.component('ValidationProvider', ValidationProvider);
Vue.component("ValidationObserver", ValidationObserver);

UPDATED: My store index.js

import langfile from '../static/lang'

export const state = () => ({
  lang: null,
  dir: null,
})

export const getters = {
  
  //----------------- Language and Direction
  lang(state){
    return state.lang
  },
  dir(state){
    return state.dir
  },
}

export const mutations = {
  SET_LANGUAGE(state, lang){
    state.lang = lang
  },
  SET_DIRECTION(state, dir){
    state.dir = dir
  },
}

export const actions = {
  
  async nuxtServerInit({dispatch, commit}) {
    // ------------- Read Language File
    let baseLang = process.env.SITE_LANGUAGE;
    let siteLang = langfile[baseLang];
    let siteDir = langfile[baseLang]['dir'];
    commit('SET_LANGUAGE', siteLang);
    commit('SET_DIRECTION', siteDir);
    
    
  },
}

来源:https://stackoverflow.com/questions/63847092/how-to-get-access-to-vuex-inside-other-js-files-like-plugins-in-nuxt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!