4

第一次上nuxt。我正在尝试添加客户端库。

在普通的 html 中,我只需将其添加到我的 index.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)/
        })
      }
    }
  }
}
4

2 回答 2

4

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

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

仅限客户端

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

例子:

nuxt.config.js:

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

插件/vue-notifications.js:

import Vue from 'vue'
import VueNotifications from 'vue-notifications'

Vue.use(VueNotifications)

如果您只需要服务器的一些库,您可以在 webpack 创建 server.bundle.js 文件时使用设置为 true 的 process.server 变量。

于 2017-08-15T14:20:17.900 回答
2

根据 Nuxt 文档,您可以在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

于 2020-09-09T21:39:01.727 回答