Nuxt+Tailwind快速上手

<h1><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12827060-55ee9a04993c60bb.jpeg" contenteditable="false" img-data="{"format":"jpeg","size":161070,"width":2736,"height":1476,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div></h1><h1><span style="font-size: 24px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";">Tailwind</span>
</h1><p>💡 Tips:Tailwind css 只是一套页面样式,可以和多个前端框架配合使用,但是如果只有css,要手工去写UI,还是比较耗时间。最好要找到一套基于Tailwind css的UI。这样前端界面的开发速度会快一点。 可以到TailwindUI 下载相应的组件和模版,如果要看全量代码的话,要买会员。</p><p>
</p><h2>安装Nuxt</h2><h4>先决条件</h4><blockquote><ol><li>Node.js - v18.0.0 或更新版本</li></ol><ol><li>文本编辑器 - 我们推荐使用 Visual Studio Code 以及 Volar 扩展</li></ol><ol><li>终端 - 用于运行 Nuxt 命令</li></ol></blockquote><p>安装 · 快速入门 Nuxt:</p><p>#检查node版本是否满足要求
<b>node --version
</b>#如果node版本需要升级
<b>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install node</b>
#安装Nuxt
<b>yarn global add nuxt
</b>#创建Nuxt项目
<b>npx nuxi@latest init <project-name>
cd <project-name>
yarn install</b>
#启动项目
<b>yarn dev --open
</b></p><p>
</p><h2>安装Tailwind CSS</h2><p>💡 Tips:Tailwind css 只是一种样式,可以用在多种前端框架下,可以将Tailwind css作为Nuxt的模块进行安装。</p><p>Nuxt 安装Tailwind CSS:</p><p>#如果未创建Nuxt项目先创建项目,如果项目已经创建,直接进入项目
<b>npx nuxi init <project-name>
</b>#键入项目
<b>cd <project-name>
</b>#安装Tailwind css
<b>yarn install
npx nuxi module add @nuxtjs/tailwindcss
npx tailwindcss init</b>
#启动
<b>yarn dev --open
</b>
</p><p>使用Tailwind css (tailwind.config.js中引用css,在pages目录下的vue文件可以直接使用Tailwind css):</p><p><b>/** @type {import('tailwindcss').Config} */
export default {
content: [],
theme: {
extend: {},
},
plugins: [
require('@tailwindcss/forms'),
],
}
</b></p><p><b><template>
<h1 class="text-3xl font-bold underline">
Hello world!
</h1>
</template></b>
</p><p><b>
</b></p><h2>安装Tailwind UI</h2><p>如果只有Tailwind css,要快速的开发出漂亮的前端界面,需要花很多时间。目前市面上有很多基于Tailwind css进行二次封装的组件和模版,需要再安装这些组件,以加速前端的开发。其中TailwindUI 是官方开发的组件和模版。<span style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";">由于我是采用Vue(Nuxt)开发,如果要使用TailwindUI,还有一些三方组件也需要安装:</span></p><p><b>yarn add @headlessui/vue
yarn add @heroicons/vue</b>
</p><p><b>
</b></p><p>添加好看的开源字体</p><p><b>// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
modules: ["@nuxtjs/tailwindcss"],
app: {
head: { // 在每个页面的head 添加inter.css引用
link: [
{ rel: 'stylesheet', href: 'https://rsms.me/inter/inter.css' }
]

}
}

})</b>
</p><p>
</p><p>tailwind.config.js中添加引用:</p><p><b>const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
theme: {
extend: {
fontFamily: { // 添加字体
sans: ['Inter var', ...defaultTheme.fontFamily.sans],
},
},
},
// ...
}</b>
</p><p><b>
</b></p><h3>TailwindUI的资源来源</h3><p>
https://heroicons.com/
</p><p>
https://unsplash.com/
</p><p>
https://www.pixsellz.io/
</p><p>
</p><h3>TailwindUI的组件使用</h3><p>Tailwind UI - 完整的组件和模版</p><p>使用组件,以结账表单为例,只要从官网拷贝对应的代码,放入app.vue(也可以是其他vue文件)。</p><p>
</p><h2>Nuxt3添加Sitemap</h2><p>1、安装依赖包</p><p><b>pnpm add -D @nuxtjs/sitemap
#
yarn add -D @nuxtjs/sitemap
#
npm install -D @nuxtjs/sitemap</b>

</p><p>2、在nuxt.config中添加对包的引用</p><p><b>export default defineNuxtConfig({
modules: ['@nuxtjs/sitemap']
})</b>
</p><p>3、访问 https://xxxxx/<strong>sitemap.xml ,Sitemap 文件后续将自动生成</strong></p><p><strong>
</strong></p><h2>Nuxt3 国际化</h2><h4>安装</h4><p><b>yarn add -D @nuxtjs/i18n
</b></p><h4>添加模块</h4><p><b>export default defineNuxtConfig({
modules: ['@nuxtjs/i18n']
})</b>

