[TOC]
注意下面都需要引入vuex.js
#### 1.vuex之vuex的使用场景分析与state购物车实例讲解
注意需要引入vuex.js
<div id="hdcms">
<lists></lists>
</div>
<script type="text/x-template" id="Lists">
<div>
<table border="1" width="90%">
<tr><th>编号</th><th>名称</th><th>价格</th></tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
</tr>
</table>
<h1>总价: {{totalPrice}}</h1>
</div>
</script>
<script>
let Lists = {
template: '#Lists',
computed: {
totalPrice(){
return this.$store.state.totalPrice;
},
goods(){
return this.$store.state.goods;
}
}
}
let store = new Vuex.Store({
state: {
totalPrice: 100,
goods: [
{id: 1, title: 'iphone7Plus', price: 399},
{id: 2, title: 'hdcms系统', price: 1999},
]
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists
}
})
</script>
#### 2.vuex之使用getters高效的获取购物车商品总价
<div id="hdcms">
<lists></lists>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>{{v.num}}</td>
<td></td>
</tr>
</table>
<h1>总价: {{totalPrice}}</h1>
</div>
</script>
<script>
let Lists = {
template: '#Lists',
computed: {
totalPrice(){
return this.$store.getters.totalPrice;
},
goods(){
return this.$store.state.goods;
}
}
}
let store = new Vuex.Store({
state: {
goods: [
{id: 1, title: 'iphone7Plus', price: 399, num: 3},
{id: 2, title: 'hdcms系统', price: 1999, num: 6},
]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
}
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists
}
})
</script>
#### 3.vuex之使用getters计算每一件购物车中商品的总价
<div id="hdcms">
<lists></lists>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>{{v.num}}</td>
<td>{{v.totalPrice}}</td>
</tr>
</table>
<h1>总价: {{totalPrice}}</h1>
</div>
</script>
<script>
let Lists = {
template: '#Lists',
computed: {
totalPrice(){
return this.$store.getters.totalPrice;
},
goods(){
return this.$store.getters.goods;
}
}
}
let store = new Vuex.Store({
state: {
goods: [
{id: 1, title: 'iphone7Plus', price: 300, num: 3},
{id: 2, title: 'hdcms系统', price: 1999, num: 6},
]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
//获取商品并计算每件商品的总价
goods(state){
let goods = state.goods;
goods.forEach((v)=>{
v.totalPrice = v.num*v.price;
});
return goods;
}
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists
}
})
</script>
#### 4.vuex之将购物车底部生成新组件用于统计总价
<div id="hdcms">
<lists></lists>
<footer-cart></footer-cart>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>
<input type="text" v-model="v.num">
</td>
<td>{{v.totalPrice}}</td>
</tr>
</table>
</div>
</script>
<script type="text/x-template" id="footerCart">
<div class="footerCart">
总计: {{totalPrice}}
</div>
</script>
<style>
.footerCart{background: #999;color:#fff;}
</style>
<script>
let Lists = {
template: '#Lists',
computed: {
goods(){
return this.$store.getters.goods;
}
}
}
let footerCart={
template:'#footerCart',
computed:{
totalPrice(){
return this.$store.getters.totalPrice;
}
}
}
let store = new Vuex.Store({
state: {
goods: [
{id: 1, title: 'iphone7Plus', price: 300, num: 3},
{id: 2, title: 'hdcms系统', price: 1999, num: 6},
]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
//获取商品并计算每件商品的总价
goods(state){
let goods = state.goods;
goods.forEach((v)=>{
v.totalPrice = v.num*v.price;
});
return goods;
}
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists,footerCart
}
})
</script>
#### 5.vuex之使用mutations修改购物车仓库数据
<div id="hdcms">
<lists></lists>
<footer-cart></footer-cart>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1>
<div v-if="goods.length>0">
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
<th>操作</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>
<input type="text" v-model="v.num">
</td>
<td>{{v.totalPrice}}</td>
<td>
<button @click="del(v.id)">删除</button>
</td>
</tr>
</table>
</div>
</div>
</script>
<script type="text/x-template" id="footerCart">
<div class="footerCart">
<div v-if="totalPrice>0">
总计: {{totalPrice}}
</div>
</div>
</script>
<style>
.footerCart{background: #999;color:#fff;}
</style>
<script>
let Lists = {
template: '#Lists',
computed: {
goods(){
return this.$store.getters.goods;
}
},
methods:{
del(id){
this.$store.commit('del',{id})
}
}
}
let footerCart={
template:'#footerCart',
computed:{
totalPrice(){
return this.$store.getters.totalPrice;
}
}
}
let store = new Vuex.Store({
state: {
goods: [
{id: 1, title: 'iphone7Plus', price: 300, num: 3},
{id: 2, title: 'hdcms系统', price: 1999, num: 6},
]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
//获取商品并计算每件商品的总价
goods(state){
let goods = state.goods;
goods.forEach((v)=>{
v.totalPrice = v.num*v.price;
});
return goods;
}
},
mutations:{
//删除购物车中的商品
del(state,param){
let k;
for(let i=0;i<state.goods.length;i++){
if(state.goods[i].id==param.id){
k=i;break;
}
}
state.goods.splice(k,1);
}
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists,footerCart
}
})
</script>
#### 6.vuex之使用actions与axios异步初始购物车数据
需要引入vuex.js 和axios.js
<div id="hdcms">
<lists></lists>
<footer-cart></footer-cart>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1>
<div v-if="goods.length>0">
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
<th>操作</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>
<input type="text" v-model="v.num">
</td>
<td>{{v.totalPrice}}</td>
<td>
<button @click="del(v.id)">删除</button>
</td>
</tr>
</table>
</div>
</div>
</script>
<script type="text/x-template" id="footerCart">
<div class="footerCart">
<div v-if="totalPrice>0">
总计: {{totalPrice}}
</div>
</div>
</script>
<style>
.footerCart {
background: #999;
color: #fff;
}
</style>
<script>
let Lists = {
template: '#Lists',
computed: {
goods(){
return this.$store.getters.goods;
}
},
methods: {
del(id){
this.$store.commit('del', {id})
}
}
}
let footerCart = {
template: '#footerCart',
computed: {
totalPrice(){
return this.$store.getters.totalPrice;
}
}
}
let store = new Vuex.Store({
state: {
goods: []
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
//获取商品并计算每件商品的总价
goods(state){
let goods = state.goods;
goods.forEach((v) => {
v.totalPrice = v.num * v.price;
});
return goods;
}
},
mutations: {
//删除购物车中的商品
del(state, param){
let k;
for (let i = 0; i < state.goods.length; i++) {
if (state.goods[i].id == param.id) {
k = i;
break;
}
}
state.goods.splice(k, 1);
},
setGoods(state,param){
state.goods=param.goods;
}
},
actions: {
loadGoods(store){
axios.get('73.php').then(function (response) {
store.commit('setGoods',{goods:response.data})
console.log(response);
});
}
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists, footerCart
},
mounted(){
this.$store.dispatch('loadGoods');
}
})
</script>
php代码:
<?php
$data = [
['id' => 1, 'title' => 'iphone7Plus', 'price' => 300, 'num' => 3],
['id' => 2, 'title' => 'hdcms系统', 'price' => 1999, 'num' => 9],
];
echo json_encode($data);
#### 7.vuex之模块化modules开发实例讲解
需要引入vuex.js 和axios.js
当使用命名空间的时候,模块里面的getters,mutations,actions,就不是全局了。
<div id="hdcms">
<lists></lists>
<footer-cart></footer-cart>
</div>
<script type="text/x-template" id="Lists">
<div>
<h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1>
<div v-if="goods.length>0">
<h1>购物车</h1>
<table border="1" width="90%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
<th>操作</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td>
<input type="text" v-model="v.num">
</td>
<td>{{v.totalPrice}}</td>
<td>
<button @click="del(v.id)">删除</button>
</td>
</tr>
</table>
</div>
</div>
</script>
<script type="text/x-template" id="footerCart">
<div class="footerCart">
<div v-if="totalPrice>0">
总计: {{totalPrice}}
</div>
</div>
</script>
<style>
.footerCart {
background: #999;
color: #fff;
}
</style>
<script>
let Lists = {
template: '#Lists',
computed: {
goods(){
// console.log(this.$store.state.cart.goods);
return this.$store.getters['cart/goods']
}
},
methods: {
del(id){
this.$store.commit('del', {id})
}
}
}
let footerCart = {
template: '#footerCart',
computed: {
totalPrice(){
return this.$store.getters['cart/totalPrice'];
}
}
}
const cartModule = {
namespaced:true,
state: {
goods: [{id:12}]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
//获取商品并计算每件商品的总价
goods(state){
let goods = state.goods;
goods.forEach((v) => {
v.totalPrice = v.num * v.price;
});
return goods;
}
},
mutations: {
//删除购物车中的商品
del(state, param){
let k;
for (let i = 0; i < state.goods.length; i++) {
if (state.goods[i].id == param.id) {
k = i;
break;
}
}
state.goods.splice(k, 1);
},
setGoods(state,param){
state.goods=param.goods;
}
},
actions: {
loadGoods(store){
axios.get('73.php').then(function (response) {
store.commit('setGoods',{goods:response.data})
console.log(response);
});
}
}
}
let store = new Vuex.Store({
modules:{
cart:cartModule
}
});
var app = new Vue({
el: '#hdcms',
store,
components: {
Lists, footerCart
},
mounted(){
this.$store.dispatch('cart/loadGoods');
}
})
</script>
- html&jquery网页特效
- 标签分类及特点
- 关于文字标签
- 网页定时跳转
- css透明度和插件
- 0.前端常用工具
- 1.tab切换效果
- 2.tab切换效果多个代码复用
- 3.百度新闻导航条效果
- 4.解决鼠标移入过快的问题
- 5.滚动条位置
- 6.元素尺寸
- 7.全选反选操作
- 8.固定定位
- 9.开关效果
- 10.节点操作
- 11.仿小米商品展示效果
- 12.仿小米商品展示效果复用
- 13.固定导航栏效果
- 14.凡客轮播图效果
- 15.顶部下滑广告效果
- 16.京东左右滑动轮播图
- 17.京东左右滑动无缝轮播图
- 18.选择器
- 19.筛选
- 20.开关效果
- 21.滑动效果
- 22.小米商品效果css实现
- 23.元素水平垂直居中
- laravel5.6
- LARAVEL 介绍&安装
- javascript & css 脚手架
- php常用工具类
- 安装laravel-ide-helper增强代码提示
- 使用migration创建表和数据填充
- 解决mysql5.7以下laravel不能执行数据迁移的问题
- 路由
- 登陆操作自定义模型
- 使用中间件middleware进行登录权限验证
- 进行表单验证处理
- 使用laracasts-flash定制消息提示
- 资源路由
- 宝塔面板安装fileinfo扩展
- laravel上传处理与使用hdjs快速实现前端上传组件
- thinkphp
- phpstorm
- phpstorm安装插件
- 定义快捷键
- 关闭提示
- 将代码实时同步到远程服务器
- sublime
- composer
- git使用
- git安装和配置作者信息
- git新建项目和维护项目
- git日志操作
- git别名操作
- git分支操作
- git生成发布压缩包
- git系统别名
- gitrebase操作
- 使用SSH与GITHUB远程服务器进行无密码连接
- 本地版本库主动使用remote与远程GITHUB进行关联
- 本地分支与GITHUB远程分支同步
- 项目实战-新入职员工参与项目开发时分支使用
- 自动部署
- ios开发
- linux
- 1.centos6.5 mysql忘记登入密码
- html5
- 标签
- 表单
- 音频与视频
- webstorage储存
- canvas
- css3
- 01.CSS3布局
- 02.transition动画
- 03.animation动画
- 04.flex弹性盒模型
- Less
- gulpjs
- es6
- ES6模块化
- let和const命令
- ES6函数扩展&解构赋值
- JavaScript之数据遍历
- class类
- Set和Map数据结构
- Vue
- 1.创建第一个应用
- 2.属性动态绑定
- 3.表达式
- 4.解决phpstorm不识别ECMASCRIPT6语法的问题
- 5.watch监听属性
- 6.使用object与array控制class
- 7.条件渲染
- 8.循环
- 9.变异方法
- 10.事件
- 11.表单
- 12.组件
- 13.css过渡动
- 14.js库控制vue过渡动作
- 15.自定义指令directive
- 16.使用vue-cli初始化单页面应用
- 17.Vue-router路由
- 18.vuex
- 19.vue-cli
- webpack
- zanui
- nodejs