Using lodash in all of vue component template











up vote
31
down vote

favorite
11












Can I using lodash _ in all of my vue component?



for example:



I have components organized like below:



App.vue > Parent.vue > Child.vue



I would like to all of my component can access _ lodash without defined in every component vm data



===



I am also trying using Mixins. it works. but the result not expected like this _().isEmpty() instead of _.isEmpty()










share|improve this question




















  • 2




    It's a best practice to include any required modules within the component
    – Jeff
    Jun 8 '16 at 6:06










  • Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
    – Franz Skuffka
    Jun 8 '16 at 6:14












  • @Jeff is there any way to reusable it?
    – antoniputra
    Jun 8 '16 at 8:01










  • @FranzSkuffka I want use underscore in both of component template and logic.
    – antoniputra
    Jun 8 '16 at 8:01










  • I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
    – Zhang Buzz
    Jan 18 at 8:47















up vote
31
down vote

favorite
11












Can I using lodash _ in all of my vue component?



for example:



I have components organized like below:



App.vue > Parent.vue > Child.vue



I would like to all of my component can access _ lodash without defined in every component vm data



===



I am also trying using Mixins. it works. but the result not expected like this _().isEmpty() instead of _.isEmpty()










share|improve this question




















  • 2




    It's a best practice to include any required modules within the component
    – Jeff
    Jun 8 '16 at 6:06










  • Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
    – Franz Skuffka
    Jun 8 '16 at 6:14












  • @Jeff is there any way to reusable it?
    – antoniputra
    Jun 8 '16 at 8:01










  • @FranzSkuffka I want use underscore in both of component template and logic.
    – antoniputra
    Jun 8 '16 at 8:01










  • I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
    – Zhang Buzz
    Jan 18 at 8:47













up vote
31
down vote

favorite
11









up vote
31
down vote

favorite
11






11





Can I using lodash _ in all of my vue component?



for example:



I have components organized like below:



App.vue > Parent.vue > Child.vue



I would like to all of my component can access _ lodash without defined in every component vm data



===



I am also trying using Mixins. it works. but the result not expected like this _().isEmpty() instead of _.isEmpty()










share|improve this question















Can I using lodash _ in all of my vue component?



for example:



I have components organized like below:



App.vue > Parent.vue > Child.vue



I would like to all of my component can access _ lodash without defined in every component vm data



===



I am also trying using Mixins. it works. but the result not expected like this _().isEmpty() instead of _.isEmpty()







javascript lodash vue.js vue-component






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 8 '16 at 8:03

























asked Jun 8 '16 at 5:51









antoniputra

1,39841424




1,39841424








  • 2




    It's a best practice to include any required modules within the component
    – Jeff
    Jun 8 '16 at 6:06










  • Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
    – Franz Skuffka
    Jun 8 '16 at 6:14












  • @Jeff is there any way to reusable it?
    – antoniputra
    Jun 8 '16 at 8:01










  • @FranzSkuffka I want use underscore in both of component template and logic.
    – antoniputra
    Jun 8 '16 at 8:01










  • I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
    – Zhang Buzz
    Jan 18 at 8:47














  • 2




    It's a best practice to include any required modules within the component
    – Jeff
    Jun 8 '16 at 6:06










  • Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
    – Franz Skuffka
    Jun 8 '16 at 6:14












  • @Jeff is there any way to reusable it?
    – antoniputra
    Jun 8 '16 at 8:01










  • @FranzSkuffka I want use underscore in both of component template and logic.
    – antoniputra
    Jun 8 '16 at 8:01










  • I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
    – Zhang Buzz
    Jan 18 at 8:47








2




2




It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06




It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06












Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14






Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14














@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01




@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01












@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01




@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01












I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
– Zhang Buzz
Jan 18 at 8:47




I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write import lodash everywhere.
– Zhang Buzz
Jan 18 at 8:47












9 Answers
9






active

oldest

votes

















up vote
46
down vote



accepted










