代码之家  ›  专栏  ›  技术社区  ›  Rishav Sharan

如何在Nuxt中添加客户端js库>

  •  4
  • Rishav Sharan  · 技术社区  · 9 年前

    在普通html中,我只会将其添加到索引中。html文件。但我不知道如何在nuxt上做同样的事情。

    如何添加?

    module.exports = {
    
      /*
      ** Headers of the page
      */
      head: {
        title: 'digglu',
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          { hid: 'description', name: 'description', content: 'social media site' },
          { name: 'google-signin-client_id', content:'xxx.apps.googleusercontent.com' }
        ],
        link: [
          { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
        ]
      },
      /*
      ** Customize the progress-bar color
      */
      loading: { color: '#3B8070' },
      /*
      ** Build configuration
      */
      build: {
        /*
        ** Run ESLINT on save
        */
        extend (config, ctx) {
          if (ctx.dev && ctx.isClient) {
            config.module.rules.push({
              enforce: 'pre',
              test: /\.(js|vue)$/,
              loader: 'eslint-loader',
              exclude: /(node_modules)/
            })
          }
        }
      }
    }
    
    2 回复  |  直到 9 年前
        1
  •  4
  •   Sergio Marcelino    9 年前

    根据NuxtJS文件: https://nuxtjs.org/guide/plugins

    我可以确认这是可行的,但一些插件在页面的前3次刷新时仍然抛出错误,然后错误消失了,我不知道原因。

    某些插件可能仅适用于浏览器,您可以使用ssr: 插件中的false选项仅在客户端运行文件。

    nuxt。配置。js:

    module.exports = {
      plugins: [
        { src: '~/plugins/vue-notifications', ssr: false }
      ]
    }
    

    import Vue from 'vue'
    import VueNotifications from 'vue-notifications'
    
    Vue.use(VueNotifications)
    

    如果您只需要为服务器提供一些库,您可以 正在创建服务器。捆js文件。

        2
  •  2
  •   lhermann jfriend00    5 年前

    nuxt.config.js .

    命名约定

    export default {
      plugins: [
        '~/plugins/foo.client.js', // only in client side
        '~/plugins/bar.server.js', // only in server side
        '~/plugins/baz.js' // both client & server
      ]
    }
    

    export default {
      plugins: [
        { src: '~/plugins/both-sides.js' },
        { src: '~/plugins/client-only.js', mode: 'client' }, // only on client side
        { src: '~/plugins/server-only.js', mode: 'server' } // only on server side
      ]
    }
    

    请参见此处: https://nuxtjs.org/guides/directory-structure/plugins#client-or-server-side-only

    推荐文章