nuxt是什么?

nuxt是一个专注于ui渲染的应用框架,你可以使用Nuxt创建一个灵活的应用框架,你可以基于它初始化新项目的基础结构代码
使用nuxt你可以快速搭建一个项目,而省略了繁琐的webpack babel sass vue-loader等等配置,同时nuxt还提供了服务器端渲染功能,
所以nuxt提供的是一种快速搭建项目的能力,
我们可以从nuxt中学到什么?
1:vue项目结构
nuxt推荐的项目结构是

assets资源目录,
components组件目录,
layouts母版页, 这个之前没写过
pages页面,
plugins插件:我之前都是把vue插件直接写在main.js中
static静态文件,
Store vuex目录,
middleware中间件目录 在每一个页面加载之前调用 如果是spa模式 就是vue的beforeRouter方法
我们可以在上面的基础上扩展新的文件:
filter 数据过滤文件 提供统一的数据整理能力
2:nuxt省去了所以的配置,同时他给我们提供了一个配置文件的入口,以保证如果我们需要特定化配置,不至于没有地方进行配置
3:为支持服务器端渲染 nuxt提供了asyncData fetch nuxtServerInit功能
4:nuxt为我们提供了去扩展nuxt能力的方法 就是模块
5:nuxt提出了view层中间件的想法:中间件的思路是在每一个页面加载和渲染之前执行的方法,我们可以用来执行鉴权或其他方法
6:nuxt提出了layout的想法,我们之前都没有想过去使用模板来定制化我们的界面
所以本质上,nuxt提供一个基本的项目结构,并把服务器端渲染集成进来,同时将当前前端发展的最新技术应用于nuxt中,如预加载 页面过度效果等

下载项目

  • npx create-nuxt-app 项目名
  • yarn create nuxt-app 项目名

    1.在集成的服务器端框架之间进行选择:

  • None (Nuxt 默认服务器)

  • Express
  • Koa
  • Hapi
  • Feathers
  • Micro
  • Fastify
  • Adonis (WIP)

    2. 选择您喜欢的 UI 框架:

  • None (无)

  • Bootstrap
  • Vuetify
  • Bulma
  • Tailwind
  • Element UI
  • Ant Design Vue
  • Buefy
  • iView
  • Tachyons

    3.选择您喜欢的测试框架:

  • None (随意添加一个)

  • Jest
  • AVA

    4.选择你想要的 Nuxt 模式 (Universal or SPA)

    5.添加 axios module 以轻松地将 HTTP 请求发送到您的应用程序中。

    6.添加 EsLint 以在保存时代码规范和错误检查您的代码。

    7.添加 Prettier 以在保存时格式化/美化您的代码。

  • 启动项目 npm run dev

    pages 路由配置

    根据目录结构会自动更新,

  • 一级路由目录结构

    1. -pages<br /> -index.vue<br /> -user.vue<br />会自动根据目录生成路由配置表
    1. [{
    2. path: "/",
    3. component: component,
    4. name: "index",
    5. },
    6. {
    7. path: "/user",
    8. component: component,
    9. name: "user",
    10. }
    11. }]
  • 二级路由目录结构

    1. -index.vue<br /> -user.vue<br /> -user | 文件夹<br /> -login.vue
    1. [
    2. {
    3. path: "/",
    4. component: component,
    5. name: "index",
    6. },
    7. {
    8. path: "/user",
    9. component: component,
    10. name: "user",
    11. children:[
    12. {
    13. path: "login",
    14. component: component,
    15. name: "user-login",
    16. }
    17. ]
    18. }
    19. ]
  • 动态路由配置

    -detail | 文件夹
    -_id.vue

    1. [
    2. {
    3. path: "/detail/:id?",
    4. component: component,
    5. name: "detail-id",
    6. }
    7. ]

跨域

在nuxt.config.js文件下配置

  1. modules: [
  2. // https://go.nuxtjs.dev/axios
  3. "@nuxtjs/axios",
  4. "@nuxtjs/proxy"
  5. ],
  6. axios: {
  7. // See https://github.com/nuxt-community/axios-module#options
  8. proxy: true, // 表示开启代理
  9. prefix: "/api", // 表示给请求url加个前缀 /api
  10. credentials: true // 表示跨域请求时是否需要使用凭证
  11. },
  12. proxy: {
  13. "/api": {
  14. target: "http://localhost:7001", // 目标接口域名
  15. changeOrigin: true, // 表示是否跨域
  16. pathRewrite: {
  17. "^/api": "" // 把 /api 替换成‘’
  18. }
  19. }
  20. }

在页面调用时

  1. this.$axios.get('/list).then(res=>{
  2. //请求结果
  3. })

附带token请求头

  1. nuxt.config.js 配置文件

    1. plugins :[
    2. {src:'~/plugins/axios',mode:'client'}
    3. ]
  2. 在plugins文件夹下面创建axios.js

    1. export default function({$axios,redirect}){
    2. $axios.onRequest(config=>{
    3. console.log("Making request to"+config.url)
    4. //从本地获取token
    5. let token = localStorage.getItem("token")
    6. //如果token不为空,将token放入请求中
    7. if(token){
    8. config.headers.authorization = token
    9. }
    10. return config
    11. })
    12. //错误处理机制
    13. $axios.onError(error => {
    14. const code = parseInt(error.response&&error.response.status)
    15. if(code===400){
    16. redirect('/400')
    17. }
    18. })
    19. }

    vuex - store仓库

  • 模块方式: store 目录下的每个 .js 文件会被转换成为状态树指定命名的子模块 (当然,index 是根模块)
  • Classic(不建议使用): store/index.js返回创建 Vuex.Store 实例的方法。

store
-user.js

  1. //user.js
  2. export const state = () => ({
  3. count: 1
  4. });
  5. export const mutations = {
  6. add(state) {
  7. state.count = state.count + 1;
  8. }
  9. };
  10. export const actions = {
  11. xx({ commit }) {
  12. commit("add");
  13. }
  14. };

在页面中 我们通过this.$store.state.user.count 拿到仓库数据
通过 this.$store.commit(‘user/add’)调用mutations的方法
通过this.$store.dispatch(‘user/xx’)调用actions 异步方法