import Vue from ‘vue‘
import App from ‘./App‘
import Vuex from ‘vuex‘
import store from ‘./vuex/store‘
Vue.use(Vuex)
3 src 目录下创建一个 vuex 目录,将 store.js 放到 vuex 目录下
import Vue from ‘vue‘
import Vuex from ‘vuex‘
Vue.use(Vuex)
const store = new Vuex.Store({
// 定义状态
state: {
animateName:‘slide-left‘
},
mutations:{
newAnimateName(state,val){
state.animateName=val
}
}
})
export default store
三、 配置
1 、 router目录> index.js
import ac2 from ‘@/components/aaachild2‘
Router.prototype.go = function (a) {
sessionStorage.isBack = true
if(a){
window.history.go(a)
}else{
window.history.go(-1)
}
}
window.addEventListener("popstate", function(e) { //×××返回键
sessionStorage.isBack = true
}, false);
Vue.use(Router)
2、 app.vue 中
<template>
<div id="app">
<!--<img src="./assets/logo.png">-->
<transition :name="transitionName" >
<router-view class="child-view" ></router-view>
</transition>
</div>
</template>
<script>
export default {
name: ‘App‘,
data () {
return {
}
},
computed: {
transitionName () { //实时获取vuex 改变的类名
return this.$store.state.animateName
}
},
watch: {
‘$route‘ (to, from) {
// 如果isBack为true时,证明是用户点击了回退,执行slide-right动画
let isBack = eval(sessionStorage.isBack)
if (isBack) {
this.$store.commit(‘newAnimateName‘, ‘slide-right‘)
} else {
this.$store.commit(‘newAnimateName‘, ‘ slide-left‘)
}
// 做完回退动画后,要设置成前进动画,否则下次打开页面动画将还是回退
sessionStorage.isBack = false
}
},
}
</script>
<style>
*{
margin: 0 ;
padding: 0;
}
body,html{
width: 100%;
height: 100%;
}
#app {
font-family: ‘Avenir‘, Helvetica, Arial, sans-serif;
width: 100%;
height: 100%;
text-align: center;
max-width: 750px;
margin: 0 auto;
overflow-x: hidden;
position: relative;
}
.child-view {
transition:all .3s ease;
-webkit-transition:all .3s ease;
position: absolute;
width: 100%;
}
.slide-left-enter,.slide-right-leave-active {
opacity:0;
-webkit-transform:translate(100%,0);
transform:translate(100%,0);
}
.slide-left-leave-active,.slide-right-enter {
opacity:0;
-webkit-transform:translate(-100%,0);
transform:translate(-100%,0);
}
/* .slide-left-enter-active {
transition-delay: 0.1s;
}*/
</style>
子组件嵌套使用 时,入a.vue 中嵌套
<transition :name="transitionName" >
<router-view class="child-view" ></router-view>
同时a中使用计算属性
computed: {
transitionName () {
return this.$store.state.animateName
}
},
可以在vuex中使用多个 类名及动画 给不同的组件使用(猜的)
原文地址:http://blog.51cto.com/521kun/2118020