Vuex以及axios 看这个
-- 安装
npm i vuex
-- 配置
-- 导入vuex
import vuex from "vuex"
-- vue使用vuex
vue.use(vuex)
-- 实例化仓库
new vuex.Store({
state: {},
getters: {},
mutations: {}
})
-- new Vue({
el: "#app",
store,
})
-- 获取仓库数据
this.$store.state.xxx
this.$store.getters.xxx
-- 改变仓库中的数据
-- 组件向仓库提交修改事件
this.$store.commit("事件名称", data)
--在仓库的mutations中
mutations: {
"事件名称": function(state, data){
修改state中的数据
}
}
-- 注意计算属性
仓库中的数据建议都放在计算属性中
axios
-- 实现ajax技术的工具
-- 配置
-- 下载
npm i axios
-- 导入
import axios from "axios"
-- 在vue的原型链中加入方法
Vue.prototype.$axios = axios
-- 发送请求
this.$axios.request({url,method}).then(function(){}).catch(function(){})
前后端的接通
-- 后端设计一个接口
-- 前端通过axios发送请求拿到数据
-- 跨域问题(只在测试用)
from django.utils.deprecation import MiddlewareMixin
def process_response(self, request, response):
response["Access-Control-Allow-Origin"] = "*"
return response
Vuex简介
vuex是一个专门为Vue.js设计的集中式状态管理架构。
状态? 我们把它理解为在data中需要共享给其他组件使用的部分。
Vuex和单纯的全局对象有以下不同:
1、Vuex 的状态存储是响应式的。当vue组件从store中读取状态的时候,
若store中的状态发生变化,那么相应的组件也会相应的得到高效更新。
2、你不能直接改变store中的状态。改变store中的状态的唯一途径就是显示的
提交(commit)mutation。这样使得我们可以方便的跟踪每一个状态的变化,
从而让我们能够实现一些工具来帮助我们更好的了解我们的应用。
安装使用vuex
-- npm install vuex

// main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import vuex from 'vuex'
Vue.use(vuex)
Vue.config.productionTip = false
const store = new vuex.Store({
state: {
show: false,
}
});
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
});

vuex的使用一

// 为了方便维护,我们通常把在src下面新建一个store文件夹,
// 然后在里面新建一个index.js
import Vue from 'vue'
import Vue_x from "vuex"
Vue.use(Vue_x);
export default new Vue_x.Store({
state: {
show: false,
},
});
// 那么main.js要改成
import Vue from 'vue'
import App from './App'
import router from './router'
import store from "./store"
Vue.config.productionTip = false;
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
});

vuex的使用二
State
简而言之~~state是保存我们data中需要共享的数据。
由于Vuex的存储是响应式的,从store实例中读取状态的最简单的方式就是在计算属性中返回某个状态。
this.$store.state.count

// 创建一个组件
const Counter = {
template: `<div>{{ count }}</div>`,
computed: {
count(){
return this.$store.state.count
}
}
};

组件中获取vuex中状态
Getter
有时候我们需要从store中的state中派生出一些状态,例如对数据进行简单的计算。
并且很多组件都需要用到此方法,我们要么复制这个函数,要么抽取到一个公共函数,多处导入。
我们vuex提供了更加方便的方法,getter ,它就像计算属性一样,getter的返回值会根据它的依赖被
缓存起来,只有它的依赖发生改变时,才会重新计算。
Getter会接收state作为其第一个参数:

import Vue from 'vue'
import Vue_x from "vuex"
Vue.use(Vue_x);
export default new Vue_x.Store({
state: {
count: 20,
},
// 通过 this.$store.getters.my_func
getters: {
my_func: function (state) {
return state.count * 2
}
},
});

Getter使用
Getter也可以接收getters为第二个参数:

import Vue from 'vue'
import Vue_x from "vuex"
Vue.use(Vue_x);
export default new Vue_x.Store({
state: {
count: 20,
},
// 通过 this.$store.getters.my_func
getters: {
my_func: function (state) {
return state.count * 2
},
// 通过 this.$store.getters.my_func_count
my_func_count: function (state, getters) {
return getters.my_func.length
}
},
});

Getter使用
Mutation
更改Vuex中的store中的状态的唯一方法是提交mutation。
每个mutation都有一个字符串的事件类型(type),和一个回调函数handler。
也就是说我们要触发mutation中定义的方法(type),然后才会执行这个方法(handler)。
这个方法就是我们更改状态的地方,它会接收state为第一个参数,后面接收其他参数:

import Vue from 'vue'
import Vue_x from "vuex"
Vue.use(Vue_x);
export default new Vue_x.Store({
state: {
count: 20,
},
// 需要通过 this.$store.commit('increment', 10)
mutations: {
increment (state, n) {
// 变更状态
state.count += n
}
}
});

