【全栈之巅】Node.js + Vue.js 全栈开发王者荣耀手机端官网和管理后台学习笔记(3.16-3.20)

【全栈之巅】Node.js + Vue.js 全栈开发王者荣耀手机端官网和管理后台学习笔记(3.16-3.20)

本项目是 学习Bilibili 全栈之巅 视频教程相关源码和体会
https://gitee.com/blaunicorn/node-vue-wangzherongyao
持续更新中…

3.16 web首页server导入英雄列表数据

// server\routes\web\index.js
    // 导入英雄列表接口
    router.get('/heroes/init', async (req, res) => {
        // 清空原有数据库,再插入数据
        await Hero.deleteMany({})
        const rawData = [获取的页面英雄数据]
        // 数组用for in 循环 返回的是索引值。 用of 返回的对象的数据
        for (let cat of rawData) {
            if (cat.name === '热门') {
                continue
            }
            // 增加分类的id,找到当前分类在数据库中对应的数据
            const category = await Category.findOne({
                name: cat.name
            })
            console.log(cat, category)
            cat.heroes = cat.heroes.map(hero => {
                // 可以写id,但mongodb足够智能,可以自动判断。
                // 对象引用传值,改了里面的也相当于改了本身
                hero.categories = [category]
                // hero.categories = [category.id]
                return hero
            })
            // 录入英雄

            await Hero.insertMany(cat.heroes)

        }
        res.send(await Hero.find())

3.17 web首页展示英雄列表

// server\routes\web\index.js
    // 英雄列表接口
    router.get('/hero/list', async (req, res) => {
        // 查找英雄列表
        const parent = await Category.findOne({
            name: '英雄分类'
        })
        // 聚合管道查询,多个条件1.查所有parent._id字段=上面的parent._id  2.关联查询heroes集合,本地字段_id,外键,也就是在heroes中的字段是categories,as 是作为什么名字
        const cats = await Category.aggregate([
            { $match: { parent: parent._id } },
            {
                $lookup: {
                    from: 'heroes',
                    localField: '_id',
                    foreignField: 'categories',
                    as: 'heroList'
                }
            },
            // 查出后,添加修改一个字段,把heroList 从原有的所有个字段中取10个,暂时不需要限制取几条
            // {
            //     $addFields: {
            //         heroList: { $slice: ['$heroList', 10] }
            //     }
            // }
        ])
        // console.log(cats)
        // 由于没有热门 这个分类,需要我们手动去查询添加。查询hero模型,关联categories模型,限制10条,
        const subCats = cats.map(v => v._id)
        cats.unshift({
            name: '热门',
            heroList: await Hero.find().where({
                categories: { $in: subCats }
            }).limit(10).lean()
            // }).populate('categories').limit(10).lean()
        })
        // console.log(cats)
        // cats.map(cat => {
        //     cat.heroList.map(news => {
        //         news.categoryName = (cat.name === '热门') ? news.categories[0].name : cat.name
        //         return news
        //     })
        //     return cat
        // })
        res.send(cats)
    })
// web\src\views\Home.vue
    <m-list-card
      icon="hero"
      title="英雄分类-ListCard组件"
      :categories="heroCats"
    >
      <!-- 在父组件里,不通过循环,直接拿到子组件里的具名slot的数据,
      这样的好处是 子组件的内容可以由父组件决定怎么展示 -->
      <template #items="{ category }">
        <div class="d-flex flex-wrap" style="margin: 0 -0.5rem">
          <div
            class="p-2 text-center"
            v-for="(item, index) in category.heroList"
            :key="index"
            style="width: 20%"
          >
            <img :src="item.avatar" class="w-100" alt="" />
            <div>{{ item.name }}</div>
          </div>
        </div>
      </template>
      <!-- <template v-slot:heros="{ category }"></template> -->
    </m-list-card>

3.18 web首页转跳新闻详情页

// web\src\views\Article.vue
<template>
  <div class="page-article" v-if="model">
    <div class="d-flex py-3 px-2 border-bottom">
      <div class="iconfont icon-back text-blue"></div>
      <strong class="flex-1 text-ellipsis text-blue pl-2">{{
        model.title
      }}</strong>
      <div class="text-grey fs-xs">2019-06-19</div>
    </div>
    <div class="px-3 body fs-lg" v-html="model.body"></div>
  </div>
</template>
<script>
  export default {
    props: {
      id: {
        required: true,
      },
    },
    data() {
      return {
        model: null,
      };
    },
    created() {
      this.fetch();
    },
    methods: {
      async fetch() {
        // 调用是 用 实参,没有冒号
        const res = await this.$http.get(`articles/${this.id}`);
        this.model = res.data;
      },
    },
  };
</script>

<style lang="scss" scoped>
  //   img不能自动缩小,可能存在不能深度渲染的问题,可以去掉scoped 或者在img前增加深度渲染::v-deep
  // less中一般使用 >>> 或 /deep/
  // scss中一般使用 ::v-deep
  .page-article {
    .icon-back {
      font-size: 1.692308rem;
    }
    .body {
      ::v-deep img {
        max-width: 100%;
        height: auto;
      }
      iframe {
        width: 100%;
        height: auto;
      }
    }
  }
</style>
// server\routes\web\index.js 增加获取新闻详情的api
    // 文章详情
    // :id 定义时是带冒号的形参
    router.get('/articles/:id', async (req, res) => {
        // console.log(req.params.id)
        const data = await Article.findById(req.params.id)
        res.send(data)
    })

3.19 web新闻详情页完善

// server\routes\web\index.js
// 增加相关资讯
    // 文章详情
    // :id 定义时是带冒号的形参
    router.get('/articles/:id', async (req, res) => {
        // console.log(req.params.id)
        // 3.19 增加lean()变成纯粹的json对象
        const data = await Article.findById(req.params.id).lean()
        data.related = await Article.find().where({
            // 不包含查询本身
            // title: { $ne: data.categories.title }
            // 包含查询本身
            categories: { $in: data.categories }
        }).limit(2)
        res.send(data)
    })
// web\src\views\Article.vue
    <div class="px-3 border-top py-3">
      <div class="d-flex ai-center">
        <i class="iconfont icon-menu"></i
        ><strong class="text-blue fs-lg ml-2">相关资讯</strong>
      </div>
      <div class="pt-2">
        <router-link
          class="py-1 mt-2"
          :to="`/articles/${item._id}`"
          tag="div"
          v-for="item in model.related"
          :key="item._id"
        >
          {{ item.title }}
        </router-link>
      </div>
    </div>
// 点击关联跳转只会改变id,不会自动刷新页面,需要通过watch去强制获取数据
        watch: {
      // 简写
      id: 'fetch',
      // 完整写法
      //   id() {
      //     this.fetch();
      //   },
    },

3.20 web端英雄详情页准备

// 页面 web\src\views\Hero.vue
<template>
  <div class="page-hero" v-if="model">
    <div
      class="topbar bg-black py-2 text-white px-3 d-flex ai-center text-white"
    >
      <img src="../assets/logo.png" height="30" alt="" sizes="" srcset="" />
      <!-- 用flex-1去占据全部的剩余空间 -->
      <div class="px-2 flex-1">
        <!-- <div class="text-white">王者荣耀</div> -->
        <!-- <div class="text-dark-1">团队成就更多</div> -->
        <!-- 不是上下两行了,需要用inline元素 -->
        <span class="text-white">王者荣耀</span>
        <span class="text-white ml-2">攻略站</span>
      </div>
      <router-link to="/" tag="div" class="jc-end">更多英雄 &gt;</router-link>
    </div>
  </div>
</template>
<script>
  export default {
    props: {
      id: { required: true },
    },
    data() {
      return {
        model: null,
      };
    },
    created() {
      this.fetch();
    },
    methods: {
      async fetch() {
        const res = await this.$http.get(`heroes/${this.id}`);
        this.model = res.data;
      },
    },
  };
</script>
    // 路由 web\src\router\index.js
    // 因为不会集成顶部布局,所以不用放在main的children里
    {
        path: '/heroes/:id',
        name: 'Hero',
        component: () => import(/* webpackChunkName: "Hero" */ '../views/Hero.vue'),
        props: true
    },
    // server\routes\web\index.js 英雄详情简略
    router.get('/heroes/:id', async (req, res) => {
        const data = await Hero.findById(req.params.id).lean()
        res.send(data)
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值