Published: 28 May 2023 › Updated: 28 May 2023
Nuxt的基础配置与自动导入函数 / nuxt#5
https://nuxt.com.cn/docs/guide/concepts/auto-imports
全局变量defineAppConfig
//app.config.ts
export default defineAppConfig({
title: 'Hello Nuxt888',
theme: {
dark: true,
colors: {
primary: '#ff0000'
}
}
})
//app.vue
{{ appConfig.title }}
const appConfig = useAppConfig()
页面关键词配置
//nuxt.config.ts
app: {
head: {
titleTemplate: "%s - 固定标题",
title: "这是首页",
charset: "utf-8",
htmlAttrs: {
lang: "zh-cn"
},
meta: [
{ name: "description", content: "首页描述" },
{ name: "keywords", content: "首页关键词" },
]
}
},
//index.vue 对单独页面的设置,会覆盖全局配置
useHead({
title:"首页index",
meta:[
{ name:"description",content:"首页描述2" },
{ name:"keywords",content:"首页关键词2" },
],
})
全局CSS
// 方法一
1. assets -> main.css
2. nuxt.config.ts中配置:
css: [
"@/assets/main.css"
],
// 方法二
//在 app.vue 中引入样式。注意不是在[removed]中!
<script setup>
import "@/assets/main.css";
</script>
// 方法三
//或者直接在 `uno.config.ts`中定义 shortcuts,它也是相当于全局的
shortcuts: {
// shortcuts to multiple utilities
'containerX': 'w-[95%] mx-auto text-4.4 lg:w-[85%]'
'btn': 'py-2 px-4 font-semibold rounded-lg shadow-md',
'btn-green': 'text-white bg-green-500 hover:bg-green-700',
// single utility alias
'red': 'text-red-100'
}
全局函数utils
Nuxt使用 utils/ 目录在整个应用程序中使用auto-imports自动导入辅助函数和其他实用程序!
export function test2() {
console.log(12793, "test.js2")
}
//也可以自定义导出目录
//nuxt.config.ts
imports: {
dirs: ["apis"]
}
composables
在composables/目录中编写自己的自动导入可重用函数。但它只导入顶层函数,如有二级目录,则必须在config中配置!
export const newFun = (i) => {
return i+5
}
//composables/gets/foo.js
imports: {
dirs: ["composables/**"]
},
Nuxt有些坑虽然让人很无语,特别是用习惯了Vue的人。但是Nuxt的强大和自动化程度真是太强大了。举个例子,以前用Vue时要写无数的import export来导入和导出包。到了Nuxt这,全自动化了,只管写就行,它会自动导入!
Nuxt的更新还是很快的,我用的这几个月中就从3.1更新到3.5了。有个[unhandled] [500] Body is unusable的问题一直没有解决,有时也会启动超慢,但瑕不掩瑜,优点还是很大嘀!
Leave Nuxt的基础配置与自动导入函数 / nuxt#5 to:
Read more #cn posts
Best Posts From lemooljiang
We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From lemooljiang
- Nextjs初体验,和Nuxt的异同
- DeepTutor:AI个性化辅导平台 / ai #57
- ComfyUI:可编程和自动化的PS / ai #56
- 将Neo4j封装进Agent / ai #55
- 闲鱼省钱大法:极限0.5折!
- 对Neo4j批量导入结构化数据 / ai #54
- langchain对neo4j进行交互,写入和查询数据 / ai #53
- 知识图谱和图数据库Neo4j的作用和使用场景 / ai #52
- SpaceX、美股和比特币
- 又是币圈血洗日!
- 链股合流,当币安也开始卖股票啰
- 天涯社区重启,爷青回?
- 诺基亚在AI时代重新杀回来啦
- 《2049:未来10000天的可能》书中的镜像世界为什么不叫元宇宙
- 今天适合吃披萨
- 开通即被封,别再给Claude送人头了!
- 两根内存条引发的“官司”
- AI·Joe V12更新, 更新GPT-5.5和DeepSeek V4 等
- 中医进校园活动反响热烈
- 猪价低伤农,猪价贵伤民