一,Vuex的使用

 import Vue from 'vue'
import Vuex from 'vuex'
import MsgModules from './MsgModules'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
msg: MsgModules
}
})
 export default{
state: {
CheckedMenu: '', //菜单选中变量
CheckedLottoryHistory: [] //彩票历史变量
},
mutations: {
setCheckedMenu (state, index) {
state.CheckedMenu = index
},
setHistory (state, arr) {
state.CheckedLottoryHistory = arr
}
},
getters: {
},
actions: {
}
}
 
state中声明各种状态变量,使用方法this.$store.state.msg.xx
mutations中对state中状态做同步修改this.$store.commit("xx",obj),只能有一个obj参数
getters中为获取状态变量的方法this.$store.getters.xx
actions中可以支持异步操作状态变量this.$store.dispatch("xx",obj)
 
vuex.store使用modules可以统筹多个状态管理文件,使业务分离,便于维护
 
二,Vue核心,组件的编写
 <template>
<div>
<div class="nav">
<div class="channel">
<ul>
<li v-for="(menu,index) in menulist" :key="index">
<LinkBtn :title="menu.DESCR" :index="index"></LinkBtn>
</li>
</ul>
</div>
</div>
<ul class="newsContent" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
<li class="section" v-for="(item, index) in types" :key="index">
<LastIssueItem :code="item" :name="categorys.CAI[item].DESCR"></LastIssueItem>
</li>
</ul>
</div>
</template> <script>
import { Indicator } from 'mint-ui'
import LinkBtn from '@/components/LinkBtn'
import LastIssueItem from '@/components/LastIssueItem'
export default {
data () {
return {
busy: false,
categorys: {},
menulist: [],
types: []
}
},
mounted () {
this.$myaxios.get('http://misc.opencai.net/consts/lotts.json') // 使用自定义的axios来做ajax操作
.then((response) => {
if (response.data) {
this.categorys = response.data
this.menulist = response.data.GRP.C // 菜单数据
let firstKey = Object.keys(this.menulist)[0] // 第一个菜单的关键字
this.$store.commit('setCheckedMenu', firstKey) // 设置第一个菜单被选中,状态提交至vuex的状态管理器中
this.types = this.menulist[firstKey].CODES // 第一个菜单下的各类彩票编码集合
}
})
},
watch: {
CheckedMenu: { /* 监听菜单是否切换 */
handler (newValue, oldValue) {
let self = this
self.busy = true
setTimeout(() => {
if (self && !self._isDestroyed) {
// 菜单发生切换之后,从集合中获取新的大类下的彩票类别10条
self.types = self.menulist[newValue].CODES.slice(0, 10)
self.busy = false
}
}, 500)
},
deep: true
},
busy (newValue, oldValue) {
if (newValue) {
Indicator.open({text: '加载中...', spinnerType: 'fading-circle'})
} else {
Indicator.close()
}
}
},
components: {
LinkBtn,
LastIssueItem
},
computed: {
CheckedMenu () {
scrollTo(0, 0)
// 获取Vuex状态管理器中的选中菜单,如果状态管理器中菜单值发生改变,此处的计算属性也会发生改变
return this.$store.state.msg.CheckedMenu
}
},
methods: {
loadMore () {
let i = this.types.length
if (i > 0) {
// 加载更多时,从某大类下获取所有彩票类型编码
let childs = this.menulist[this.CheckedMenu].CODES
// 如果大类下的彩票编码数量大于当前彩票编码数量则可以加载更多
if (childs.length > i) {
this.busy = true
// 从大类下的彩票编码集合中截取更多10条加入当前彩票编码集合
let array = childs.slice(i, i + 10)
for (var j = 0; j < array.length; j++) {
this.types.push(array[j])
}
this.busy = false
}
}
}
}
}
</script> <style lang="css" scoped>
.nav {
width: 100%;
height: .96rem;
background-color: #f4f5f6;
display: flex;
position: fixed;
z-index: 99;
}
.channel {
display: inline-block;
flex: 1;
white-space: nowrap;
display: flex;
align-items: center;
overflow-x: scroll;
font-size: .30rem;
color: #4b4949;
}
.channel::-webkit-scrollbar {
height: 0;
}
.channel ul li {
display: inline-block;
}
.newsContent {
padding-top: 0.96rem;
}
.section {
width: 100%;
height: 2.24rem;
border-bottom:1px solid #ccc;
}
</style>

