15.vue动画& vuex
Vue.config.productionTip = false; 是否显示提示信息
import/export
export xxx 必须跟跟对象或者和定义一起
对象: export {xxxx}
定义: export let a = xxx;
export function(){}
引入:
import modA from "./a" 错误
import {a} from "./a"
导出:
a.js
let a = 12;
export a;错误
export {a}
引入:
import modA from "./a" // modA => {a:12}
导出:
a.js
let a = 12;
export default {a}
导出:
export let a = "a";
等价于
let a = "a";
export {a};
引入:
import {a} from "./a"
导出:
let a = "a"
export default {a};
等价于
export default {a:"a"};
引入:
import modA from "./a";
导出
export var a = "a";
export var b = "b";
export var c = "c";
导入
import modA from "./a";错误
import * as modA from "./a";
console.log("modA",modA,modA.a,modA.b,modA.c);
或:
import {a,b,c} from "./a";
console.log("modA",a,b,c);
导出:
let a = "a";
let b = "b";
let c = "c";
export default {a,b,c}
导入
import modA from "./a";
console.log("modA",modA,modA.a,modA.b,modA.c);
export default 导出去的东西
引入的时候可以取任意的名字
++一个模块 只能有一个default++
导出
export default function(){
alert("fn1");
};
export let a = 1;
export let b = 2;
export let c = 3;
引入:
import * as all from "./a";
// 接收所有
console.log("0modA",all,all.default,all.a,all.b,all.c);
import modA from "./a";
//获取的default出去的东西
console.log("1modA",modA);
import {a,b,c} from "./a";
console.log("2modA", a,b,c);
==>合并
import modA,{a,b,c} from "./a";
console.log("2modA",modA, a,b,c);
导出
export default {a:1};
export let b = "b";
引入
import modA from "./a";
console.log("2modA",modA);
setTimeout(()=>{
import("./a").then(res=>{
console.log("then:",res,res.default,res.b);
});
sass/less/css
sass:$
less:@
sass:安装 cnpm i -S sass-loader node-sass
.sass
.scss
less:安装 cnpm i -S less-loader less
样式里面配置 lang="scss/less"
运动/动画
原生写法
两部分:进入enter、离开leave
enter ---->none-->block 显示的过程
leave ---->block-->none 消失的过程
动画原生写法
xxx-enter{} 开始的状态
xxx-enter-active{} 运动形态
xxx-enter-to{} 终点
xxx-leave{} 离开的开始的状态
xxx-leave-active{} 运动形态
xxx-leave-to{} 离开终点
exp1:留言板一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div1{width:200px; height: 200px; background: #ccc;}
.fade-enter{opacity: 0;}
.fade-enter-active{ transition: 1s all ease;}
.fade-enter-to{opacity: 1;}
.fade-leave{opacity: 1;}
.fade-leave-active{transition: 1s all ease;}
.fade-leave-to{opacity: 0;}
</style>
<script src = "vue.js"></script>
<script>
window.onload = function(){
let vm = new Vue({
el:"#app",
data:{
isShow:true,
},
methods:{
showHide(){
this.isShow = !this.isShow;
}
}
})
}
</script>
</head>
<body>
<div id="app">
<input @click="showHide" type="button" value="显示隐藏"/>
<transition name="fade">
<div id="div1" v-show="isShow"></div>
</transition>
</div>
</body>
</html>
使用自定义的动画名称
enter-class = "xxx"
enter-active-class = "xxx"
enter-to-class = "xxx"
leave-class = "xxx"
leave-active-class = "xxx"
leave-to-class = "xxx"
exp2:留言板二
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{marign:0;padding:0;list-style:none;}
ul{width:300px; overflow: hidden; border:1px solid green;}
ul li{ text-indent: 2em; height:50px; line-height: 50px; border-bottom: 1px dashed red;}
ul li:nth-last-child(1) { border-bottom:none;}
.fade-slide-enter{opacity:0;height: 0;}
/*.fade-slide-enter-active{ transition: 1s all ease;}*/
.fade-slide-in-active{ transition: 1s all ease;}
.fade-slide-enter-to{opacity:1;height: 50px;}
.fade-slide-leave{opacity:1;height: 50px;}
/*.fade-slide-leave-active{transition: 1s all ease;}*/
.fade-slide-out-active{transition: 1s all ease;}
.fade-slide-leave-to{opacity:0;height: 0;}
</style>
<script src="vue.js"></script>
<script>
window.onload = function(){
let vm = new Vue({
el:"#app",
data:{
msg:"abc",
arr:["aaa","bbb","ccc"]
},
methods:{
add(){
this.arr.push(this.msg);
},
/*del(index){
this.arr.splice(index,1)
} */
del(item){
this.arr = this.arr.filter(value=>{
return item != value;
});
}
}
});
};
</script>
</head>
<body>
<div id="app">
<input v-model="msg" type="text" value=""/>
<input @click="add" type="button" value="添加"/>
<ul>
<transition-group name="fade-slide"
enter-active-class="fade-slide-in-active"
leave-active-class="fade-slide-out-active"
>
<li v-for="item,index in arr" :key="item">{{item}}<a @click="del(item)" href="javascript:;">删除</a></li>
</transition-group>
</ul>
</div>
</body>
</html>
运动:
transition:只能作用一个元素 要多个元素需要使用 transition-group
<transition/transition-group name="指定动画的名称" enter-active-class="自定义的动画样式名称">
</transition/transition-group>
使用animate.css:
1、animated添加到 transition标签上
<transition enter-active-class="animated xxx">
<div>动画的元素</div>
</transition>
2、animated添加到 transition动画的元素上
<transition>
<div class="animated"></div>
</transition>
exp3:留言板三
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<link rel="stylesheet" href="animate.css"/>
<style>
*{margin:0;padding:0;list-style:none;}
ul{width:300px; overflow: hidden; border:1px solid green;}
ul li{ text-indent: 2em; height:50px; line-height: 50px; border-bottom: 1px dashed red;}
ul li:nth-last-child(1) { border-bottom:none;}
</style>
<script src="vue.js"></script>
<script>
window.onload = function(){
let vm = new Vue({
el:"#app",
data:{
msg:"abc",
arr:["aaa","bbb","ccc"]
},
methods:{
add(){
this.arr.push(this.msg);
},
del(item){
this.arr = this.arr.filter(value=>{
return item != value;
});
}
}
});
};
</script>
</head>
<body>
<div id="app">
<input v-model="msg" type="text" value=""/>
<input @click="add" type="button" value="添加"/>
<ul>
<transition-group
enter-active-class="animated rollIn"
leave-active-class="animated rollOut"
>
<li v-for="item,index in arr" :key="item">{{item}}<a @click="del(item)" href="javascript:;">删除</a></li>
</transition-group>
</ul>
</div>
</body>
</html>
vue2-animate
cnpm i vue2-animate
vue-cli:引入外部文件
1、index.html页面安装传统的引入文件的方式进行引入
2、在入口文件main.js 通过import 直接导入
import "vue2-animate/dist/vue2-animate";
vuex
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化
记住一句话:如果要修改state里面的值,只能通过mutaions进行修改!!!
https://vuex.vuejs.org/zh/guide/
1、选安装
cnpm i -S vuex
2、导入
import Vuex from "vuex";
3、创建存储空间
let store = new Vuex.Store({
strict:true,严格模式
getters:{},
mutations:{},
actions:{},
modules:{}
});
每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。Vuex 和单纯的全局对象有以下两点不同:
Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。
你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化,从而让我们能够实现一些工具帮助我们更好地了解我们的应用
4、注入实例Vue
new Vue({
store
})
通信方式:
组件--->store
0、this.$store.state.count = xxx; 不推荐的 错误方式
1、this.$store.commit("mutation",value);
2、this.$store.dispatch("action",value);
store-->store
action--->mutation
ctx.commit("mutation",value);
ctx.dispatch("action",value);
action/mutation区别
mutation只能做同步操作不能做异步!
所有数据修改只能通过mutation进行,
action不能直接修改state值
getters:简单的认为就是获取数据的一个计算属性
使用的时候不需要带括号: this.$store.getters.方法名称
exp:
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
strict:true,
state:{
count:1,
},
getters:{
filterCount(state){
return state.count >= 20? "20":state.count;
}
},
mutations:{//只能做同步操作
plusMutation(state,payload){//value payload有效载荷
state.count++;
},
minusMutation(state,payload){//value payload有效载荷
state.count--;
}
},
actions:{//执行同步和异步操作
plusAction({commit,dispatch},payload){//ctx容器 payload有效载荷
commit("plusMutation");
},
minusAction({commit},payload){
commit("minusMutation");
}
}
});
模块:多人开发,命名冲突
1、模块之间的数据访问,是区分模块
this.$store.state.count
this.$store.state.modA.count
2、方法不区分模块
this.$store.commit("mutation",value);
如果要区分模块 需要加上命名空间
在子模块中添加 namespaced: true,
调用子模块的方法
this.$store.commit("modA/mutation",value);
功能函数
mapState/mapGetters/mapActions/mapMutations
... 解构赋值
store.vue
<template>
<div>
<input @click="minus" type="button" value="减"/><br />
{{count}}------{{filterCount}} <br />
<input @click="plus" type="button" value="加"/>
</div>
</template>
<script>
import {mapState,mapGetters,mapActions,mapMutations} from "vuex";
console.log("mapState:",mapState({count:"count"}));
console.log("mapGetters:",mapGetters({filterCount:"filterCount"}));
console.log("mapActions:",mapActions({minus:"minusAction"}));
console.log("mapMutations:",mapMutations({plus:"plusMutation"}));
export default {
name: "Store",
data(){
return{
}
},
computed:{
...mapState({count:"count"}),
...mapGetters({filterCount:"filterCount"})
},
methods:{
...mapActions({minus:"minusAction"}),
...mapMutations({plus:"plusMutation"})
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
store.js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
strict:true,
state:{
count:1,
},
getters:{
filterCount(state){
return state.count >= 20? "20":state.count;
}
},
mutations:{//只能做同步操作
plusMutation(state,payload)st{//value payload有效载荷
state.count++;
},
minusMutation(state,payload){//value payload有效载荷
state.count--;
}
},
actions:{//执行同步和异步操作
plusAction({commit,dispatch},payload){//ctx容器 payload有效载荷
commit("plusMutation");
},
minusAction({commit},payload){
commit("minusMutation");
}
}
});
混入 (mixins)
mixins是一种分发 Vue 组件中可复用功能的非常灵活的方式。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。
<template>
<div>
<input @click="minus" type="button" value="减"/><br />
{{count}}------{{filterCount}} <br />
<input @click="plus" type="button" value="加"/>
</div>
</template>
<script>
import {mapState,mapGetters,mapActions,mapMutations} from "vuex";
console.log("mapState:",mapState({count:"count"}));
console.log("mapGetters:",mapGetters({filterCount:"filterCount"}));
console.log("mapActions:",mapActions({minus:"minusAction"}));
console.log("mapMutations:",mapMutations({plus:"plusMutation"}));
let stateMixi = {
computed:mapState({count:"count"})
};
let gettersMixi = {
computed:mapGetters({filterCount:"filterCount"})
};
let actionsMixi = {
methods:mapActions({minus:"minusAction"})
};
let mutationsMixi = {
methods:mapMutations({plus:"plusMutation"})
};
export default {
name: "Store",
data(){
return{
}
},
mixins: [stateMixi,gettersMixi,actionsMixi,mutationsMixi]
// computed:mapGetters({filterCount:"filterCount"}),
// methods:mapMutations({plus:"plusMutation"}),
// computed:{
// // count(){
// // return this.$store.state.count;
// // },
// // filterCount(){
// // return this.$store.getters.filterCount;
// // }
// },
// methods:{
// plus(){
// //mutation
// this.$store.commit("plusMutation");
// },
// minus(){
// //action
// this.$store.dispatch("minusAction");
// },
// }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
15.vue动画& vuex的更多相关文章
- 基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
基于vue2.0打造移动商城页面实践 地址:https://www.jianshu.com/p/2129bc4d40e9 vue实现商城购物车功能 地址:http://www.jb51.net/art ...
- webpack4 + vue + vue-router + vuex
ps: 所有案例使用的 node 及 npm 版本如下 node版本: v8.4.0 npm: 5.3.0 下一个案例默认是接着上一个继续写的 建议先熟悉以下文档 vue vue-router vue ...
- 【09】Vue 之 Vuex 数据通信
9.1. 引言 Vue组件化做的确实非常彻底,它独有的vue单文件组件也是做的非常有特色.组件化的同时带来的是:组件之间的数据共享和通信的难题. 尤其Vue组件设计的就是,父组件通过子组件的prop进 ...
- Vue 2.0 + Vue Router + Vuex
用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...
- Vue之Vuex
一.什么是vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.简单来说就是一个数据统一 ...
- vue动画的用法
vue动画 在vue.js中有两种写动画的方法,第一种就是像js里一样,用原生代码来实现,第二种则是使用animate.css的动画类文件,这个动画类和bootstrap.css文件类似,直接调用类就 ...
- requirejs、vue、vuex、vue-route的结合使用,您认为可行吗?
在五一节之前和一网友讨论前端技术时,对方提到vue.vue-route如果配合requirejs应用.当时的我没有想得很明白,也没能这位网友一个准确的回复,但我许诺于他五一研究后给他一个回复.本是一天 ...
- vue动画及其原理
1,vue动画的实现原理,主要是通过在不同时期给需要动画的dom元素加上css动画样式 我们以显示和隐藏动画为例 a, 需要动画的dom元素 b,点击时vue控制往vue中加的样式 2, 我们以两张 ...
- Vue中Vuex的详解与使用(简洁易懂的入门小实例)
怎么安装 Vuex 我就不介绍了,官网上有 就是 npm install xxx 之类的.(其实就是懒~~~哈哈) 那么现在就开始正文部分了 众所周知 Vuex 是什么呢?是用来干嘛的呢? Vuex ...
随机推荐
- Springboot中Aspect实现切面(以记录日志为例)
前言今天我们来说说spring中的切面Aspect,这是Spring的一大优势.面向切面编程往往让我们的开发更加低耦合,也大大减少了代码量,同时呢让我们更专注于业务模块的开发,把那些与业务无关的东西提 ...
- [Web 前端] VML、SVG、Canvas简介
1.VML: VML的全称是Vector Markup Language(矢量可标记语言),矢量的图形,意味着图形可以任意放大缩小而不损失图形的质量,这在制作地图上有很大用途,VML只是被IE支持. ...
- Linux中的普通命令如何以管理员身份运行
set uid, gid, sticky bit 权限 一个文件都有一个所有者, 表示该文件是谁创建的.同时, 该文件还有一个组编号, 表示该文件所属的组, 一般为文件所有者所属的组.如果是一个可执行 ...
- grid - 隐式网格
当网格项目确认在显式网格之外时就会创建隐性网格,当没有足够的空间或者显式的网格轨道来设置网格项目,此时网格项目就会自动创建隐式网格. 隐式网格可以定义:grid-auto-rows.grid-auto ...
- wait-for
Use a tool such as wait-for-it, dockerize, or sh-compatible wait-for. These are small wrapper script ...
- [转] 阿里研究员谷朴:API 设计最佳实践的思考
API是软件系统的核心,而软件系统的复杂度Complexity是大规模软件系统能否成功最重要的因素.但复杂度Complexity并非某一个单独的问题能完全败坏的,而是在系统设计尤其是API设计层面很多 ...
- POSIX 线程清理函数
POSIX 多线程的 cleanup 函数 控制清理函数的函数有两个,一个是 pthread_cleanup_push(), 用来把清理函数压入栈中,另一个是 pthread_cleanup_pop( ...
- CentOS 7.4安装nodejs & nginx & pm2
一.安装nodejs1.查看操作系统信息 uname -a cat /etc/centos-release 2.安装wget yum install wget -y3.安装nodejs 1.下载 wg ...
- Open Graph Protocol(开放内容协议)
最近在整理公司hexo博客的时候突然发现在页面 head 里面有一个这个奇怪的 meta Open Graph Protocol(开放内容协议) 开放内容协议一种新的HTTP头部标记,即这种协议可以让 ...
- 在 WPF 中如何在控件上屏蔽系统默认的触摸长按事件
来源:https://stackoverflow.com/questions/5962108/disable-a-right-click-press-and-hold-in-wpf-applicati ...