</p><h4>添加国际化配置</h4><p><b> i18n: {
baseUrl: 'https://www.taskease.info/', // 设定域名前缀,这样在生成link 的href 会是绝对路径
strategy: 'prefix', //url中自动增加语言前缀
langDir: 'locales', //语言文件所在的目录
locales: [
{
"code": "ja",
"iso": "ja-JP", //为了便于seo,要带上iso
"name": "日本語",
"file": "ja-JP.js"
},
{
"code": "en",
"iso": "en-US",
"name": "English",
"file": "en-US.js"
},
{
"code": "ar",
"iso": "ar-AR",
"name": "العربية",
"file": "ar-AR.js"
},
{
"code": "cs",
"iso": "cs-CZ",
"name": "Čeština",
"file": "cs-CZ.js"
},
{
"code": "da",
"iso": "da-DK",
"name": "Dansk",
"file": "da-DK.js"
},
{
"code": "de",
"iso": "de-DE",
"name": "Deutsch",
"file": "de-DE.js"
},
{
"code": "el",
"iso": "el-GR",
"name": "Ελληνικά",
"file": "el-GR.js"
},
{
"code": "es",
"iso": "es-ES",
"name": "Español",
"file": "es-ES.js"
},
{
"code": "fi",
"iso": "fi-FI",
"name": "Suomi",
"file": "fi-FI.js"
},
{
"code": "fil",
"iso": "fil-PH",
"name": "Filipino",
"file": "fil-PH.js"
},
{
"code": "zh_CN",
"iso": "zh-CN",
"name": "中文",
"file": "zh-CN.js",
},
{
"code": "fr",
"iso": "fr-FR",
"name": "Français",
"file": "fr-FR.js"
},
{
"code": "id",
"iso": "id-ID",
"name": "Bahasa Indonesia",
"file": "id-ID.js"
},
{
"code": "ms",
"iso": "ms-MY",
"name": "Bahasa Melayu",
"file": "ms-MY.js"
},
{
"code": "pt_BR",
"iso": "pt-BR",
"name": "Português (Brasil)",
"file": "pt-BR.js"
},
{
"code": "ru",
"iso": "ru-RU",
"name": "Русский",
"file": "ru-RU.js"
},
{
"code": "sv",
"iso": "sv-SE",
"name": "Svenska",
"file": "sv-SE.js"
},
{
"code": "tr",
"iso": "tr-TR",
"name": "Türkçe",
"file": "tr-TR.js"
}
]
,
defaultLocale: 'en', //默认使用英文
lazy: true, //语言文件懒加载
detectBrowserLanguage: { // 开启浏览器默认使用语言检测,如果浏览器使用zh,则进入浏览器会自动跳转中文
useCookie: false, // 不使用cookie
alwaysRedirect: true // 每次重新进入网页,都是自动跳转对应的语言页面
}
},</b>
</p><p><b>
</b></p><p><b>
</b></p><h4>添加每个语言对应的js文件</h4><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12827060-f7fdd3a557309138.jpeg" img-data="{"format":"png","size":31447,"width":386,"height":434,"space":"srgb","channels":4,"depth":"uchar","density":72,"isProgressive":false,"hasProfile":false,"hasAlpha":true}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>
</p><h4>语言文件内容:</h4><p>export default {
welcome: 'hello',
backHome: 'gobackhome',
about: {
title: 'about us',
description: 'about us page'
}
};
</p><p><span style="color: rgba(0, 0, 0, 0.85); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";">vue文件中使用国际化及语言切换</span>
</p><p>
</p><h3>多语言SEO</h3><p>目前为了国际化,需要引入nuxtjs/i18n 组件。目前i18n有四种模式,为了便于Google的抓取,我使用了URL带语言前缀的方式。</p><p>
</p><p>
</p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12827060-1b16a9fb75d87f58.jpeg" img-data="{"format":"png","size":224388,"width":748,"height":401,"space":"srgb","channels":4,"depth":"uchar","density":72,"isProgressive":false,"hasProfile":false,"hasAlpha":true}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h3>Robots.txt</h3><h3>如果没有添加Robots.txt 爬虫会根据自己的设定,选择性的爬取或者不爬取该网站。所以最好的方式是,新建一个Robots.txt文件,并明确告知爬虫允许爬取网站</h3><p>
</p><h2>落地页会用到的资源</h2><p>Unsplash --获取图片</p><p>SVG Repo - 大量免费的svg和尺量图下载</p><p>Google Fonts --google 用来设计文字、图标的网站</p><p>vecteezy -- 艺术设计的svg、图片、模版(很棒)</p><p>Figma: 导入下载的文件,直接设计</p><p>Tailwind UI - 获取模版和组件</p><p>Create Amazing Mockups -- 截图美化</p><p>Adobe Express Unavailable --图片转svg</p><p>Icon Maker by Raycast -icon美化工具</p>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容