可以看到组件中引入了两个自定义的组件,横向大类菜单和纵向各类彩票最新一期的信息。使用了Mint-UI中的Infinite scroll来做无限滚动加载,Indicator来做loading。唯一的操作“菜单切换”使用vuex来做状态管理。最终效果如下图

列表的子项组件,当传入某彩票编码时,获取彩票的数据

 <template lang="html">
<div class="news" @click="toDetail">
<div class="p">
<span class="s1">{{name}}</span>
<span class="s2">第{{Detail&&Detail.expect||""}}期</span>
<span class="s3">
<span style=" color:grey;">开奖:</span>
<span v-if="Detail.hasOwnProperty('opentime')&&Detail.opentime">
{{(new Date(Detail.opentime)).toLocaleDateString()}}
</span>
</span>
</div>
<div class="p">
<span v-for="(value, index) in RedBall" class="ball1" :key="'Red'+index">
<span class="word">
{{value}}
</span>
</span>
<span v-for="(value, index) in BlueBall" class="ball2" :key="'Blue'+index">
<span class="word">
{{value}}
</span>
</span>
</div>
</div>
</template> <script>
export default {
data () {
return {
List: [],
Detail: {},
RedBall: [],
BlueBall: []
}
},
props: ['code', 'name'],
mounted () {
// 此处根据传过来的彩票代码获取彩票最新的数据
this.$myaxios.get('/myapi/' + this.code + '-20.json')
.then((response) => {
if (response.data.rows > 0) {
// 某类彩票的最近20条数据
this.List = response.data.data
// 某类彩票的最近一条数据
this.Detail = response.data.data[0]
// 彩票数据的数字码
if (this.Detail.hasOwnProperty('opencode') && this.Detail.opencode) {
let arr = this.Detail.opencode.split('+')
if (arr.length > 0) {
if (arr[0]) {
this.RedBall = arr[0].split(',')
}
if (arr[1]) {
this.BlueBall = arr[1].split(',')
}
} else {
this.RedBall = this.Detail.opencode.split(',')
}
}
}
})
},
methods: {
toDetail () {
// 点击时,将某彩票的历史数据传递给Vuex的状态管理器。然后在历史页中直接获取这些历史数据并呈现
this.$store.commit('setHistory', this.List)
this.$router.push({path: '/history', query: { name: this.name }})
}
},
computed: {
}
}
</script> <style lang="css" scoped>
.news {
height: 2.24rem;
box-sizing: border-box;
width: 100%;
padding: 0.01rem;
}
.p{
height: 1.12rem;
line-height: 1.12rem;
padding-left: .1rem;
padding-right: .1rem;
vertical-align:middle;
width: 100%;
display:inline-block;
}
.s1{
color:blue;
width: 30%;
padding:2px;
}
.s2{
padding-left:2px;
padding-right: 2px;
}
.ball1{
border-radius: 50%;
height: 20px;
width: 20px;
display: inline-block;
background: #f54646;
vertical-align: top;
}
.ball2{
border-radius: 50%;
height: 20px;
width: 20px;
display: inline-block;
background: #39f;
vertical-align: top;
}
.word{
display: block;
color: #FFFFFF;
height: 20px;
line-height: 20px;
text-align:center;
}
</style>

三,路由设置

 import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index'
import history from '@/pages/history' Vue.use(Router) export default new Router({
mode: 'history',
routes: [
{
path: '/',
component: index
},
{
path: '/history',
component: history
}
],
scrollBehavior (to, from, savedPosition) {
return { x: 0, y: 0 }
}
})

四,工程的入口main.js,引入各种包和声明全局变量

 import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import store from './store/'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI) Vue.config.productionTip = false
axios.default.timeout = 5000
Vue.prototype.$myaxios = axios /* eslint-disable no-new */
let vm = new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
console.log(vm)

Vuejs2.0构建一个彩票查询WebAPP(1)

Vuejs2.0构建一个彩票查询WebAPP(2)

Vuejs2.0构建一个彩票查询WebAPP(3)

