Skip to content

Commit

Permalink
collect
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufengzhufeng committed Jul 13, 2017
1 parent 395dc75 commit 533b6b9
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion vue-book/vue-webpack/build/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function resolve (dir) {

module.exports = {
entry: {
app: './src-vuex/main.js'
app: './src-book/main.js'
},
output: {
path: config.build.assetsRoot,
Expand Down
3 changes: 2 additions & 1 deletion vue-book/vue-webpack/src-book/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<template>
<div id="app">
<transition name="translate">
<router-view class="pos"></router-view>
<!--增加缓存 -->
<router-view class="pos"></router-view>
</transition>
<tab></tab>
</div>
Expand Down
15 changes: 11 additions & 4 deletions vue-book/vue-webpack/src-book/containers/Collect.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<template>
<div>
collect
<div class="page">
<MHeader title="收藏列表"></MHeader>
<div class="content-scroll">
{{collect}}
</div>
</div>
</template>
<script>
import {mapState} from 'vuex';
import MHeader from '../components/MHeader.vue'
export default {
data(){
return {}
},
computed: {},
components: {},
computed: {
...mapState(['collect'])
},
components: {MHeader},
methods: {}
}
</script>
Expand Down
1 change: 1 addition & 0 deletions vue-book/vue-webpack/src-book/containers/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<div>
<h3>{{book.bookName}}</h3>
<p>{{book.bookInfo}}</p>
<span><i class="iconfont icon-shoucang"></i>收藏</span>
</div>
</li>
</ul>
Expand Down
5 changes: 3 additions & 2 deletions vue-book/vue-webpack/src-book/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Vue.use(VueLazyLoad, {
loading: loading,//加载是显示的loding图,提供了一个全局 指令 v-lazy 将真实的路径放到v-lazy上即可
});


import store from './vuex'
new Vue({
el: '#app',
router,
...App
...App,
store
/*template:'<App/>',
components:{App}*/
});
Empty file.
8 changes: 8 additions & 0 deletions vue-book/vue-webpack/src-book/vuex/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

import {state} from './state'
export default new Vuex.Store({
state
});
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions vue-book/vue-webpack/src-book/vuex/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//定义收藏列表
export const state = {
collect:[]
};

17 changes: 14 additions & 3 deletions vue-book/vue-webpack/src-vuex/components/Blue.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<template>
<div>
蓝色 {{$store.state.color}}
蓝色 {{c}}
<button @click="changeColor">变蓝</button>
</div>
</template>
<script>
import {mapState} from 'vuex';
export default {
data(){
return {}
},
computed: {},
computed: {
...mapState({c:'color',a:'a'}),
//...mapState(['color'])
},
components: {},
methods: {}
methods: {
//不允许组件直接更改状态
changeColor(){
this.$store.commit('change','blue')
//this.$store.state.color = 'blue';
}
}
}
</script>
<style scoped>
Expand Down
22 changes: 19 additions & 3 deletions vue-book/vue-webpack/src-vuex/components/Red.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<template>
<div>
红色 {{$store.state.color}}
红色 {{color}} {{a}} {{msg}}
<button @click="changeColor">变红</button>
{{computeColor}}
</div>
</template>
<script>
//辅助函数 简化写法
import {mapState,mapMutations,mapGetters} from 'vuex';
export default {
data(){
return {}
},
computed: {},
//计算属性 this.$store,虽然是属性 但是写法是函数
computed:{
msg(){
return '我很帅'
},
...mapState(['color','a']),
...mapGetters(['computeColor'])
},
components: {},
methods: {}
methods: {
...mapMutations(['change']),
changeColor(){
this.change('red');
}
}
}
</script>
<style scoped>
Expand Down
20 changes: 18 additions & 2 deletions vue-book/vue-webpack/src-vuex/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import App from './App.vue';
//使用vuex插件,帮我们解决跨组件传递数据
import Vuex from 'vuex';
Vue.use(Vuex);
//每一个规则都是一个函数,函数第一个参数就是状态
const mutations = {
change(state,n){
state.color = n;
},
};
const state = { //状态,将所有的状态统一放到state中
color:'red'
color:'red',
a:'1'
};
//计算状态的
const getters = {
computeColor(state){ //根据状态计算新属性
return state.color+'black';
}
};
let store = new Vuex.Store({
state
state,
mutations, //管理状态
getters
});

new Vue({
...App,
store, //会在实例上生成一个属性 this.$store
Expand Down

0 comments on commit 533b6b9

Please sign in to comment.