• axios安装及使用

    网站文档地址:https://www.kancloud.cn/yunye/axios/234845
    1.npm安装 cnpm install axios
    2.// 在main.js里面引入axios
    import Axios from 'axios'
    3.// 将axios 挂载到Vue原型上,这样全局的组件都有该方法了
    Vue.prototype.$https = Axios;
    4.// Axios全局配置基本url(当然也可以不配置),配置后后面就直接写之后的url即可,会默认帮你拼接
    Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
    methods:{
    // 获取课程分类
    get_category_list(){
    // 调用axios的get方法获取数据
    this.$https.get('course_sub/category/list/')
    // 这里注意this指向
    .then((response) => {
    // 如果状态正常,则赋值给category_list
    if (response.data.error_no === 0){
    this.category_list = response.data.data
    }
    })
    .catch(function (error) {
    console.log(error)
    })
    }
    },
    created(){
    // 调用课程分类方法
    this.get_category_list()
    }
  • vuex安装和简单使用

vuex中,有默认的五种基本的对象:

state:存储状态(变量)
getters:对数据获取之前的再次编译,可以理解为state的计算属性。我们在组件中使用 $sotre.getters.fun()
mutations:修改状态,并且是同步的。在组件中使用$store.commit('',params)。这个和我们组件中的自定义事件类似。
actions:异步操作。在组件中使用是$store.dispath('')
modules:store的子模块,为了开发大型项目,方便状态管理而使用的。
1.安装vuex    npm i vuex -S
2.可以在src目录下创建一个vuex文件夹,建一个store,js文件

store.js文件

import Vue from 'vue'
import Vuex from 'vuex' // 引入vuex并且使用vuex
Vue.use(Vuex) // 存储变量count
const state = {
count:0
} // mutations 里面放置的是我们操作state对象属性的方法,还属于同步操作
const mutations = {
// mutations里面的参数,第一个默认为state,接下来的为自定义参数
addCount(state, n) {
return (state.count += n)
},
reduceCount(state, n){
return (state.count -= n)
}
}; // actions是异步操作,有两个不同的参数,一个是context,它是一个和store对象具有相同对象属性的参数
const actions = {
actionAddCount(context, n){
return context.commit('addCount',n)
},
actionReduceCount(context, n){
return context.commit('reduceCount',n)
},
}; // 通过Vuex的方法Store返回
export default new Vuex.Store({
state,
mutations,
actions
})

main.js 引入并挂载store

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store' // 引入store Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, // 记得挂载
components: { App },
template: '<App/>'
})

HelloWorld.vue里面

<template>
<div class="hello">
<!--获取count的值-->
<h2>{{ $store.state.count }}</h2>
<div>同步操作
<div>
<button @click='addClick(1)'>增加</button>
<button @click='reduceClick(1)'>减少</button>
</div>
</div>
<div>异步操作
<div>
<button @click='actionAddClick(1)'>异步增加</button>
<button @click='actionReduceClick(1)'>异步减少</button>
</div>
</div>
</div>
</template> <script>
export default {
name: 'HelloWorld',
data () {
return {}
},
methods:{
addClick(n){
// 通过commit方法调用addCount来进行加减n操作
this.$store.commit('addCount',n);
},
reduceClick(n){
this.$store.commit('reduceCount',n);
},
actionAddClick(n){
// 通过dispatch方法调用actionAddCount,然后actionAddCount再通过commit方法调用addCount进行加减
this.$store.dispatch('actionAddCount',n);
},
actionReduceClick(n){
this.$store.dispatch('actionReduceCount',n);
},
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

vuex原理图:

最新文章

  1. 使用MyBatis对表执行CRUD操作
  2. 利用ListView的基本方法实现效果
  3. PHP Code Reviewing Learning
  4. php curl破解防盗链
  5. 邮箱password复位图
  6. word加载项打包发布注意事项总结
  7. 基于回调的事件处理——重写onTouchEvent方法响应触摸屏事件
  8. 新概念英语(1-23)Which glasses?
  9. 【原创】大叔经验分享(47)yarn开启日志归集
  10. GlusterFS
  11. 二层协议--MPLS协议总结
  12. SkinTK编译使用
  13. 1-2、LVS之Linux集群系统基础
  14. 【转】Castle Windsor之组件注册
  15. ASP.NET中修改从数据库获取的datatable中的值
  16. CSS 鼠标样式
  17. 微信小程序中用户唯一ID的获取
  18. ssh: Could not resolve hostname问题终于解决了?
  19. Jenkins 通过ssh 拷贝文件到远程机器上。
  20. 关于WebSecurityConfigurerAdapter和ResourceServerConfigurerAdapter源码分析

热门文章

  1. centos启动排障
  2. DP,数论————洛谷P4317 花神的数论题(求1~n二进制中1的个数和)
  3. web安全checklist
  4. CSS - Animate动画
  5. [转帖]差之毫厘谬之千里!带你认识CPU后缀含义
  6. (4.35)sql server清理过期文件【转】
  7. MySql查看时间
  8. OpenVZ平台 Google BBR加速
  9. MySQL之主键
  10. 怎样使用 ssh 命令远程连接服务器?