nuxt记录

57 阅读1分钟

提出问题

1.

plugins

  1. plugins意图是用来执行库或者第三方的模块,除此之外还可以加入自己封装的方法
  1. 使用步骤
1.增加plugins/xxxx.js文件,,举例子:
//注入vue不需要用export default导出,一般使用vue.prototype 或者Vue.use
//1
import Vue from 'vue'
import VueNotifications from 'vue-notifications'

Vue.use(VueNotifications)

// 2
Vue.prototype.$myInjectedFunction = string =>
  console.log('This is an example', string)
  
// 注入context ,一般挂载在app上,且要export default一下,从而可以再AsyncData中去使用context.app.xxx

// 1

export default ({ app }, inject) => {
  // Set the function directly on the context.app object
  app.myInjectedFunction = string =>
    console.log('Okay, another function', string)
}

// 同时在vue vuex context中注入需要使用 export default({app}, injext) => {}中inject

// 1

export default ({ app }, inject) => {
  inject('myInjectedFunction', string => console.log('That was easy!', string))
}


  
2.在nuxt.config.js文件内增加配置
module.exports = {
  plugins: ['~/plugins/vue-notifications']
  ...
}
3.限制使用环境
1.只在浏览器(客户端),不支持ssr(服务端),将ssr设置为false 或者mode: 'client'

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