首页 > 解决方案 > Nuxt.js 为自定义插件返回未定义

问题描述

我想在 Nuxt.js 中为本地数据库创建一个自定义插件,在完成代码后,我在 nuxt.config.js 中注册了它,但它不起作用。所以我尝试了文档上的示例代码,看看我做错了什么,问题是,示例代码也不起作用。这是我注册它的方式:

插件/hello.js

export default ({ app }, inject) => {
  inject("hello", msg => console.log(`Hello ${msg}!`));
};

nuxt.config.js

module.exports = {
  ssr: false,
  target: "static",
  head: {
    title: "project-title",
    meta: [{ charset: "utf-8" }],
  },
  loading: false,
  plugins: [{ ssr: true, src: "@/plugins/icons.js" }, "@/plugins/hello.js"],
  modules: ["@nuxtjs/axios", "@nuxtjs/auth-next"],
}

每当我尝试使用this.$hello("something")时,Nuxt 都会返回this.$hello 不是函数

标签: vue.jsvuejs2nuxt.js

解决方案


Hmm, got it. After deleting and reinstalling node_modules it's resolved, then I tried changing the name of the plugin file, after then build failed from ./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js. After reinstalling packages again (deleted .nuxt, node_modules and lock file) issue is resolved.


推荐阅读