Nuxt3中的useNuxtApp和插件plugins

useNuxtApp

nuxtApp是一个运行时的上下文, 可以通过插件(plugins)来扩展它。使用provide方法就可以创建nuxt 插件,指定name,就可以在所有的组合式API和组件中通过name来调用value指定对象。
示例:

Nuxt3中的useNuxtApp和插件plugins_第1张图片

pages/index.vue:




plugins 

插件plugins目录,插件可以全局使用

Nuxt3中的useNuxtApp和插件plugins_第2张图片

plugins/index.ts:

export default defineNuxtPlugin(() => {
    return {
        provide: {
            hello: (msg: String) => {
                return `hello ${msg}!`;
            }
        }
    };
});

 pages/index.ts:


 

你可能感兴趣的:(nuxt,前端)