1. 项目创建
官网戳这里
node >= v14.20.1
使用 yarn 创建项目
yarn create nuxt-app <项目名>
2. 项目目录结构
│ nuxt.config.js ==> nuxt配置文件
│ package.json
│ README.md
│ yarn.lock
├───.nuxt ==> 打包后的文件
├───api ==> 接口,axios
│ request.js
├───assets ==> 自己的资源文件
├───components ==> 组件,nuxt 中的组件不需要引入,可以直接使用
├───pages ==> 页面,nuxt 中的路由也不需要配置,会根据 pages 下的文件生成路由表
│ │ home.vue
│ │ index.vue
├───plugins ==> 插件比如三方库需要放在这里
├───static ==> 资源文件
│ │ favicon.ico
└───store ==> vuex-store 状态管理
3. nuxtjs 配置文件
export default {
head: {
title: "nuxtjs",
htmlAttrs: {
lang: "zh-CN",
},
meta: [
{ charset: "utf-8" },
{
name: "viewport",
content: "width=device-width, initial-scale=1",
},
{
hid: "description",
name: "description",
content: "nuxtjs ssr",
},
{ name: "format-detection", content: "telephone=no" },
],
link: [
{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
{ rel: "stylesheet", href: "/style/normalize.css" },
],
},
css: ["element-ui/lib/theme-chalk/index.css"],
plugins: [{ src: "~plugins/elementUI" }],
components: true,
modules: [
"@nuxtjs/axios",
"@nuxtjs/proxy",
],
axios: {
proxy: true,
},
proxy: {
"/api": {
target: "https://xxx.com",
pathRewrite: {
"^/api": "",
},
},
},
};
4. 关于 asyncData 的一个小坑
export default {
async asyncData(){
return { }
},
data(){
return { }
}
}
5. 项目打包
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate"
}
开发时指令
打包时指令,打包后不和 vue 一样就一个 dist 文件需要部署
而 nuxt 需要这些文件 nuxt.config.js、package.json、plugins、static、yarn.lock,把这些丢给后端就好了
执行上面打包的文件
这个好像是生产源代码文件吧,我也没有用过