Some of the current answers may work in your scenario, but they have downsides:




  • Adding to the window object means your Vue project can't be server rendered, because servers don't have access to the window object.

  • Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.


An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this keyword.



import _ from 'lodash';    
Object.defineProperty(Vue.prototype, '$_', { value: _ });


Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:



export default {
created() {
console.log(this.$_.isEmpty(null));
}
}


The advantage of using Object.defineProperty rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.



This is more thoroughly explained in this blog post (which I wrote).



Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash"; at the top of the file requiring it.






share|improve this answer



















  • 8




    FYI - can write like Vue.prototype.$lodash = lodash
    – DogCoffee
    May 5 '17 at 9:18










  • Thank you! I aliased lodash with $_
    – pymarco
    Sep 6 '17 at 18:21










  • I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
    – user5814
    Mar 31 at 10:08






  • 2




    I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
    – anthonygore
    Apr 1 at 10:31


















up vote
18
down vote













You could import the lodash into each component:



<script>
import _ from 'lodash'

export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>





share|improve this answer





















  • the example of your code is only wrapping isEmpty method. how about the other lodash methods?
    – antoniputra
    Jun 8 '16 at 8:04










  • This is just an example based on yours. You could replace the isEmpty with any lodash method.
    – Pantelis Peslis
    Jun 8 '16 at 8:21








  • 2




    using import is the right way if you using lodash (_) in child component.
    – hendra1
    Feb 14 '17 at 9:38












  • I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
    – Zhang Buzz
    Jan 18 at 8:50


















up vote
12
down vote













For inline templates separated from the js module code it should work with:



  Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});


And then you can use it in template in "native" way - _.isEmpty(value).






