I can't use third party components in Nuxt.js/vue.js

纵然是瞬间 提交于 2019-12-04 17:10:24

I have had the same problem with vue-touch and I tackled it by adding it as a plugin as suggested by Nuxtjs.Org

Workflow

  • Create a folder on the main directory 'plugins'
  • Create a .js file under like 'plugins/the-external-component-you-want-to-add.js' for may case 'plugins/vue-notifications.js'
  • Modify the code below, according to your needs.

    import Vue from 'vue'
    import VueTouch from 'vue-touch'
    

    Both client and server side usage

    Vue.use(VueTouch, {name: 'v-touch'})
    

    If the plugin is needed client side only

    if (process.BROWSER_BUILD) { 
      Vue.use(VueTouch, {name: 'v-touch'})
    }
    

    Then a nice log

    console.log('plugin v-touch is locked and loaded')
    
  • Inject your plugin to the webpack workflow by editing nuxt.config.js

    plugins: ['~plugins/vue-touch'],
    build: {
     ...
    }
    
  • then you could use it as you named in your plugin file.

    <v-touch @swipe="onswipeleft" class="dragme">SWIIIIIIPE</v-touch>
    

Documentation

For more information you could take a look in nuxtjs.org documentation.

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