Mutation基本使用
Mutation需要遵守Vue的响应规则
既然vuex中的store中的状态是响应式的,那么当我们状态变更时,监视状态的vue组件也会更新。
这就意味着vuex中的mutation也需要与使用vue一样遵守一些注意事项:
-- 1,最好提前在你的store中初始化好所有的所需要的属性
-- 2,当对象需要添加属性时,你应该使用
-- Vue.set(obj, 'newProp', 123)
-- 以新对象代替老对象 state.obj = { ...state.obj, newProp: 123}
axios的简单使用
基于Promise的HTTP请求客户端,可以同时在浏览器和node.js使用。
使用npm安装axios
-- npm install axios -D
基本的配置

// main.js
import axios from "axios"
Vue.prototype.$axios = axios
// 组件中
methods: {
init () {
this.$axios({
method: "get",
url: "/user"
})
},
};

axios的基本配置
基本的使用

test(){
this.$axios.get(this.$store.state.apiList.course,{
params: {
id: 123,
}
}).then(function (response) {
// 请求成功回调函数
}).catch(function (response) {
// 请求失败的回调函数
})
}

get请求

test(){
this.$axios.post(this.$store.state.apiList.course,{
course_title: "Python",
course_price: "19.88"
}).then(function (response) {
// 请求成功回调函数
}).catch(function (response) {
// 请求失败的回调函数
})
}

post请求

function getCourse(){
return this.$axios.get('/course/12')
}
function getCourse_all() {
return this.$axios.get('/course')
}
this.$axios.all([getCourse_all(),getCourse()])
.then().catch()

发送多个并发请求

methods: {
init(){
var that = this
this.$axios.request({
url: that.$store.state.apiList.course,
method: 'get'
}).then(function (data) {
if (data.status === 200){
that.courseList = data.data
}
}).catch(function (reason) {
console.log(reason)
})
}
},

Vuex以及axios 看这个的更多相关文章
- 基于Vue+Vuex+Vue-Router+axios+mint-ui的移动端电商项目
第一步:安装Node 1.打开NodeJS的官网,下载和自己系统相配的NodeJS的安装程序,包括32位还是64位一定要选择好,否则会出现安装问题. 下载地址:https://nodejs.org/e ...
- Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...
- vue全家桶router、vuex、axios
main.js import Vue from 'vue' import App from './App' import router from './router' import store fro ...
- 使用vue2.x+webpack+vuex+sass+axios+elementUI等快速搭建前端项目框架
一.本文将分享如何快速搭起基于webpack+vue的前端项目框架,利用vue的自己的脚手架工具vue-cli搭建起基本的环境配置,再通过npm包管理工具引入相应的依赖来完善项目的各种依赖框架.下面是 ...
- 已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用
github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入 vue-cli-project 已构建配置好的vuejs全家桶项目,统一管 ...
- Vuex、axios、跨域请求处理和import/export的注意问题
一.Vuex 1.介绍 vuex是一个专门为Vue.js设计的集中式状态管理架构. 对于状态,我们把它理解为在data中需要共享给其他组件使用的部分数据. Vuex和单纯的全局对象有以下不同: 1. ...
- Vuex、axios以及跨域请求处理
一.Vuex 1.介绍 vuex是一个专门为Vue.js设计的集中式状态管理架构. 对于状态,我们把它理解为在data中需要共享给其他组件使用的部分数据. Vuex和单纯的全局对象有以下不同: 1. ...
- Vuex以及axios
Vuex 简介 vuex是一个专门为Vue.js设计的集中式状态管理架构. 状态? 我们把它理解为在data中需要共享给其他组件使用的部分. Vuex和单纯的全局对象有以下不同: 1.Vuex 的状态 ...
- Vuex与axios介绍
Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...
随机推荐
- TI AM335x Linux MUX hacking
/********************************************************************************************* * TI ...
- Softmax回归介绍
把输入值当成幂指数求值,再正则化这些结果值.这个幂运算表示,更大的证据对应更大的假设模型(hypothesis)里面的乘数权重值.反之,拥有更少的证据意味着在假设模型里面拥有更小的乘数系数.假设模型里 ...
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 【HAOI2010】订货
可以DP也可以是费用流,然而被我用非常简单的DP破了[开心] 原题: 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定 ...
- python 2个dict如何合并
dictMerged2 = dict( dict1, **dict2 ) 这种效率比较高 refer to: http://www.pythoner.com/13.html
- C语言屏幕打印,再删除打印的内容
在做项目的时候,用到了命令行模式进行监听数据,并且是多线程的.因为程序大部分时间都只是在监听状态,容易给人假死的信息.所以单独使用一个进行在屏幕上打印省略号(.......),然后再把打印的省略号(. ...
- BAT 批处理脚本教程
第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令统称批处理命令.小知识:可以 ...
- 【转】Ubuntu添加PATH环境变量
原文网址:http://www.cnblogs.com/pang123hui/archive/2011/05/28/2309889.html 添加分两种: 一.临时性添加 ~$ echo $PATH ...
- bzoj 3622 已经没有什么好害怕的了——二项式反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3622 令 f[i] 表示钦定 i 对 a[ ]>b[ ] 的关系的方案数:g[i] 表 ...
- 深入理解ASP.NET MVC(4)
系列目录 DataTokens和Areas机制 到目前为止Route对象只剩下DataTokens属性没有涉及,事实上这个Areas机制的核心. DataTokens实际上也是一个RouteValue ...