總結一下寫 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'
(我也不知道緣由)