<template>
<div id="app">
<router-link to="/home">首页</router-link>
<router-link to="/news">新闻</router-link>
<router-link to="/user">用户</router-link>
<router-view />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style lang='scss' scoped>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 在vuex中用于存储数据
var state = {
count: 1
}
// mutations里是放方法,主要改变state里的数
var mutations = {
incCount () {
state.count++
}
}
// 实例化vuex的store
const store = new Vuex.Store({
state,
mutations
})
export default store
<template>
<!-- 所有的内容要被根节点包含起来 -->
<div id="home">
我是首页组件
<button @click="goNews()">通过js跳转到新闻页面</button>
<mt-button type="default">default</mt-button>
<br>
<mt-button type="primary">primary</mt-button>
<mt-index-list>
<mt-index-section index="A">
<mt-cell title="Aaron"></mt-cell>
<mt-cell title="Alden"></mt-cell>
<mt-cell title="Austin"></mt-cell>
</mt-index-section>
<mt-index-section index="B">
<mt-cell title="Baldwin"></mt-cell>
<mt-cell title="Braden"></mt-cell>
</mt-index-section>
<mt-index-section index="C">
<mt-cell title="ccc"></mt-cell>
<mt-cell title="ccccc"></mt-cell>
</mt-index-section>
<mt-index-section index="D">
<mt-cell title="DDD"></mt-cell>
<mt-cell title="DDDD"></mt-cell>
</mt-index-section>
<mt-index-section index="Z">
<mt-cell title="Zack"></mt-cell>
<mt-cell title="Zane"></mt-cell>
</mt-index-section>
</mt-index-list>
<hr>
<h2>调用store里的数据{{this.$store.state.count}}</h2>
<button @click="changeCount()">改变store里的值</button>
</div>
</template>
<script>
// 引入store
import store from '../Vuex/store'
export default {
data () {
return {
msg: '我是一个home组件'
}
},
// 注册store
store,
methods: {
goNews () {
this.$router.push({ name: 'news' })
},
changeCount () {
// 改变vuex store里的值
// 触发vuex store 里的mutations里的incCount方法
this.$store.commit('incCount')
}
}
}
</script>
<style lang="scss" scoped>
</style>
<template>
<div id="news">
我是用户组件
<br>
<v-actionsheet></v-actionsheet>
<br>
<mt-button @click.native="flag = true" size="large">选择用户头像</mt-button>
<mt-actionsheet :actions="actions" v-model="flag"></mt-actionsheet>
<hr>
<h2>调用store里的数据{{this.$store.state.count}}</h2>
<button @click="changeCount()">改变store里的值</button>
</div>
</template>
<script>
import store from '../Vuex/store'
import ActionSheet from './ActionSheet.vue'
export default {
data () {
return {
msg: '我是一个新闻组件',
list: [],
flag: false,
actions: []
}
},
// 注册store
store,
components: {
'v-actionsheet': ActionSheet
},
methods: {
takePhoto () {
alert('执行拍照')
},
openAlbum () {
alert('打开相册')
},
changeCount () {
// 改变vuex store里的值
// 触发vuex store 里的mutations里的incCount方法
this.$store.commit('incCount')
}
},
mounted () {
this.actions = [{
name: '拍照',
method: this.takePhoto
}, {
name: '从相册中选择',
method: this.openAlbum
}]
}
}
</script>
<style lang="scss" scoped>
</style>