npx nuxi init nuxt3-template-by-leehan
code nuxt3-template-by-leehan
npm install
npm run dev
启动成功后打开:http://localhost:3000/
显示nuxt定义的welcome页面
项目创建成功
npm install --save-dev @nuxtjs/tailwindcss
在nuxt.config.js中添加代码
modules: [
'@nuxtjs/tailwindcss'
]
npx tailwindcss init
Tailwind CSS IntelliSense
修改app.vue文件代码
HOMEPAGE
页面全屏显示红色 说明样式文件配置成功
根目录创建文件夹-layouts
layouts文件夹下创建default.vue
创建布局结构
NUXT3 TEMPLATE
修改app.vue文件
HOMEPAGE
重启项目检查布局是否应用成功
HOMOPAGE
USERINFO
```
ARTICLE LIST
-
文章123
-
文章111
```
[id].vue - 文章详情(动态路由)
```
ARTICLE DETAIL - {{ $route.params.id }}
```
注:在安装pinia的时候报了冲突 所以我选择加了–legacy-peer-deps 不报错的情况下可以不加
npm install pinia @pinia/nuxt @nuxtjs/composition-api --legacy-peer-deps
创建env文件夹
env文件夹中创建环境文件
.env.dev 开发环境变量
VITE_API_ENV="dev"
VITE_BASE_URL='http://10.86.11.99:8099/'
.env.test
VITE_API_ENV="test"
VITE_BASE_URL='http://www.test******.com/'
.env.prod
VITE_API_ENV="dev"
VITE_BASE_URL='http://www.prod*****.com/'
修改package.json文件
"scripts": {
"build:test": "nuxt build --mode test",
"build:prod": "nuxt build --mode prod",
"dev": "nuxt dev --mode dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
4、 修改nuxt.config.ts文件
// https://nuxt.com/docs/api/configuration/nuxt-config
import { loadEnv } from 'vite';
const envScript = process.env.npm_lifecycle_script?.split(" ") as Array
interface VITE_ENV_CONFIG{
VITE_API_ENV:string,
VITE_BASE_URL:string
}
const envName = envScript[envScript?.length - 1] // 通过启动命令区分环境
const evnData = loadEnv(envName,"env") as unknown as VITE_ENV_CONFIG
export default defineNuxtConfig({
modules: [
'@nuxtjs/tailwindcss',
'@pinia/nuxt'
],
css: [
'animate.css/animate.css',
],
runtimeConfig:{
public:evnData
}
})
@/utils/http.ts
import { hash } from 'ohash'
// 后端返回的数据类型
export interface ResOptions {
data?: T
code?: number
msg?: string
}
/**
* api请求封装
* @param { String } url 请求地址
* @param { Object } options useFtech第二个参数
* @param { Object } headers 自定义header头, 单独设置headers区分请求参数,也好设置类型
*/
const fetch = (url: string, options?: any, headers?: any): Promise => {
const { public: { VITE_BASE_URL } } = useRuntimeConfig() // 3.0正式版环境变量要从useRuntimeConfig里的public拿
const reqUrl = VITE_BASE_URL + url // 你的接口地址
// console.log(NUXT_PUBLIC_API_MOCK)
// 不设置key,始终拿到的都是第一个请求的值,参数一样则不会进行第二次请求
const key = hash(JSON.stringify(options) + url)
// 可以设置默认headers例如
const customHeaders = { token: useCookie('token').value, ...headers }
return new Promise((resolve, reject) => {
useFetch(reqUrl, { ...options, key, headers: customHeaders }).then(({ data, error }) => {
if (error.value) {
reject(error.value)
return
}
const value = data.value
// console.log('useFetchResData: ', value)
if (!value) {
// 这里处理错你自定义的错误,例如code !== 1
throw createError({
statusCode: 500,
statusMessage: reqUrl,
message: '自己后端接口的报错信息',
})
} else {
resolve(value)
}
}).catch((err: any) => {
console.log(err)
reject(err)
})
})
}
export default new class Http {
get(url: string, params?: any, headers?: any): Promise {
return fetch(url, { method: 'get', params }, headers)
}
post (url: string, params?: any, headers?: any): Promise {
return fetch(url, { method: 'post', params }, headers)
}
put (url: string, params?: any, headers?: any): Promise {
return fetch(url, { method: 'put', params }, headers)
}
delete (url: string, params?: any, headers?: any): Promise {
return fetch(url, { method: 'delete', params }, headers)
}
}
@/composables/api.ts
import Http from "@/utils/http";
export const nav = (params?:any)=>{
return Http.post('后端api路径',params)
}
服务端
const navData = await nav()
const {data} = navData
客户端
const navValue = ref([]) as any
const getNav = async ()=>{
const res = await nav()
navValue.value = res.data
}
onMounted(()=>{
getNav()
})
npm i -g pm2
pm2 init
测试环境
npm run build:test
生产环境
npm run build:prod
复制.output和ecosystem.config.js到服务器主页
测试环境
pm2 start ecosystem.config.js --env test
生产环境
pm2 start ecosystem.config.js --env prod