当前位置:网站首页>[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 .
边栏推荐
- 卸载 Navicat:正版 MySQL 官方客户端,真香!
- Introduction to resttemplate
- 用C语言开发NES游戏(CC65)08、背景 碰撞
- AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!
- Let Arduino support nuvotom Xintang
- Fusion cloud native, enabling new mileage | 2022 open atom global open source summit cloud native sub forum successfully held
- Develop NES game (cc65) 07 and controller with C language (collision with spirit)
- Analysis of new retail e-commerce o2o model
- PHP gets all the dates of this week or the last seven days
- How does musk lay off staff?
猜你喜欢

云原生机器学习落地难?灵雀云助力企业快速应用 MLOps

DIY system home page, your personalized needs PRO system to meet!

用C语言开发NES游戏(CC65)07、控制器

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

【萌新解题】爬楼梯

Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held

Analysis of new retail e-commerce o2o model

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开

FlexPro软件:生产、研究和开发中的测量数据分析

Come to tdengine Developer Conference and have an insight into the future trend of data technology development
随机推荐
Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
Great! Jd.com developed the highly available website construction technology PDF recommended by the first brother. Prepare the water and chew it slowly
First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
【一知半解】零值拷贝
Four authentic postures after suffering and trauma, Zizek
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
用arduino开发ESP8266 搭建开发环境
What SaaS architecture design does a software architect need to know?
[Nuxt 3] (十二) 项目目录结构 3
卸载 Navicat:正版 MySQL 官方客户端,真香!
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
What if the instruction set umi2 is missing? PTK installation cannot be carried out
On Governance and innovation | the openanolis sub forum of the 2022 open atom global open source summit was successfully held
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server
行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
arduino pro mini ATMEGA328P 连线和点亮第一盏LED(同时记录烧录失败的问题stk500_recv)
Analysis of new retail e-commerce o2o model
易观分析:以用户为中心提升手机银行用户体验,助力用户价值增长
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?