我是靠谱客的博主 可靠鸭子,这篇文章主要介绍Vue脚手架编写试卷页面功能,现在分享给大家,希望可以做个参考。

脚手架(vue-cli)

  (一)什么是脚手架

    概念:是一种用于快速开发Vue项目的系统架构

    优点:能够帮助咱们快速的开发项目

    缺点:由于脚手架适用于各种项目的开发,而不是单独的针对某一项目单独研发的,会出现代码冗余

脚手架的使用:

    1、安装脚手架 vue-cli

      全局安装打开cmd运行:cnpm install -g @vue/cli

    2、查看当前版本号:

      vue -V

    3、创建项目:

      根目录下打开cmd运行:vue create objectname项目名称(名称不能有大写)

正文开始

Vue脚手架实现试卷页面功能

将moduleA中的store模块化
在state中放入变量subjectList,通过mutations更新subjectList
在Home.vue中通过mapMutations激活mutations中的getSubjectList,从而更新subjectList

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
import '@/mock' export default { namespaced: true, state: { subjectList: [] }, mutations: { getSubjectList(state, payload) { state.subjectList = payload } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
store/index.js import Vuex from 'vuex' import Vue from 'vue' Vue.use(Vuex) import user from './modules/user' import moduleA from './modules/moduleA' let store =new Vuex.Store({ modules:{ moduleA } }) export default store

Home.vue

fmtSubjectType,fmtOrder2ABC为过滤器,checkSubjectType为方法,统一放在Vue.mixin中,保存在mixin中的index.js文件中

通过checkSubjectType方法的结果真假控制此div是否存在

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template> <div class="main"> <ul> <li class="item" v-for="(item, i) in subjectList" :key="i"> <h4>{{i+1}}.[{{item.type|fmtSubjectType}}] {{item.title}}</h4> <div style="color:#888;font-size:14px"> {{item.author}}{{item.createDate}} </div> <fieldset style="padding:0 10px;" v-if="checkSubjectType(item.type)"> <legend >选项</legend> <div v-for="(choice, j) in item.choice" :key="j"> {{j|fmtOrder2ABC}} {{choice.answer}} </div> </fieldset> <div v-if="checkSubjectType(item.type)">答案:{{item.answer}}</div> <div >解析:{{item.desc}}</div> </li> </ul> </div> </template>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script> import '@/mock' import {createNamespacedHelpers} from 'vuex' let {mapState,mapMutations,mapActions}= createNamespacedHelpers('moduleA') export default { async created() { let {subjectList} = await this.$get('/subjectList') this.getSubjectList(subjectList) }, computed: { ...mapState(['subjectList']) }, methods: { ...mapMutations(['getSubjectList']), } }; </script>
复制代码
1
2
3
4
5
6
7
8
9
<style scoped lang='scss'> .main{ border: 1px solid red; .item{ padding: 20px 10px; border-bottom: 1px solid #ccc; } } </style>

mixin/index.js
通过切 换BASE_URL 来切换接口,axios中的url是通过 BASE_URL 拼接的

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import axios from 'axios' import Vue from 'vue' import { BASE_URL } from '@/config' import {SUBJECT_TYPE} from '@/config/enum' Vue.mixin({ methods: { async $get(url,params){ let {data} = await axios.get(BASE_URL+url,{params}) return data }, checkSubjectType(type){ return type===SUBJECT_TYPE.DANXUAN||type===SUBJECT_TYPE.DUOXUAN } }, filters:{ fmtSubjectType(val){ return ['单选题', '多选题', '判断题', '简答题'][val] }, fmtOrder2ABC(val) { return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[val] } }, data() { return { SUBJECT_TYPE } }, })

config/index.js

通过MODE的改变,更换接口

复制代码
1
2
3
4
5
6
import {MODE_TYPE} from './enum' const BASE_URL_BEF='' const BASE_URL_PRO='XXX' const BASE_URL_DEV='PPPP' const MODE=MODE_TYPE.BEF export const BASE_URL = [BASE_URL_BEF,BASE_URL_PRO,BASE_URL_DEV][MODE]

config/enum.js

鉴于魔法数字的缘故,通过如下,使代码更清晰

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
//枚举 export const MODE_TYPE={ BEF:0, PRO:1, DEV:2 } export const SUBJECT_TYPE={ DANXUAN:0, DUOXUAN:1, PANDUAN:2, JIANDA:3 }

mock/index.js

通过mock伪造数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Mock from 'mockjs' Mock.mock('/subjectList',{ "subjectList|10":[ { "id|+1": 1, "title": "@cword(5,10)", "type": "@integer(0,3)", author:"@cname", createDate:'@datetime', "choice": [ { "id": 11, "choice": "A", "answer": 0 }, { "id": 12, "choice": "B", "answer": 1 }, { "id": 13, "choice": "C", "answer": 2 }, { "id": 14, "choice": "D", "answer": 3 } ], "answer": "C", desc:'@cword(8,25)' } ] })

总结

到此这篇关于Vue脚手架实现试卷页面的文章就介绍到这了,更多相关Vue脚手架实现试卷页面内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是可靠鸭子最近收集整理的关于Vue脚手架编写试卷页面功能的全部内容,更多相关Vue脚手架编写试卷页面功能内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(243)

评论列表共有 0 条评论

立即
投稿
返回
顶部