share|improve this answer




























    up vote
    12
    down vote













    You could import lodash globally like this



    window._ = require('lodash');


    Once that has been imported, you will have access to _ from anywhere.






    share|improve this answer

















    • 3




      I downvoted because window is undefined when you server render the app.
      – anthonygore
      Apr 22 '17 at 7:04






    • 1




      Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
      – Zhang Buzz
      Jan 18 at 9:13




















    up vote
    7
    down vote













    A simple approach that worked for me:



    Vue.set(Vue.prototype, '_', _);



    This should allow you to use _ in all component templates and vue instances.






    share|improve this answer





















    • It doesn't work..
      – Rbex
      Jun 9 at 8:06










    • this solution just works for one of my projects, not in another one, don't know why... weird.
      – spetsnaz
      Jul 11 at 21:13


















    up vote
    3
    down vote













    You can use plugin/mixin like this.



    import _ from 'lodash';
    exports default {
    install : function(Vue, options){
    Vue.mixin({
    computed : {
    "_" : function(){
    return _;
    }
    }
    });
    }
    }





    share|improve this answer




























      up vote
      1
      down vote













      Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.



      I would note that in the tutorial, he left import "jquery" in his app.js file, which is not required. The plugin with import it automatically.






      share|improve this answer




























        up vote
        0
        down vote













        Check out vue-lodash!!
        It's a new wrapper for using lodash in vue.
        You can call it using




        Vue._.random(20) // for getting random number between 20



        this._.random(20) //or other method you want to use




        in any of the component file :)






        share|improve this answer

















        • 5




          This works, but do you really need another module in your project to add something that can be added manually with one line?
          – anthonygore
          Apr 22 '17 at 7:07








        • 1




          I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
          – Ewing
          Apr 24 '17 at 21:41




















        up vote
        0
        down vote













        import _ from 'lodash'
        Vue.prototype._ = _


        Insert these lines in your main.js file and it will work all over your app.






        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37694243%2fusing-lodash-in-all-of-vue-component-template%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          9 Answers
          9






          active

          oldest

          votes








          9 Answers
          9






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          46
          down vote



          accepted










          Some of the current answers may work in your scenario, but they have downsides:




          • Adding to the window object means your Vue project can't be server rendered, because servers don't have access to the window object.

          • Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.


          An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this keyword.



          import _ from 'lodash';    
          Object.defineProperty(Vue.prototype, '$_', { value: _ });


          Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:



          export default {
          created() {
          console.log(this.$_.isEmpty(null));
          }
          }


          The advantage of using Object.defineProperty rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.



          This is more thoroughly explained in this blog post (which I wrote).



          Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash"; at the top of the file requiring it.






          share|improve this answer



















          • 8




            FYI - can write like Vue.prototype.$lodash = lodash
            – DogCoffee
            May 5 '17 at 9:18










          • Thank you! I aliased lodash with $_
            – pymarco
            Sep 6 '17 at 18:21










          • I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
            – user5814
            Mar 31 at 10:08






          • 2




            I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
            – anthonygore
            Apr 1 at 10:31















          up vote
          46
          down vote



          accepted










          Some of the current answers may work in your scenario, but they have downsides:




          • Adding to the window object means your Vue project can't be server rendered, because servers don't have access to the window object.

          • Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.


          An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this keyword.



          import _ from 'lodash';    
          Object.defineProperty(Vue.prototype, '$_', { value: _ });


          Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:



          export default {
          created() {
          console.log(this.$_.isEmpty(null));
          }
          }


          The advantage of using Object.defineProperty rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.



          This is more thoroughly explained in this blog post (which I wrote).



          Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash"; at the top of the file requiring it.






          share|improve this answer



















          • 8




            FYI - can write like Vue.prototype.$lodash = lodash
            – DogCoffee
            May 5 '17 at 9:18










          • Thank you! I aliased lodash with $_
            – pymarco
            Sep 6 '17 at 18:21










          • I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
            – user5814
            Mar 31 at 10:08






          • 2




            I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
            – anthonygore
            Apr 1 at 10:31













          up vote
          46
          down vote



          accepted







          up vote
          46
          down vote



          accepted






          Some of the current answers may work in your scenario, but they have downsides:




          • Adding to the window object means your Vue project can't be server rendered, because servers don't have access to the window object.

          • Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.


          An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this keyword.



          import _ from 'lodash';    
          Object.defineProperty(Vue.prototype, '$_', { value: _ });


          Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:



          export default {
          created() {
          console.log(this.$_.isEmpty(null));
          }
          }


          The advantage of using Object.defineProperty rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.



          This is more thoroughly explained in this blog post (which I wrote).



          Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash"; at the top of the file requiring it.






          share|improve this answer














          Some of the current answers may work in your scenario, but they have downsides:




          • Adding to the window object means your Vue project can't be server rendered, because servers don't have access to the window object.

          • Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.


          An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this keyword.



          import _ from 'lodash';    
          Object.defineProperty(Vue.prototype, '$_', { value: _ });


          Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:



          export default {
          created() {
          console.log(this.$_.isEmpty(null));
          }
          }


          The advantage of using Object.defineProperty rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.



          This is more thoroughly explained in this blog post (which I wrote).



          Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash"; at the top of the file requiring it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 4 at 7:45

























          answered Apr 22 '17 at 6:58









          anthonygore

          2,6812026




          2,6812026








          • 8




            FYI - can write like Vue.prototype.$lodash = lodash
            – DogCoffee
            May 5 '17 at 9:18










          • Thank you! I aliased lodash with $_
            – pymarco
            Sep 6 '17 at 18:21










          • I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
            – user5814
            Mar 31 at 10:08






          • 2




            I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
            – anthonygore
            Apr 1 at 10:31














          • 8




            FYI - can write like Vue.prototype.$lodash = lodash
            – DogCoffee
            May 5 '17 at 9:18










          • Thank you! I aliased lodash with $_
            – pymarco
            Sep 6 '17 at 18:21










          • I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
            – user5814
            Mar 31 at 10:08






          • 2




            I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
            – anthonygore
            Apr 1 at 10:31








          8




          8




          FYI - can write like Vue.prototype.$lodash = lodash
          – DogCoffee
          May 5 '17 at 9:18




          FYI - can write like Vue.prototype.$lodash = lodash
          – DogCoffee
          May 5 '17 at 9:18












          Thank you! I aliased lodash with $_
          – pymarco
          Sep 6 '17 at 18:21




          Thank you! I aliased lodash with $_
          – pymarco
          Sep 6 '17 at 18:21












          I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
          – user5814
          Mar 31 at 10:08




          I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
          – user5814
          Mar 31 at 10:08




          2




          2




          I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
          – anthonygore
          Apr 1 at 10:31




          I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
          – anthonygore
          Apr 1 at 10:31












          up vote
          18
          down vote













          You could import the lodash into each component:



          <script>
          import _ from 'lodash'

          export default {
          methods: {
          test (value) {
          return _.isEmpty(value)
          }
          }
          }
          </script>





          share|improve this answer





















          • the example of your code is only wrapping isEmpty method. how about the other lodash methods?
            – antoniputra
            Jun 8 '16 at 8:04










          • This is just an example based on yours. You could replace the isEmpty with any lodash method.
            – Pantelis Peslis
            Jun 8 '16 at 8:21








          • 2




            using import is the right way if you using lodash (_) in child component.
            – hendra1
            Feb 14 '17 at 9:38












          • I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
            – Zhang Buzz
            Jan 18 at 8:50















          up vote
          18
          down vote













          You could import the lodash into each component:



          <script>
          import _ from 'lodash'

          export default {
          methods: {
          test (value) {
          return _.isEmpty(value)
          }
          }
          }
          </script>





          share|improve this answer





















          • the example of your code is only wrapping isEmpty method. how about the other lodash methods?
            – antoniputra
            Jun 8 '16 at 8:04










          • This is just an example based on yours. You could replace the isEmpty with any lodash method.
            – Pantelis Peslis
            Jun 8 '16 at 8:21








          • 2




            using import is the right way if you using lodash (_) in child component.
            – hendra1
            Feb 14 '17 at 9:38












          • I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
            – Zhang Buzz
            Jan 18 at 8:50













          up vote
          18
          down vote










          up vote
          18
          down vote









          You could import the lodash into each component:



          <script>
          import _ from 'lodash'

          export default {
          methods: {
          test (value) {
          return _.isEmpty(value)
          }
          }
          }
          </script>





          share|improve this answer












          You could import the lodash into each component:



          <script>
          import _ from 'lodash'

          export default {
          methods: {
          test (value) {
          return _.isEmpty(value)
          }
          }
          }
          </script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 8 '16 at 6:10









          Pantelis Peslis

          8,43832840




          8,43832840












          • the example of your code is only wrapping isEmpty method. how about the other lodash methods?
            – antoniputra
            Jun 8 '16 at 8:04










          • This is just an example based on yours. You could replace the isEmpty with any lodash method.
            – Pantelis Peslis
            Jun 8 '16 at 8:21








          • 2




            using import is the right way if you using lodash (_) in child component.
            – hendra1
            Feb 14 '17 at 9:38












          • I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
            – Zhang Buzz
            Jan 18 at 8:50


















          • the example of your code is only wrapping isEmpty method. how about the other lodash methods?
            – antoniputra
            Jun 8 '16 at 8:04










          • This is just an example based on yours. You could replace the isEmpty with any lodash method.
            – Pantelis Peslis
            Jun 8 '16 at 8:21








          • 2




            using import is the right way if you using lodash (_) in child component.
            – hendra1
            Feb 14 '17 at 9:38












          • I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
            – Zhang Buzz
            Jan 18 at 8:50
















          the example of your code is only wrapping isEmpty method. how about the other lodash methods?
          – antoniputra
          Jun 8 '16 at 8:04




          the example of your code is only wrapping isEmpty method. how about the other lodash methods?
          – antoniputra
          Jun 8 '16 at 8:04












          This is just an example based on yours. You could replace the isEmpty with any lodash method.
          – Pantelis Peslis
          Jun 8 '16 at 8:21






          This is just an example based on yours. You could replace the isEmpty with any lodash method.
          – Pantelis Peslis
          Jun 8 '16 at 8:21






          2




          2




          using import is the right way if you using lodash (_) in child component.
          – hendra1
          Feb 14 '17 at 9:38






          using import is the right way if you using lodash (_) in child component.
          – hendra1
          Feb 14 '17 at 9:38














          I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
          – Zhang Buzz
          Jan 18 at 8:50




          I downvote because we have thousands of vue files, and we don't want to repeat type in import lodash everywhere. That's not good practice.
          – Zhang Buzz
          Jan 18 at 8:50










          up vote
          12
          down vote













          For inline templates separated from the js module code it should work with:



            Vue.component('some-tag', {
          computed: {
          _() {
          return _;
          }
          }
          });


          And then you can use it in template in "native" way - _.isEmpty(value).






          share|improve this answer

























            up vote
            12
            down vote













            For inline templates separated from the js module code it should work with:



              Vue.component('some-tag', {
            computed: {
            _() {
            return _;
            }
            }
            });


            And then you can use it in template in "native" way - _.isEmpty(value).






            share|improve this answer























              up vote
              12
              down vote










              up vote
              12
              down vote









              For inline templates separated from the js module code it should work with:



                Vue.component('some-tag', {
              computed: {
              _() {
              return _;
              }
              }
              });


              And then you can use it in template in "native" way - _.isEmpty(value).






              share|improve this answer












              For inline templates separated from the js module code it should work with:



                Vue.component('some-tag', {
              computed: {
              _() {
              return _;
              }
              }
              });


              And then you can use it in template in "native" way - _.isEmpty(value).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 18 '16 at 20:34









              antongorodezkiy

              1,42022030




              1,42022030






















                  up vote
                  12
                  down vote













                  You could import lodash globally like this



                  window._ = require('lodash');


                  Once that has been imported, you will have access to _ from anywhere.






                  share|improve this answer

















                  • 3




                    I downvoted because window is undefined when you server render the app.
                    – anthonygore
                    Apr 22 '17 at 7:04






                  • 1




                    Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                    – Zhang Buzz
                    Jan 18 at 9:13

















                  up vote
                  12
                  down vote













                  You could import lodash globally like this



                  window._ = require('lodash');


                  Once that has been imported, you will have access to _ from anywhere.






                  share|improve this answer

















                  • 3




                    I downvoted because window is undefined when you server render the app.
                    – anthonygore
                    Apr 22 '17 at 7:04






                  • 1




                    Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                    – Zhang Buzz
                    Jan 18 at 9:13















                  up vote
                  12
                  down vote










                  up vote
                  12
                  down vote









                  You could import lodash globally like this



                  window._ = require('lodash');


                  Once that has been imported, you will have access to _ from anywhere.






                  share|improve this answer












                  You could import lodash globally like this



                  window._ = require('lodash');


                  Once that has been imported, you will have access to _ from anywhere.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 11 '16 at 17:02









                  Brandon Ferens

                  12912




                  12912








                  • 3




                    I downvoted because window is undefined when you server render the app.
                    – anthonygore
                    Apr 22 '17 at 7:04






                  • 1




                    Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                    – Zhang Buzz
                    Jan 18 at 9:13
















                  • 3




                    I downvoted because window is undefined when you server render the app.
                    – anthonygore
                    Apr 22 '17 at 7:04






                  • 1




                    Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                    – Zhang Buzz
                    Jan 18 at 9:13










                  3




                  3




                  I downvoted because window is undefined when you server render the app.
                  – anthonygore
                  Apr 22 '17 at 7:04




                  I downvoted because window is undefined when you server render the app.
                  – anthonygore
                  Apr 22 '17 at 7:04




                  1




                  1




                  Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                  – Zhang Buzz
                  Jan 18 at 9:13






                  Also do not forget to add below code into .eslintrc.js file, otherwise ESLint will always whine about _ is undefined. globals: { _: false, },
                  – Zhang Buzz
                  Jan 18 at 9:13












                  up vote
                  7
                  down vote













                  A simple approach that worked for me:



                  Vue.set(Vue.prototype, '_', _);



                  This should allow you to use _ in all component templates and vue instances.






                  share|improve this answer





















                  • It doesn't work..
                    – Rbex
                    Jun 9 at 8:06










                  • this solution just works for one of my projects, not in another one, don't know why... weird.
                    – spetsnaz
                    Jul 11 at 21:13















                  up vote
                  7
                  down vote













                  A simple approach that worked for me:



                  Vue.set(Vue.prototype, '_', _);



                  This should allow you to use _ in all component templates and vue instances.






                  share|improve this answer





















                  • It doesn't work..
                    – Rbex
                    Jun 9 at 8:06










                  • this solution just works for one of my projects, not in another one, don't know why... weird.
                    – spetsnaz
                    Jul 11 at 21:13













                  up vote
                  7
                  down vote










                  up vote
                  7
                  down vote









                  A simple approach that worked for me:



                  Vue.set(Vue.prototype, '_', _);



                  This should allow you to use _ in all component templates and vue instances.






                  share|improve this answer












                  A simple approach that worked for me:



                  Vue.set(Vue.prototype, '_', _);



                  This should allow you to use _ in all component templates and vue instances.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 28 '17 at 19:03









                  newms87

                  5031618




                  5031618












                  • It doesn't work..
                    – Rbex
                    Jun 9 at 8:06










                  • this solution just works for one of my projects, not in another one, don't know why... weird.
                    – spetsnaz
                    Jul 11 at 21:13


















                  • It doesn't work..
                    – Rbex
                    Jun 9 at 8:06










                  • this solution just works for one of my projects, not in another one, don't know why... weird.
                    – spetsnaz
                    Jul 11 at 21:13
















                  It doesn't work..
                  – Rbex
                  Jun 9 at 8:06




                  It doesn't work..
                  – Rbex
                  Jun 9 at 8:06












                  this solution just works for one of my projects, not in another one, don't know why... weird.
                  – spetsnaz
                  Jul 11 at 21:13




                  this solution just works for one of my projects, not in another one, don't know why... weird.
                  – spetsnaz
                  Jul 11 at 21:13










                  up vote
                  3
                  down vote













                  You can use plugin/mixin like this.



                  import _ from 'lodash';
                  exports default {
                  install : function(Vue, options){
                  Vue.mixin({
                  computed : {
                  "_" : function(){
                  return _;
                  }
                  }
                  });
                  }
                  }





                  share|improve this answer

























                    up vote
                    3
                    down vote













                    You can use plugin/mixin like this.



                    import _ from 'lodash';
                    exports default {
                    install : function(Vue, options){
                    Vue.mixin({
                    computed : {
                    "_" : function(){
                    return _;
                    }
                    }
                    });
                    }
                    }





                    share|improve this answer























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      You can use plugin/mixin like this.



                      import _ from 'lodash';
                      exports default {
                      install : function(Vue, options){
                      Vue.mixin({
                      computed : {
                      "_" : function(){
                      return _;
                      }
                      }
                      });
                      }
                      }





                      share|improve this answer












                      You can use plugin/mixin like this.



                      import _ from 'lodash';
                      exports default {
                      install : function(Vue, options){
                      Vue.mixin({
                      computed : {
                      "_" : function(){
                      return _;
                      }
                      }
                      });
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 17 '17 at 6:15









                      Hotbrains

                      311




                      311






















                          up vote
                          1
                          down vote













                          Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.



                          I would note that in the tutorial, he left import "jquery" in his app.js file, which is not required. The plugin with import it automatically.






                          share|improve this answer

























                            up vote
                            1
                            down vote













                            Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.



                            I would note that in the tutorial, he left import "jquery" in his app.js file, which is not required. The plugin with import it automatically.






                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.



                              I would note that in the tutorial, he left import "jquery" in his app.js file, which is not required. The plugin with import it automatically.






                              share|improve this answer












                              Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.



                              I would note that in the tutorial, he left import "jquery" in his app.js file, which is not required. The plugin with import it automatically.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 31 at 5:51









                              user5814

                              1,058519




                              1,058519






















                                  up vote
                                  0
                                  down vote













                                  Check out vue-lodash!!
                                  It's a new wrapper for using lodash in vue.
                                  You can call it using




                                  Vue._.random(20) // for getting random number between 20



                                  this._.random(20) //or other method you want to use




                                  in any of the component file :)






                                  share|improve this answer

















                                  • 5




                                    This works, but do you really need another module in your project to add something that can be added manually with one line?
                                    – anthonygore
                                    Apr 22 '17 at 7:07








                                  • 1




                                    I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                    – Ewing
                                    Apr 24 '17 at 21:41

















                                  up vote
                                  0
                                  down vote













                                  Check out vue-lodash!!
                                  It's a new wrapper for using lodash in vue.
                                  You can call it using




                                  Vue._.random(20) // for getting random number between 20



                                  this._.random(20) //or other method you want to use




                                  in any of the component file :)






                                  share|improve this answer

















                                  • 5




                                    This works, but do you really need another module in your project to add something that can be added manually with one line?
                                    – anthonygore
                                    Apr 22 '17 at 7:07








                                  • 1




                                    I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                    – Ewing
                                    Apr 24 '17 at 21:41















                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  Check out vue-lodash!!
                                  It's a new wrapper for using lodash in vue.
                                  You can call it using




                                  Vue._.random(20) // for getting random number between 20



                                  this._.random(20) //or other method you want to use




                                  in any of the component file :)






                                  share|improve this answer












                                  Check out vue-lodash!!
                                  It's a new wrapper for using lodash in vue.
                                  You can call it using




                                  Vue._.random(20) // for getting random number between 20



                                  this._.random(20) //or other method you want to use




                                  in any of the component file :)







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Apr 22 '17 at 6:22









                                  Ewing

                                  711




                                  711








                                  • 5




                                    This works, but do you really need another module in your project to add something that can be added manually with one line?
                                    – anthonygore
                                    Apr 22 '17 at 7:07








                                  • 1




                                    I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                    – Ewing
                                    Apr 24 '17 at 21:41
















                                  • 5




                                    This works, but do you really need another module in your project to add something that can be added manually with one line?
                                    – anthonygore
                                    Apr 22 '17 at 7:07








                                  • 1




                                    I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                    – Ewing
                                    Apr 24 '17 at 21:41










                                  5




                                  5




                                  This works, but do you really need another module in your project to add something that can be added manually with one line?
                                  – anthonygore
                                  Apr 22 '17 at 7:07






                                  This works, but do you really need another module in your project to add something that can be added manually with one line?
                                  – anthonygore
                                  Apr 22 '17 at 7:07






                                  1




                                  1




                                  I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                  – Ewing
                                  Apr 24 '17 at 21:41






                                  I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
                                  – Ewing
                                  Apr 24 '17 at 21:41












                                  up vote
                                  0
                                  down vote













                                  import _ from 'lodash'
                                  Vue.prototype._ = _


                                  Insert these lines in your main.js file and it will work all over your app.






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    import _ from 'lodash'
                                    Vue.prototype._ = _


                                    Insert these lines in your main.js file and it will work all over your app.






                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      import _ from 'lodash'
                                      Vue.prototype._ = _


                                      Insert these lines in your main.js file and it will work all over your app.






                                      share|improve this answer












                                      import _ from 'lodash'
                                      Vue.prototype._ = _


                                      Insert these lines in your main.js file and it will work all over your app.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 at 16:23









                                      butaminas

                                      3018




                                      3018






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37694243%2fusing-lodash-in-all-of-vue-component-template%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          What visual should I use to simply compare current year value vs last year in Power BI desktop

                                          Alexandru Averescu

                                          Trompette piccolo