Vuejs2.0构建一个彩票查询WebAPP(2)的更多相关文章

  1. Vuejs2.0构建一个彩票查询WebAPP(1)

    说明:本人也是刚接触VUE.js,作为一个学习笔记,旨在与初学者共同学习.其中编程语法错误或者写作水平刺眼,还望轻喷. 使用工具:Visual Studio Code.技术栈为vue2+vuex+ax ...

  2. Vuejs2.0构建一个彩票查询WebAPP(3)

    整个工程的目录及截图如下,源码下载    使用心得: 1.了解Vue的生命周期很有必要,详情参见博文Vue2.0 探索之路——生命周期和钩子函数的一些理解 2.Vuex全局状态管理真是美味不可言 st ...

  3. 开源低代码平台开发实践二:从 0 构建一个基于 ER 图的低代码后端

    前后端分离了! 第一次知道这个事情的时候,内心是困惑的. 前端都出去搞 SPA,SEO 们同意吗? 后来,SSR 来了. 他说:"SEO 们同意了!" 任何人的反对,都没用了,时代 ...

  4. vue2.0构建淘票票webapp

    项目描述 之前一直用vue1.x写项目,最近为了过渡到vue2.0,特易用vue2.0栈仿写了淘票票页面,而且加入了express作为后台服务. 前端技术栈:vue2.0 + vue-router + ...

  5. vuejs2.0实现一个简单的分页

    用js实现的分页结果如图所示: css .page-bar{ margin:40px; } ul,li{ margin: 0px; padding: 0px; } li{ list-style: no ...

  6. Blazor入门笔记(1)-从0构建一个组件

    1.环境 VS2019 16.5.1 .NET Core SDK 3.1.200 Blazor WebAssembly Templates 3.2.0-preview2.20160.5 2.创建项目 ...

  7. react构建淘票票webapp,及react与vue的简单比较。

    前言 前段时间使用vue2.0构建了淘票票页面,并写了一篇相关文章vue2.0构建淘票票webapp,得到了很多童鞋的支持,因此这些天又使用react重构了下这个项目,目的无他,只为了学习和共同进步! ...

  8. 使用TensorFlow v2.0构建多层感知器

    使用TensorFlow v2.0构建一个两层隐藏层完全连接的神经网络(多层感知器). 这个例子使用低级方法来更好地理解构建神经网络和训练过程背后的所有机制. 神经网络概述 MNIST 数据集概述 此 ...

  9. 使用vuejs2.0和element-ui 搭建的一个后台管理界面

    说明: 这是一个用vuejs2.0和element-ui搭建的后台管理界面. 相关技术: vuejs2.0:一套构建用户界面的渐进式JavaScript框架,易用.灵活.高效. element-ui: ...

随机推荐

  1. CentOS 7.2 (mini) 里iptables防火墙怎么关闭?

    centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但是iptables的服务是没安装的.所以你只要停止firewalld服务即可:sudo ...

  2. 洛谷 P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed

    P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed 题目描述 Farmer John has always done his best to k ...

  3. maven的setting设置

    maven的setting设置,settings.xml文件,多写了几个仓库的地址: <?xml version="1.0" encoding="UTF-8&quo ...

  4. tomcatserver管理界面username和password忘记

    tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...

  5. nodejs02

    Node.js没有根目录的概念,因为它根本没有任何的web容器! 让node.js提供一个静态服务,都非常难! 也就是说,node.js中,如果看见一个网址是 1127.0.0.1:3000/fang ...

  6. angularjs --- ngResource 类似于 ajax发送请求。

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  7. 23.STL容器小结

  8. 20.计算速度最快的valarray

    #include <string> #include <iostream> //用于计算,计算的性能高于vector与array #include <valarray&g ...

  9. 【转】Android ClearEditText:输入用户名、密码错误时整体删除及输入为空时候晃动提示

    1 package com.lixu.clearedittext; 2 3 4 import android.app.Activity; 5 import android.os.Bundle; 6 i ...

  10. POJ 3277 线段树+扫描线

    题意: 思路: 线段树求矩形面积的并...同 POJ 1151 //By SiriusRen #include <cstdio> #include <algorithm> us ...