总结一下写 demo 过程当中 遇到的一些问题,方便本身的学习总结!若有错误,还请指正!
node-sass sass-loader
npm install node-sass sass-loader --save-dev
<style rel="stylesheet/scss" lang="scss">
http://www.jianshu.com/p/67f52071657d
底部导航栏栏 ==> 刚开始时用的 vux
的 tabbar tabbar-item
组件,发现有需求实现不了,刚开始还改了源码,最后实在受不了了
就用 vue-router roter-link
本身写了
底部导航栏 四个按钮分别对应 四个组件css
\index
import
引入了,因此不须要写路径了引入 axios ,因为 axios 不支持jsonp,因此还得引入 jsonphtml
npm install axios jsonp --save
import axios from 'axios'
import jsonp from 'jsonp'
遇到个问题不知道怎么解决vue
在纠结的过程当中也发现了几个问题:
最大的一个坑,列表老是有一部分滚动不上来,碰到这个问题,首先想到:node
图片懒加载 vue-lazyload 插件,超好用webpack
import logo from './assets/loading.gif'
static
目录里beforeEach(function(to,from,next){***})
钩子里 要作下面的事情index1,index2
sessionStorage
里面 建立 一个 __router__
值__router__
的值包括:count, transitionName , to.path ,from.path
to.path !== '/' && history[to.path] = historyCount
history['transitionName'] = 'forward'
; 为前进状态index1:1,count:1
to.path
依旧 为 undefined
,而 from.path 为 to.path 的值
index2:2,count2,index1:1
to.path == index1, from.path == index2
forward
前进状态reserve
后退状态Do not bb ,show me codeios
router.beforeEach(function (to, from, next) { let history = window.sessionStorage.__router__; if(!history){ history = {}; }else{ history = JSON.parse(history); } let historyCount = history.count * 1; //记录走过的 tab 页数量 const toIndex = history[to.path]; // 要去的索引 const fromIndex = history[from.path]; //要离开的索引 if (toIndex) { if (!fromIndex || parseInt(toIndex) > parseInt(fromIndex) || (toIndex === '0' && fromIndex === '0')) { history['transitionName'] = 'forward'; } else { history['transitionName'] = 'reverse'; } } else { //第一次没有记录session-storage 的状况 ++historyCount; history['count'] = historyCount; to.path !== '/' && (history[to.path] = historyCount); history['transitionName'] = 'forward'; } history = JSON.stringify(history); window.sessionStorage.__router__ = history; if (/\/http/.test(to.path)) { let url = to.path.split('http')[1]; window.location.href = `http${url}` } else { next() } });
经过 watch 选项监测 $route
动态的改变transitionName
的值git
<transition :name="transitionName"> <keep-alive> <router-view></router-view> </keep-alive> </transition> watch: { '$route' (to, from) { console.log(to,from); this.transitionName = JSON.parse(window.sessionStorage.__router__).transitionName; } }, 样式: //微信切换样式 ,左右滚动 //前进动画样式 .forward-enter-active,.forward-leave-active{ transition: all 0.3s; } .forward-enter{ transform: translateX(100%); } .forward-leave-to{ transform: translateX(-100%); } // 后退动画样式 .reverse-enter-active,.reverse-leave-active{ transition: all 0.3s; } .reverse-enter{ transform: translateX(-100%); } .reverse-leave-to{ transform: translateX(100%); }
若是你监听storage变动事件你就会发现,当数据发生变化时本页是监听不到storage事件变动消息的。而同域的其余打开的页面反而监听到了该消息。悲剧不?github
let history = window.sessionStorage.__router__; if(!history){ history = {}; }else{ //读取 history = JSON.parse(history); } //存储 history = JSON.stringify(history); window.sessionStorage.__router__ = history;
使用场景: 列表页==> 详情页的切换web
{path: '/newsDetails/:key', name: 'newsDetails',component:newsDetails },
<router-link class="news-item" v-for="(item,index) in newsData" :to ='{ path: "/newsDetails" + item.source_url, query:{ newsItem:JSON.stringify(item) } }' tag='li' :key='index' > </router-link> //路由外链 <router-view></router-view>
<router-link class="news-item" v-for="(item,index) in newsData" :to=" 'newsDetail' + item.source_url " tag='li' :key='index' > </router-link>
从列表页到详情页不适合用嵌套路由
由于其是两个单独的页面,并不会同时出如今一屏上
{path: '/index', name:'index', component: index, children:[ {path: '/index/newsDetails/:id', name: 'newsDetails',component:newsDetails }, ] }
<router-link> :to = "'index/newsDetails' + item.source_url" </router-link>
route
routes
router
this.$route
和 路由实例对象 this.$router
<to-top :flag="toTop" @scrolltoTop="scroll_to"></to-top>
默认值写法: 对象形式ajax
props:{ flag:{ type:Bollean, dafault(){ return false; } } }
v-on
绑定 子组件派发而来的事件 <to-top :flag="toTop" @scrolltoTop="scroll_to"></to-top>
-
methods:{ scroll_to(childmsg){ //执行 。。。 childmsg 为子组件向父组件传递的参数 } }
子组件
methods:{ xxx(){ this.$emit('scrolltoTop','aaa向父组件传递的参数') } }
注意不能直接使用 $on 监听子组件抛出的事件,而必须在模板里使用 v-on 绑定
import Vue from 'vue' import Vuex from 'vuex' import mutations from './mutations' Vue.use(Vuex); const store = new Vuex.Store({ state:{ //数据管理中心 count:0, }, mutations, //使用处进行 commit getters:{ //外界在此处得到 vuex 数据 nowTime(state){ return new Date() - 0 + '-' + state.count; } } }); export default store;
//全局触发事件 export default { increment (state){ // 只有经过此处的方法才能改变vuex 内的数据 state.count++; }, decrement (state){ state.count--; }, }
import {mapState,mapMutations,mapGetters} from 'vuex'
this.$store
对象进行操做/static/[filename]
'./static/img/loading.gif'
(我也不知道缘由)