4

I have a Lang object created from lang.min.js from:

https://github.com/rmariuzzo/Lang.js

in my Laravel app I have the following in app.js:

import SearchToggle from './components/toggle.vue'
import Lang from './lang.min.js'
...
Vue.component('toggle', Toggle);
...
var lang = new Lang();
lang.setMessages(require('./messages.js'));
console.log(lang.getLocale());
console.log(lang.get('main.specialties'));
...

the lang objects has no errors and manage to output the get as intended.

Now I have a vue component in the following directory which was imported as shown above: ressource/assets/js/components/toggle.vue

in the script section:

<script>
export default{
        data(){
            return {
                text_holder: lang.get('main.specialties'),

            }
        },
  ...
}

However this doesn't work. It complains that the lang is undefined. I think I'm missing the gap (concept) of javascript scope. I thought that if everything is in the app.js and got imported then the var lang would have been int he global scope and I would be allowed to use it anywhere across the the import (similar to how vendor is from php) I guess not.

Now what do I do? pull in an import on each vue component (but then I have to ensure the path going backward is correct and also multi loading the same object / message.js will bloat the javascript.

Would appreciate the JS help.

2
  • vuejs.org/v2/guide/state-management.html Commented Jan 10, 2017 at 3:58
  • @ceejayoz but the object from the lang.min.js is not a state - its a reference object that gets its message loaded into a json file format. so its like carrying around a configured static array. Commented Jan 10, 2017 at 4:02

1 Answer 1

3

Create your lang.js, and export the lang instance

var lang = new Lang();
lang.setMessages(require('./messages.js'));
...
export default lang;

then you can import and use that instance in your components

import lang from 'path/to/lang'

export default {
  data () {
    return {
      text_holder: lang.get('main.specialties'),
      ...
    }
  }
}

another way is to write plugins, you can inject lang to Vue, so you are able to access lang in your components.

Sign up to request clarification or add additional context in comments.

6 Comments

wouldn't importing it in each components bloat out the vue?
No, the import in javascript will only import the module once
so you saying that if I have 16 components and all of them import the same file javascript is actually importing once?
Yes, you can try it.
I tried your solution the export works but I get the following when using within a component: [Vue warn]: Error when evaluating expression "lang.get('main.specialties')": TypeError: Cannot read property 'get' of undefined (found in component: <toggle>)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.