当前位置:网站首页>[nuxt 3] (XII) project directory structure 3
[nuxt 3] (XII) project directory structure 3
2022-07-28 12:39:00 【choukin】
If a person has a friend , Even if you are dying , It's worth it . I'm so happy , There was a fox friend ... ——《 The little prince 》
Plug-in directory
Nuxt Will automatically read and load plugins/ Documents in . You can add .server or .client Suffixed file names allow plug-ins to execute only on the server or client .
plugins/ All plug-ins in the directory will be registered automatically , So you don't need to be alone nuxt.config Add .
Which files will be registered
Only plugins/ Top level file ( Or in the subdirectory index file ) Will be registered as a plug-in .
Example :
plugins
| - myPlugin.ts
| - myOtherPlugin
| --- supportingFile.ts
| --- componentToRegister.vue
| --- index.ts
Only myPlugin.ts and myOtherPlugin/index.ts Will be registered .
Creating plug-ins
Plug in only receives nuxtApp) Parameters .
export default defineNuxtPlugin(nuxtApp => {
// Doing something with nuxtApp
})
Automatic auxiliary function
If you want to NuxtApp Example provides an auxiliary function , In the return object provide Add functions to . for example :
export default defineNuxtPlugin(() => {
return {
provide: {
hello: (msg: string) => `Hello ${msg}!`
}
}
})
How to use it in other documents :
<template>
<div>
{{ $hello('world') }}
</div>
</template>
<script setup lang="ts">
// alternatively, you can also use it here
const { $hello } = useNuxtApp()
</script>
Plug in type
If you return a custom auxiliary function from the plug-in , They will automatically type derivation , You will find that they will come from usenuxtApp() Return is automatic dynamic type derivation , It can also be used in the template .
If you need to use auxiliary functions inside another plug-in , You can call useNuxtApp() To get the type version , But in general , Unless you determine the calling order of the plug-in , Otherwise, this situation should be avoided .
Advanced
For advanced use cases , You can declare the type of the injected attribute like this :
declare module '#app' {
interface NuxtApp {
$hello (msg: string): string
}
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$hello (msg: string): string
}
}
export { }
Vue plug-in unit
If you want to use Vue plug-in unit , for example vue-gtag, To add GOOgle Statistics , label , You can use Nuxt Plug in .
There is one Open source RFC Make it easier to realize this function nuxt/framework#1175
First install the plug-in you want .
yarn add --dev vue-gtag-next
Create a name that is plugins/vue-gtag.client.js Plug in for
import VueGtag from 'vue-gtag-next'
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(VueGtag, {
property: {
id: 'GA_MEASUREMENT_ID'
}
})
})
Vue Instructions
Similarly , You can register a customized Vue Instructions , for example : stay plugins/directive.ts in
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.directive('focus', {
mounted (el) {
el.focus()
},
getSSRProps (binding, vnode) {
// you can provide SSR-specific props here
return {}
}
})
})
App file
app.vue File is Nuxt 3 The main components of the application .
Basic usage
stay Nuxt 3 in ,pages/ The directory is optional . If it doesn't exist pages/ non-existent ,Nuxtt It won't introduce vue-router rely on , It is very useful when an application or landing page does not need route navigation . With Nuxt 3, the pages/ directory is optional. If not present,
<template>
<h1>Hello World!</h1>
</template>
Use with pages
If you use pages/ Catalog , Need to use <NuxtPage> To show the page :
<template>
<div>
<NuxtLayout>
<NuxtPage/>
</NuxtLayout>
</div>
</template>
because Nuxt3 stay <NuxtPage> Internal use Suspense, We recommend wrapping it with a unique root element .
remember app.vue yes Nuxt The main components of the application , Any code you add to it (JS and CSS) Will be included in every page .
边栏推荐
- SuperMap itablet license module division
- IRBuilder
- 让arduino支持nuvotom新唐
- Using Arduino to develop esp8266 to build a development environment
- What if the instruction set umi2 is missing? PTK installation cannot be carried out
- 要想组建敏捷团队,这些方法不可少
- SQL injection LESS18 (header injection + error injection)
- Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment
- 软件架构师必需要了解的 saas 架构设计?
- AVL树(平衡搜索树)
猜你喜欢

用C语言开发NES游戏(CC65)10、游戏循环

Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment

Developing NES games with C language (cc65) 09, scrolling

C for循环内定义int i变量出现的重定义问题

Distributed session solution
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou

Configure jupyter remote server

Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops

Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?

用C语言开发NES游戏(CC65)09、滚动
随机推荐
Marketing play is changeable, and understanding the rules is the key!
Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Configure jupyter remote server
用C语言开发NES游戏(CC65)06、精灵
用C语言开发NES游戏(CC65)11、Metatiles
[Nuxt 3] (十二) 项目目录结构 3
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution
Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
If you don't roll the golden nine and silver ten, it's too late
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server
云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
太赞了!京东研发一哥力荐的高可用网站构建技术PDF,备好水,慢慢啃
SuperMap arsurvey license module division
数字经济时代的开源数据库创新 | 2022 开放原子全球开源峰会数据库分论坛圆满召开
SQL注入 Less24(二次注入)
金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目
Knowledge points of MySQL (13)
Zadig v1.13.0 believes in the power of openness, and workflow connects all values