学习【全栈之巅】Node.js + Vue.js 全栈开发王者荣耀手机端官网和管理后台笔记(2.10-2.12)_利用vue框架写一个王者荣耀英雄资料列表-程序员宅基地

技术标签: web  vue  

【全栈之巅】Node.js + Vue.js 全栈开发王者荣耀手机端官网和管理后台

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

2.10 英雄管理,比较复杂的英雄编辑页面

2.10.1 admin端Main.vue中创建英雄列表组(admin\src\views\Main.vue)
        <el-menu-item-group>
          <template slot="title">英雄</template>
          <el-menu-item index="/heroes/create">新建英雄</el-menu-item>
          <el-menu-item index="/heroes/list">英雄列表</el-menu-item>
        </el-menu-item-group>
2.10.2 新建HeroList.vue和HeroEdit.vue,并在route中增加路由
// admin\src\views\HeroEdit.vue

<template>
  <div class="about">
    <h1>{
    {
    id ? "编辑":"新建"}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="名称" >
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label="头像" >
        <!-- :action:表单提交地址,on-success:成功之后做什么,before-upload:上传之后做什么 -->
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/upload'"
          :show-file-list="false"
          :on-success="afterUpload"
        >
          <!-- 有图片显示图片,没有则显示上传图标,:src显示的图片 -->
          <img v-if="model.avatar" :src="model.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">上传</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    
  props:{
    
    id:{
    }
  },
  data(){
    
    return{
    
      model:{
    
        name: '',
        avatar: ''
      },
    }
  },
  methods:{
    
    afterUpload(res){
    
      console.log(res)
      //vue提供的方法(赋值主体,赋值的属性,res.url),效果类似this.model.icon = res.url
      // this.$set(this.model,'avatar',res.url)
      //上面data中设置了属性就可以用这个方法了,推荐使用
      this.model.avatar = res.url
    },
    async save(){
    
      if(this.id){
    
        this.$http.put(`/rest/heroes/${
      this.id}`,this.model)
      }else{
    
        this.$http.post('/rest/heroes',this.model)
      }
      this.$router.push('/heroes/list')
      this.$message({
    
        type:'success',
        message:'保存成功'
      })
    },
    async fetch(){
    
      const res = await this.$http.get(`/rest/heroes/${
      this.id}`)
      this.model = res.data
    },
    async fetchParents(){
    
      const res = await this.$http.get(`/rest/items`)
      this.parents = res.data
    }
  },
  created(){
    
    this.fetchParents()
    this.id && this.fetch()
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
// admin\src\views\HeroList.vue
<template>
  <div class="about">
    <h1>英雄列表</h1>
    <el-table :data="items">
      <el-table-column prop="_id" label="ID" width="230"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="height:3rem">
        </template>
      </el-table-column>
      <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="$router.push(`/heroes/create/${scope.row._id}`)" type="text" size="small">编辑</el-button>
        <el-button @click="remove(scope.row)" type="text" size="small">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
  </div>
</template>


<script>
export default {
    
  data(){
    
    return{
    
      items:[]
    }
  },
  methods:{
    
    async fetch(){
    
      const res = await this.$http.get('/rest/heroes')
      this.items = res.data
    },
    async remove(row){
    
      this.$confirm(`是否要删除分类${
      row.name}`, '提示', {
    
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
    
          await this.$http.delete(`/rest/heroes/${
      row._id}`)
          this.$message({
    
            type: 'success',
            message: '删除成功!'
          });
          this.fetch()
        })
    }
  },
  created(){
    
    this.fetch()
  }
}
</script>
2.10.3 路由中引用HeroList.vue和HeroEdit.vue
// admin\src\router\index.js
import HeroEdit from '../views/HeroEdit.vue'
import HeroList from '../views/HeroList.vue'

      {
    path:'/heroes/create',component:HeroEdit},
      {
    path:'/heroes/List',component:HeroList},
      {
    path:'/heroes/create/:id',component:HeroEdit,props:true}
2.10.4、添加英雄模型Hero.js
// server\models\Hero.js
const mongoose = require('mongoose')

const schema = new mongoose.Schema({
    
    name:{
    type:String},
    avatar:{
    type:String},
    title:{
    type:String},
    // 实现多选
    categories:[{
    type:mongoose.SchemaTypes.ObjectId,ref:'Category'}],
    // 评分
    scores:{
    
        difficult:{
    type:Number},
        skills:{
    type:Number},
        attack:{
    type:Number},
        survive:{
    type:Number}
    },
    // 多个技能
    skills:[{
    
        icon:{
    type:String},
        name:{
    type:String},
        description:{
    type:String},
        tips:{
    type:String}
    }],
    // 装备
    items1:[{
    type:mongoose.SchemaTypes.ObjectId,ref:'Item'}],
    items2:[{
    type:mongoose.SchemaTypes.ObjectId,ref:'Item'}],
    //使用技巧
    usageTips:{
    type:String},
    //对抗技巧
    battleTips:{
    type:String},
    //团战技巧
    teamTips:{
    type:String},
    //英雄关系
    partners:[{
    
        hero:{
    type:mongoose.SchemaTypes.ObjectId,ref:'Hero'},
        description:{
    type:String}
    }]
})

module.exports = mongoose.model('Hero',schema)
2.10.5 实现复杂的英雄编辑录入页(HeroEdit.vue) (关联,多选,el-select, multiple)
<template>
  <div class="about">
    <h1>{
    {
    id ? "编辑":"新建"}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="名称" >
        <el-input v-model="model.name"></el-input>
      </el-form-item>

      <el-form-item label="称号" >
        <el-input v-model="model.title"></el-input>
      </el-form-item>

      <el-form-item label="头像" >
        <!-- :action:表单提交地址,on-success:成功之后做什么,before-upload:上传之后做什么 -->
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/upload'"
          :show-file-list="false"
          :on-success="afterUpload"
        >
          <!-- 有图片显示图片,没有则显示上传图标,:src显示的图片 -->
          <img v-if="model.avatar" :src="model.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="类型" >
        <!-- 加multiple就可以多选 -->
        <el-select v-model="model.categories" multiple >
          <el-option v-for="item of categories" :key="item._id" :label="item.name" :value="item._id"></el-option>
        </el-select>


        <!-- 分数 -->
      <el-form-item label="难度" >
        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.difficult"></el-rate>
      </el-form-item>

        <!-- 分数 -->
      <el-form-item label="技能" >
        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.skills"></el-rate>
      </el-form-item>


        <!-- 分数 -->
      <el-form-item label="攻击" >
        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.attack"></el-rate>
      </el-form-item>


        <!-- 分数 -->
      <el-form-item label="生存" >
        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.survive"></el-rate>
      </el-form-item>

    </el-form-item>

      <el-form-item label="顺风出装" >
        <!-- 加multiple就可以多选 -->
        <el-select v-model="model.items1" multiple >
          <el-option v-for="item of items" :key="item._id" :label="item.name" :value="item._id"></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="逆风出装" >
        <!-- 加multiple就可以多选 -->
        <el-select v-model="model.items2" multiple >
          <el-option v-for="item of items" :key="item._id" :label="item.name" :value="item._id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="使用技巧">
        <el-input type="textarea" v-model="model.usageTips"></el-input>
      </el-form-item>

      <el-form-item label="对抗技巧">
        <el-input type="textarea" v-model="model.battleTips"></el-input>
      </el-form-item>

      <el-form-item label="团战技巧">
        <el-input type="textarea" v-model="model.teamTips"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">上传</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    
  props:{
    
    id:{
    }
  },
  data(){
    
    return{
    
      categories:[],
      items:[],
      model:{
    
        name: '',
        avatar: '',
        scores:{
    
          difficult:0
        }
      },
    }
  },
  methods:{
    
    afterUpload(res){
    
      console.log(res)
      //vue提供的方法(赋值主体,赋值的属性,res.url),效果类似this.model.icon = res.url
      // this.$set(this.model,'avatar',res.url)
      //上面data中设置了属性就可以用这个方法了,推荐使用
      this.model.avatar = res.url
    },
    async save(){
    
      if(this.id){
    
        this.$http.put(`/rest/heroes/${
      this.id}`,this.model)
      }else{
    
        this.$http.post('/rest/heroes',this.model)
      }
      this.$router.push('/heroes/list')
      this.$message({
    
        type:'success',
        message:'保存成功'
      })
    },
    async fetch(){
    
      const res = await this.$http.get(`/rest/heroes/${
      this.id}`)
      // this.model = res.data
      this.model = Object.assign({
    },this.model,res.data)
    },
    async fetchCategories(){
    
      const res = await this.$http.get(`/rest/categories`)
      this.categories = res.data
    },
    async fetchItems(){
    
      const res = await this.$http.get(`/rest/items`)
      this.items = res.data
    }
  },
  created(){
    
    this.fetchCategories()
    this.fetchItems()
    this.id && this.fetch()
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

2.12 英雄的技能编辑=》将英雄编辑页内容用tab包裹区分为basic 和skills 两个tab页

// admin\src\views\HeroEdit.vue
<el-tab-pane label="技能" name="skills">
          <!-- type默认为按钮,type="text"为文字链接样式 -->
          <!-- 数据中skills必须是数组 -->
          <el-button style="margin-bottom:1rem;" size="small" @click="model.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <!-- :md="12"表示在普通屏幕上一行显示两个框 -->
            <el-col :md="12" v-for="(item,i) in model.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <!-- 将res.url赋值到item.icon上,res => item.icon = res.url -->
                <!-- res => $set(item,'icon',res.url,将res.url赋值在item主体的icon属性上 -->
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="res => $set(item,'icon',res.url)"
                >
                  <!-- 有图片显示图片,没有则显示上传图标,:src显示的图片 -->
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <!-- 大文本框 -->
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.skills.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

      </el-tabs>
      ...
<script>
  export default {
    
    props: {
    
      id: {
    },
    },
    data() {
    
      return {
    
        categories: [],
        items: [],
        model: {
    
          skills: [],
          scores: {
    
            difficult: 0,
          },
        },
        parents: [],
        imageUrl: '',
      };
    },
    created() {
    
      this.fetchParents();
      this.fetchCategories();
      this.fetchItems();
      this.id && this.fetch(this.id);
    },
    methods: {
    
      afterUpload(res, file) {
    
        console.log(res, file);
        this.$set(this.model, 'avatar', res.url);
        // this.model.icon = res.url; // 可能会无法响应赋值,也可以先在data上定义好,就 不用set赋值了。
        // this.imageUrl = URL.createObjectURL(res.raw);
      },
      beforeAvatarUpload(file) {
    
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
    
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
    
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      async fetch(id) {
    
        const res = await this.$http.get(`/rest/hero/${
      id}`);
        console.log(res);
        // 通过y一次浅拷贝,确保model中有多层属性
        this.model = Object.assign({
    }, this.model, res.data);
        // this.model = res.data;
      },
      async fetchParents() {
    
        const res = await this.$http.get(`/rest/hero/`);
        console.log(res);
        this.parents = res.data;
      },
      async fetchCategories() {
    
        const res = await this.$http.get(`/rest/categories/`);
        console.log(res);
        this.categories = res.data;
      },
      async fetchItems() {
    
        const res = await this.$http.get(`/rest/item/`);
        // console.log(res);
        this.items = res.data;
      },
      async save() {
    
        if (this.id) {
    
          const res = await this.$http.put(`/rest/hero/${
      this.id}`, this.model);
          console.log(res);
          this.$message({
    
            type: 'success',
            message: '编辑成功',
          });
          this.$router.push('/hero/list');
          return;
        }
        // async await 与  this.$http.post().then 相似
        const res = await this.$http.post('/rest/hero/', this.model);
        console.log(res);
        this.$message({
    
          type: 'success',
          message: '创建成功',
        });
        this.$router.push('/hero/list');
      },
    },
  };
</script> 
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43941712/article/details/115013680

智能推荐

王者荣耀服务器维护到几点,王者荣耀维护到几点?6.23维护时间介绍[多图]-程序员宅基地

文章浏览阅读1.1k次。王者荣耀6月23日要维护多久才能玩?昨天新赛季就已经在更新了,晚上23:30就开始停机维护,一直到现在还在维护中,在此期间玩家是进不去游戏的,具体维护的时间114小编会在下方的攻略中为玩家们提供,各位都一起来了解下吧。王者荣耀6.23维护时间介绍抢先服:2021年6月22日23:30-6月23日9:30停机更新(6月22日23:00禁止进入PVP和人机对战)正式服:2021年6月22日23:30-..._王者抢先服维护到什么时候

数字孪生|交通运输可视化系统_数据可视化在交通运输领域的应用-程序员宅基地

文章浏览阅读1.9k次。智慧交通的发展,得益于现代物联网、云计算、大数据、移动互联网等新一代信息技术的快速发展,为智慧交通提供了强大的技术支撑。_数据可视化在交通运输领域的应用

洛谷题解P1022 计算器的改良_问题 m: 计算器的改良c++-程序员宅基地

文章浏览阅读360次。一、题目https://www.luogu.org/problemnew/show/P1022二、代码#include<bits/stdc++.h>using namespace std;int main(){ int coe = 0; // coe * x = value, coe即为x的系数 int value = 0; int..._问题 m: 计算器的改良c++

html标记 字体标记_html语言中,标题字体的标记是(-程序员宅基地

文章浏览阅读78次。======================================================注:重要!程序员如何有效的放松身心!下班后做什么?======================================================html的字体标记分为实体标记和逻辑标记两种,两者有以下不同:a)实体标记有固定的显示效果,逻辑标记则根据不同的浏_html语言中,标题字体的标记是(

Allegro 16.6快捷键设置_allegro16.6快捷键-程序员宅基地

文章浏览阅读2.1k次。C:\Cadence\Cadence_SPB_16.6-2015\share\pcb\text路径下的env文件,用记事本打开,找到# example of a funckey to emulate Layout capability# The "-cursor" option can be added to any Allegro command with the pick familty# and we utilize the position under the cursor when t_allegro16.6快捷键

Shell编程之流程控制_shell 流程-程序员宅基地

文章浏览阅读228次。一、shell概述Shell是一个命令行解释器,可以接收应用程序和用户的命令,然后调用操作系统的内核。Shell还是一个功能相当强大的编程语言,易编写、易调试、灵活性强。二、流程控制2.1 if判断基本语法if [ 条件判断式 ];then 程序fi或者if [ 条件判断式 ] then 程序elif [ 条件判断式 ] then 程序else 程序fi(1)[ 条件判断式 ] 中括号和条件._shell 流程

随便推点

Ubuntu 16.04下安装CUDA8.0+Cudnn+Caffe_ubuntu本地安装cudnn8.0-程序员宅基地

文章浏览阅读1.3k次。参考http://www.linuxidc.com/Linux/2017-11/148629.htm http://blog.csdn.net/yaningli/article/details/77089696 首先卸载1.cuda9.0卸载切换到安装目录执行 cd /usr/local/cuda-9.0/bin/ lssudo ./uninstall_cuda_9.0.pl2.cuda8._ubuntu本地安装cudnn8.0

i.MX6 交叉编译opencv3.4.1_ld: ../../lib/libopencv_imgcodecs.so.3.4.1: undefi-程序员宅基地

文章浏览阅读2.7k次,点赞2次,收藏11次。环境:PC操作系统:Ubuntu 16.04 LTS交叉编译工具:Poky 1.7.0cmake:3.13.2cmake-gui:3.13.2准备工作:一、下载opencv3.4.1源码:opencv-3.4.1.zip二、创建工作目录及解压:buildopencv — 总目录buildopencv/build — 配置生成makefile的目录buildopencv/..._ld: ../../lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `png_init

matlab信号仿真模型,基于MATLAB的UWB信号仿真模型-程序员宅基地

文章浏览阅读1.9k次。!"#$! !""#$"%$!& %&’(! !"#$%&’()*+,-.!-./0"# ’""($")" )*+,! 12!)&%*$"#3#45678#9*:;?>@ABCD+ ! " # $ % & E ’ $ F ,-./012 -3 4-415 .0567/859: ;410<=4-. G )> HG # I !""# J )! ..._matlab 生成uwb信号

CentOS 7中安装配置JDK 1.7-程序员宅基地

文章浏览阅读432次。前言简单记录一下在CentOS 7中安装配置JDK 1.7的全过程~下载首先是jdk 1.7 64bit &amp; 32bit的下载地址:jdk-7u79-linux-x64.tar.gz (http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz) jdk-7u79-linux...

GAN的理解与TensorFlow的实现 谷磊_gan的tensorflow实现-程序员宅基地

文章浏览阅读2.4k次。对应的github:https://github.com/burness/tensorflow-101近年来,基于数据而习得“特征”的深度学习技术受到狂热追捧,而其中GAN模型训练方法更加具有激进意味:它生成数据本身。GAN是“生成对抗网络”(Generative Adversarial Networks)的简称,由2014年还在蒙特利尔读博士的Ian Goodfellow引入深度学习领域。201..._gan的tensorflow实现

总结ctf中 MD5 绕过的一些思路_ctf md5-程序员宅基地

文章浏览阅读1.1w次,点赞28次,收藏118次。总结ctf中 MD5 绕过的一些思路,包括在PHP弱类型比较中 0e 、数组、MD5后的值等于原值,以及强比较的MD5碰撞_ctf md5

推荐文章

热门文章

相关标签