:::
14-7-1 注入 plugin
- plugin用法,先在 plugins 下建立一個 js 檔,如:plugins/hello.js
export default ({ app }, inject) => { // 注入 $hello(msg) in Vue, context and store. inject('hello', msg => console.log(`Hello ${msg}!`)) } - 接著修改nuxt.config.js,加入該plugin:
export default { plugins: ['~/plugins/hello.js'] } - 使用方法,前端用
this.$外掛名稱,後端用$外掛名稱,如:export default { mounted() { this.$hello('我在前端被mounted了') }, asyncData({ app, $hello }) { $hello('我在後端被asyncData了') } } - 後端用這樣也行
asyncData(context) { context.$hello('我在後端被asyncData了') },
14-7 plugin