Nuxt + tailwindcss初始化构建WebApp

Nuxt构建程序

1
2
3
4
5
npm install -g create-nuxt-app
npx create-nuxt-app nuxtDemo

# 根据提示步骤选择对应的模块初始化即可
# 官方文档:https://www.nuxtjs.cn/guide/installation

Tailwindcss初始化

安装tailwindcss

1
2
npm install -D tailwindcss
npx tailwindcss init # 会生成tailwindcss.config.js文件

配置模板文件的路径

1
2
3
4
5
6
7
8
9
10
11
// 修改tailwindcss.config.js文件内容
/** @type {import('tailwindcss').Config} */
module.exports = {
# 在content中填入实际的路径
# 默认:content: ["./src/**/*.{html,js}"],
content: ["./src/**/*.{html,js}","./components/**/*.{vue,js,ts}","./pages/**/*.{vue,js,ts"],
theme: {
extend: {},
},
plugins: [],
}

添加tailwindcss指令至主css文件中

1
2
3
4
/** src/css/input.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

开启 Tailwind CLI 构建流程

1
npx tailwindcss -i ./src/css/input.css -o ./src/css/output.css --watch

将编译出的output.css加入nuxt主配置文件中进行全局定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 修改nuxt.config.js文件,在css中添加对应的路径
export default {
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'nuxtAppDemo',
htmlAttrs: {
lang: 'en',
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' },
{ name: 'format-detection', content: 'telephone=no' },
],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
},

// Global CSS: https://go.nuxtjs.dev/config-css
css: ['element-ui/lib/theme-chalk/index.css','./src/css/output.css'],

// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: ['@/plugins/element-ui'],

// Auto import components: https://go.nuxtjs.dev/config-components
components: true,

// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
// https://go.nuxtjs.dev/typescript
'@nuxt/typescript-build',
],

// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
'@nuxtjs/axios',
],

// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
// Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
baseURL: '/',
},

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
transpile: [/^element-ui/],
},
}

完成并测试

1
2
3
4
5
6
7
<template>
<div>
<h1 class="text-3xl font-bold underline">
Hello world!
</h1>
</div>
</template>