一,數據共用 1. 安裝: 2. 在src目錄下 新建state文件夾,新建index.js文件 3. 創建一個 store 4.main.js中創建根實例時,傳入store 5.然後子組件使用: 二,數據的修改 1. 給每個城市綁定一個方法: 2. 在Index.js中: 效果: 3. 簡化步驟 ...
一,數據共用
1. 安裝:
npm install vuex --save
2. 在src目錄下 新建state文件夾,新建index.js文件
3. 創建一個 store
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
city: '上海'
}
})
4.main.js中創建根實例時,傳入store
import store from './store'
......
new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
......
5.然後子組件使用:
//獲取城市名
{{this.$store.state.city}}
二,數據的修改
1. 給每個城市綁定一個方法:
@click="HandleCity(城市名)"
HandleCity (value) {
// 派發一個citychange city的 action
this.$store.dispatch('citychanged',value)
}
2. 在Index.js中:
actions: {
//ctx上下文 citychanged (ctx, cityname) { ctx.commit('citychanged', cityname) } }
mutations: {
//改變數據
citychanged (state, city) {
state.city = city
}
}
效果:
3. 簡化步驟
HandleCity (value) { this.$store.commit('citychanged', value) }
index.js中 省略actions:
mutations: { citychanged (state, city) { state.city = city } }
搜索Seatch組件步驟一致
三,使用localstorage存儲當前城市,並返回到城市
每次刷新後,城市預設
localStorage - 用於長久保存整個網站的數據,保存的數據沒有過期時間,直到手動去除。
let defaultCity = '北京' try {
//判斷瀏覽器是否支持 if (localStorage.city) { defaultCity = localStorage.city } } catch (e) {}
//點擊後頁面跳轉 this.$router.push('/')
在 Vue 實例內部,你可以通過 $router
訪問路由實例。因此你可以調用 this.$router.push
四,vuex高級使用
mapState:
import { mapState, mapMutations } from 'vuex'
mapState指將state數據映射到city的計算屬性中
computed: mapState({ current_city: 'city' })
// html寫法:
// {{this.current_city}}
mapMutations :
你可以在組件中使用this.$store.commit('xxx')
提交 mutation,或者使用mapMutations
輔助函數將組件中的 methods 映射為store.commit
調用
HandleCity (value) { this.citychanged(value) this.$router.push('/') }, ...mapMutations([ 'citychanged' ])
mapgetter函數:
mapGetters 輔助函數僅僅是將 store 中的 getter 映射到局部計算屬性: import { mapGetters } from 'vuex' export default { // ... computed: { // 使用對象展開運算符將 getter 混入 computed 對象中 ...mapGetters([ 'doneTodosCount', 'anotherGetter', // ... ]) } } 如果你想將一個 getter 屬性另取一個名字,使用對象形式: mapGetters({ // 把 `this.doneCount` 映射為 `this.$store.getters.doneTodosCount` doneCount: 'doneTodosCount' })
<template> <div class="list" ref="wrapper"> <div> <div class="area"> <div class="title border-topbottom">當前城市</div> <div class="button-list"> <div class="button-wrapper"> <div class="button" ref="mycity">{{this.current_city}}</div> </div> </div> </div> <div class="area"> <div class="title border-topbottom">熱門城市</div> <div class="button-list"> <div class="button-wrapper" v-for="city in hotcities" :key="city.id"> <div class="button" @click="HandleCity(city.name)">{{city.name}}</div> </div> </div> </div> <div class="area" v-for="(city,key) in cities" :key="key" :ref="key"> <div class="title border-topbottom">{{key}}</div> <div class="item-list"> <div class="item border-bottom" v-for="c in city" :key="c.id" @click="HandleCity(c.name)">{{c.name}}</div> </div> </div> </div> </div> </template> <script> import BScroll from 'better-scroll' import { mapState, mapMutations } from 'vuex' export default { name: 'CityList', mounted: function () { this.scroll = new BScroll(this.$refs.wrapper) }, props: ['cities', 'hotcities', 'letter'], methods: { HandleCity (value) { // this.$store.commit('citychanged', value) this.citychanged(value) this.$router.push('/') }, ...mapMutations([ 'citychanged' ]) }, watch: { letter () { if (this.letter) { const element = this.$refs[this.letter][0] this.scroll.scrollToElement(element) } } }, computed: mapState({ current_city: 'city' }) } </script> <style lang="stylus" scoped> @import "~styles/varibles.styl" .border-topbottom &:before border-color #ccc &:after border-color #ccc .border-bottom &:before border-color #ccc .list overflow hidden position absolute top 1.58rem right 0 bottom 0 left 0 .title line-height .54rem padding-left .2rem background #eee color #666 font-size .26rem .button-list padding .1rem .6rem .1rem .1rem overflow hidden .button-wrapper float left padding .1rem .button text-align center margin .1rem border .02rem solid #ccc border-radius .06rem padding .1rem .5rem .item-list .item line-height .76rem padding-left .2rem </style>List.vue
import Vue from 'vue' import Vuex from 'vuex' import state from './state' import mutations from './mutations' Vue.use(Vuex) export default new Vuex.Store({ state: state, // 省略步驟 // 1. this.$store.dispatch('citychanged', value) // 2. actions: { // citychanged (ctx, cityname) { // ctx.commit('citychanged', cityname) // } // }, mutations: mutations })/src/store/index.js
項目地址:
https://github.com/1417766861/Vue2.5-App