Vue学习六之axios、vuex、脚手架中组件传值

 

本节目录

一 axios的使用

  Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中,promise是es6里面的用法。

  axios能做的事情:官网地址

  1. 从浏览器中创建 XMLHttpRequests
  2. node.js 创建 http 请求
  3. 支持 Promise API
  4. 拦截请求和响应
  5. 转换请求数据和响应数据
  6. 取消请求
  7. 自动转换 JSON 数据
  8. 客户端支持防御 XSRF

  我们先看一下promise对象,看文档

    

  axios是基于promise对象的,axios的用法类似于jQuery的链式用法,就是.then().catch().then().catch()连着用等等,每个方法都相当于给你返回了一个promise对象,接着可以调用这个对象的其他方法

  好,那么首先现在安装一下axios,两种方式:

  1. 直接下载安装:
    $ npm install axios //在咱们模块化开发的项目中,一定要使用这个来下载
  2.  
  3. cdn的方式引入:
  4. <script src="https://unpkg.com/axios/dist/axios.min.js"></script> //这个做简单的演示的时候可以用

  其实用法官网写的很详细了,这里你可以去直接看官网,我这里就简单看一下get请求和post请求的使用方法:

  get请求的写法:

  1. // 为给定 ID 的 user 创建请求
  2. axios.get('/user?ID=12345') //get方法then方法等都给你返回了一个promise对象,接着可以调用这个对象的方法
  3. .then(function (response) { //response请求成功接收的数据
  4. console.log(response);
  5. })
  6. .catch(function (error) { //error请求错误捕获的异常
  7. console.log(error);
  8. });
  9.  
  10. // 可选地,上面的请求可以这样做
  11. axios.get('/user', { //请求的查询参数还可以这样写
  12. params: {
  13. ID: 12345
  14. }
  15. })
  16. .then(function (response) {
  17. console.log(response);
  18. })
  19. .catch(function (error) {
  20. console.log(error);
  21. });

  post请求的写法:

  1. axios.post('/user', { //后面这个大括号里面写请求体里面的请求数据,就是jQuery的ajax里面的data属性
  2. firstName: 'Fred',
  3. lastName: 'Flintstone'
  4. })
  5. .then(function (response) {
  6. console.log(response);
  7. })
  8. .catch(function (error) {
  9. console.log(error);
  10. });

  好,接下来在们上篇博客最后通过webpack做的那个项目来演示一下axios的使用,首先在我们的项目目录下执行下载安装axios的指令:

  1. npm install axios -S //为什么要下载项目下呢,因为将来打包项目推送到线上的时候,打包我们的项目,全局的那些环境你是打包不上的,你线上环境里面可能没有我们下载的这些环境或者说工具,所以直接在我们的项目环境中下载,打包的时候一同打包给线上

  那么接下来axios这个东西怎么用呢,我们是不是可能在好多个组件中都要发送请求啊,首先看一下我们开发时的结构:

    

  那这个怎么玩呢,原型链技术:

    vue对象实例在我们的mian.js里面引用的,那么我们就在main.js里面引入axios,并且封装给vue实例,那么其他组件就能用了(继承),在mian.js里面加上下面两句:

  1. import Axios from 'axios'
  2. Vue.prototype.$http = Axios; //Vue.prototype.名字(这个名字随便起,一般是叫$http或者$https,那么一看就明白,你这是在往后端发送请求)

    看mian.js代码:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6.  
  7. //引入axios,封装axios,注意不用使用Vue.use()昂,人家官网也没说让你用,用use的一般是在vue的基础上封装的一些框架或者模块,但是这个axios不是vue的,是一个新的基于es6的http库,在哪个语言里面都可以用这个库,它是一个独立的内容,vue里面一般不用jQuery的ajax,人间jQuery也是前端的一个框架可以这么说,人家vue也是前端的框架,两者做的事情差不多,人家不想用jQuery的
  8. import Axios from 'axios'
  9. Vue.prototype.$http = Axios; //Vue.prototype.名字(这个名字随便起,一般是叫$http或者$https,那么一看就明白,你这是在往后端发送请求)
  10.  
  11. import ElementUI from 'element-ui';
  12. import 'element-ui/lib/theme-chalk/index.css';
  13. Vue.use(ElementUI);
  14. import '../static/global/index.css'
  15.  
  16. Vue.config.productionTip = false
  17.  
  18. /* eslint-disable no-new */
  19. new Vue({
  20. el: '#app',
  21. router,
  22. components: { App },
  23. template: '<App/>'
  24. })

  然后我们启动我们的项目,执行npm run dev,然后在我们的组件中使用一下axios,我在我们之前创建的那个Course.vue组件中使用了一下,也算是写一下前面布置的那个练习吧,大家看看代码,看看你能看懂不:

  1. <template>
  2. <!--<div>-->
  3. <!--这是Course页面-->
  4. <!--</div>-->
  5.  
  6. <div>
  7. <div class="categorylist">
  8. <span v-for="(item,index) in categoryList" :key="item.id" :class="{active:currentIndex===index}" @click="clickCategoryHandler(index,item.id)">{{ item.name }}</span>
  9. </div>
  10.  
  11. <div class="course">
  12. <ul>
  13. <li v-for="(course,index) in courseList" :key="course.id">
  14. <h3>{{ course.name }}</h3> <!-- 这里就拿个name的值演示一下效果 -->
  15. </li>
  16. </ul>
  17. </div>
  18.  
  19. </div>
  20.  
  21. </template>
  22.  
  23. <script>
  24. export default {
  25. name: 'Course',
  26. data(){
  27. return{
  28. categoryList:[],//用来接收请求回来的数据,一般都和我们后端返回回来的数据的数据类型对应好,所以我写了个空列表(空数组),那么在使用这个数据属性的地方就变成了你请求回来的数据
  29. currentIndex:0, //为了让我们上面渲染的第一个span标签有个默认样式
  30. courseList:[], //存放免费课程页里面的课程分类标签页对应的数据
  31. courseId:0, //用来记录用户点击的哪个课程标签,根据这个id动态的往后端请求数据
  32. }
  33. },
  34. //发送请求获取这个组件的数据,我们随便找个网站,找个数据接口昂,比如这里用的是https://www.luffycity.com/courses这个网址里面的组件的数据接口,我们访问这个网址,你在浏览器调试台的network的地方能看到,它发送了两个请求,一个是请求了这个网址https://www.luffycity.com/api/v1/course_sub/category/list/,一个是请求了这个网址https://www.luffycity.com/api/v1/courses/?sub_category=0&ordering=
  35. //好,那么我们就通过axios来请求一下这两个接口的数据,自己写一个getCategoryList方法,封装请求逻辑,在created钩子函数中使用一下,直接在created里面写请求逻辑也是可以的,只不过结构不太清晰而已
  36. //我们发现,我们要请求的这两个url'https://www.luffycity.com/api/v1/courses/?sub_category=0&ordering='和https://www.luffycity.com/api/v1/course_sub/category/list/,你会发现这些url的前面部分https://www.luffycity.com/api/v1/都是一样的,能不能做一个公用的部分存起来啊,省去了很多的代码,好,axios也提供了一个对应的方法defaults.baseURl,怎么用这个东西呢,在我们引入axios的方法配置,也就是在我们的main.js里面配置,看我们的main.js内容
  37. methods:{
  38. //获取标题栏的数据
  39. getCategoryList(){
  40. // this.$https.get('https://www.luffycity.com/api/v1/course_sub/category/list/'),配置了公用的url,所以我们下面就写这个公用url的后面的部分就可以了
  41. // var _this = this; 可以先将this保存下来,下面的方法中也可以不写成箭头函数了,直接使用_this变量就是我们的当前组件对象
  42. this.$https.get('course_sub/category/list/')
  43. .then((response)=>{ //注意我们写箭头函数,因为写普通函数的话,里面使用this的时候,this指向的当前调用该方法的对象,就是axios,而我们像让this指向定义当前对象的父类,也就是我们的当前组件对象,所以要改变一下this的指向,不用箭头函数也能搞定,把this先保存一下,赋值给一个变量
  44. // console.log(response); //响应数据的对象
  45. // console.log(response.data.data); //响应回来的数据
  46. // console.log(response.data.error_no); //响应数据的状态
  47. if (response.data.error_no === 0){
  48. this.categoryList = response.data.data;
  49. let obj = { //因为我们的免费课程里面的子组件的标签页最前面还有个'全部'功能,所以把它加上
  50. id:0,
  51. name:'全部',
  52. category:0,
  53. hide:false,
  54. };
  55. this.categoryList.unshift(obj);//数组头部追加元素
  56. //数据删除任意一个指定的元素,按照索引来删除对应元素,splice(2,2,'a')方法还记得吗,三个参数,第一个2是从索引2开始删,第二个2,是删除几个,第三个'a',第三个参数可以不写,就成了单纯的删除操作,是删除元素的地方替换成'a',突然想起来了,就在这里提一嘴
  57. }
  58. })
  59. .catch((error)=>{
  60. console.log('获取数据失败,错误是:',error);//后端返回的错误
  61. })
  62.  
  63. },
  64. //获取标题栏中免费课程的标签页列表数据
  65. getCourseList(){
  66. // this.$https.get('courses/?sub_category=0&ordering=') 通过this.courseId的值来改变sub_category=0的这个0,来发送动态的请求,获取不同课程标签对应的不同的数据,别忘了用反引号,模板字符串,键盘上tab键上面那个键
  67. this.$https.get(`courses/?sub_category=${this.courseId}&ordering=`)
  68. .then((response)=>{
  69. // console.log(response);
  70. // console.log(response.data);
  71. // console.log(response.data.data);
  72. if (response.data.error_no === 0){
  73. this.courseList = response.data.data;
  74. }
  75. })
  76. .catch((error)=>{
  77. console.log('标题栏中免费课程的标签页列表数据获取失败,错误是:',error)
  78. })
  79. },
  80.  
  81. //点击span标签变css样式的,并且发送另外一个这个标签对应的请求,请求的url是https://www.luffycity.com/api/v1/courses/?sub_category=0&ordering=,这些url的数据是我们点击免费课程那个组件的时候发送的请求,返回给我们的,然后我们渲染到这个我们这里点击的每个span标签的
  82. clickCategoryHandler(val,course_id){
  83. this.currentIndex = val;
  84. this.courseId = course_id; //将用户点击的课程标签的id赋值给我们设置的这个数据属性,将来通过这个数据属性来动态的发送请求,执行下面的方法来发送请求
  85. this.getCourseList(); //根据用户点击的课程标签传过来的courseid的值来动态获取对应的数据
  86.  
  87. }
  88. },
  89. //免费课程页面刚加载完就发送了下面的两个请求,拿到了数据,渲染在页面上了,所以我们通过一个created钩子函数搞一下
  90. created(){
  91. this.getCategoryList();
  92. this.getCourseList();
  93. }
  94. }
  95. </script>
  96.  
  97. <style scoped>
  98. span{
  99. padding: 0 20px;
  100. }
  101. span.active{
  102. color:blue;
  103. }
  104.  
  105. </style>

  里面涉及到了mian.js文件的一个配置,我把main.js的代码也放到这里吧:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7.  
  8. //看这里,看这里!!!
  9. Vue.prototype.$https = Axios; //Vue.prototype.名字(这个名字随便起,一般是叫$http或者$https,那么一看就明白,你这是在往后端发送请求)
  10. //给Axios设置一个将来发送请求时的公用的url,那么将来通过axios请求网址时,就只需要写这个公用url的后面的部分
  11. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/'; //axios官网上有
  12.  
  13. //引入element-ui
  14. import ElementUI from 'element-ui';
  15. //引入element-ui的css样式,这个需要单独引入
  16. import 'element-ui/lib/theme-chalk/index.css';
  17. //给Vue添加一下这个工具
  18. Vue.use(ElementUI);
  19. import '../static/global/index.css'
  20.  
  21. Vue.config.productionTip = false
  22.  
  23. /* eslint-disable no-new */
  24. new Vue({
  25. el: '#app',
  26. router,
  27. components: { App },
  28. template: '<App/>'
  29. })

二 vuex的使用

  

  vuex官网地址:https://vuex.vuejs.org/zh/guide/

  vuex是什么呢?官方说法是这样的:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

  其实vuex简单来讲就是一个临时数据仓库,能够帮我们保存各个组件中的数据,也就是每个组件都能使用这个仓库里面的数据,只要修改了数据仓库里面的数据,使用了仓库中这个数据的组件中的这个数据都会跟着变化,非常适合各组件的一个传值,之前我们使用组件之间的传值都是用的父子组件传值或者平行组件传值的方式,这个vuex帮我们做了一个更方便的数据仓库来实现组件传值和各组件的数据共享,但是小型项目一般用它比较合适,大型项目还是用组件传值的方式,因为这个仓库对于大型项目来讲,比较笨重,共享数据量太大了也会对页面性能有所影响,所以我们今天学的这个vuex不是说必须要用的,还是看需求和业务场景的。

  来看一个图:

    

  通过上面这个图我相信大家应该很清楚vuex是做什么的,好,既然涉及到组件往里面存值、取值、修改值的操作,那么我们就来看看怎么完成这些操作。

  vuex就相当于一个仓库(store),首先我们说一下vuex的五大悍将:

    state:状态,也就是共享数据

    mutations:更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数,但是所有修改数据的操作通过mutations都是同步(串行)操作,目的应该是为了保证数据安全,大家应该能够发现,多个组件可能都会执行修改一个共享数据的操作(异步操作,定时器,ajax都是异步的),那么异步操作共享数据,就容易出现数据混乱的问题,所以凡是通过mutations的数据操作都变成了同步状态。

    actions:Action 类似于 mutation,不同在于:Action 提交的是 mutation,也就是提交的是mutations中的函数(方法),而不是直接变更状态(共享数据)。Action 可以包含任意异步操作,也就是说异步操作可以同时提交给actions,通过dispatch方法,但是action提交数据的操作必须经过mutations,通过commit方法提交给mutations,又变成了同步,为了数据安全可靠不混乱。也就是说操作数据的方式有两个: 1.操作数据--> commit -->mutations(同步)     2.操作数据--> dispatch -->actions(异步)--> commit -->mutations(同步),如果将异步任务直接交给mutations,就会出现数据混乱不可靠的问题。

    getters:相当于store的计算属性

    modules:模块

  

  getters和modules不常用,我们后面的项目也用不到,所以这里就不做详细的解释了,我们只要学习前三个悍将(state、mutations、actions),这些悍将都是写在我们创建的store对象里面的属性,具体他们几个怎么搞,先看图:

    

  首先下载安装,项目目录下在终端执行一下下面的指令:

  1. npm i vuex -S

  由于vuex这个仓库所有的组件都要使用,所以我们需要将它挂载到我们的全局vue实例里面,vue实例我们是在main.js里面引入并创建的,所以我们看main.js代码如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7. Vue.prototype.$https = Axios;
  8. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
  9. import ElementUI from 'element-ui';
  10. import 'element-ui/lib/theme-chalk/index.css';
  11. Vue.use(ElementUI);
  12. import '../static/global/index.css'
  13. Vue.config.productionTip = false;
  14.  
  15. //引入vuex
  16. import Vuex from 'vuex'
  17. Vue.use(Vuex); //必须use一下
  18. //创建vuex的store实例,这个实例就是我们的vuex仓库
  19. const store = new Vuex.Store({
  20. //这里面使用vuex的五大悍将
  21. state:{
  22. num:1, //state状态有个num=1,也就是有个共享数据num=1,组件中使用它
  23. },
  24. mutations:{
  25. //mutations里面的函数第一个参数就是上面这个state属性,其他的参数是调用这个这函数时给传的参数
  26. setNum(state,val){
  27. // console.log(val);
  28. state.num += val;
  29. },
  30. },
  31.  //此时还没有用到actions异步提交仓库的一些数据操作的任务
  32. actions:{
  33. }
  34. });
  35.  
  36. /* eslint-disable no-new */
  37. new Vue({
  38. el: '#app',
  39. router,
  40. store, // 必须将创建的vuex.Store对象挂载到vue实例中,那么相当于给我们的vue对象添加了一个$store属性,将来组件中通过this.$store就能调用这个对象,this虽然是组件对象,但是组件对象都是相当于继承了vue对象,还记得router吗,使用router的时候有两个对象,通过vue对象调用,this.$router,this.$route,和它类似的用法
  41. components: { App },
  42. template: '<App/>'
  43. });

  然后我们创建两个组件,一个叫做Home.vue,一个叫做Son.vue,这个Son组件是Home组件的子组件,我们就玩一下这两个组件对vuex仓库的一个共享数据的一系列操作,下面完成了组件从仓库中取值(必须通过组件的计算属性完成这个事情),还有通过mutations来完成的一个同步方式修改数据的操作,看代码:

  Home.vue文件的代码如下:

  1. <template>
  2. <div>
  3. 这是Home页面
  4. <h1>我是父组件中的 {{ myNum }}</h1>
  5. <Son></Son>
  6. </div>
  7. </template>
  8. <script>
  9. //引入子组件
  10. import Son from '../son/Son'
  11. export default {
  12. name: 'Home',
  13. data() {
  14. return {
  15. }
  16. },
  17. //挂载Son子组件
  18. components:{
  19. Son,
  20. },
  21. //组件中想操作store中的数据,必须通过计算属性来监听vuex的store中的state里面的数据,组件中才能获取到并使用store中的这个数据
  22. computed:{
  23. //在模板语法中使用{{ myNum }},就能用myNum对应函数里面的返回值
  24. myNum:function () {
  25. // console.log(this);//还记得这个this指的是谁吗,就是当前的组件对象,组件是不是都相当于继承于我们的vue对象,而store又挂载到了我们的vue实例上,那么通过组件对象.$store就能获得咱们的vuex仓库store
  26. return this.$store.state.num //获取到我们store中的state里面的数据
  27. }
  28. },
  29. }
  30. </script>
  31.  
  32. <style>
  33. </style>

  Son.vue文件代码如下:我们在子组件中来个button按钮,点击一下这个按钮,就给store仓库中的数据num加1,然后Home组件中使用了这个num地方的num值也自动跟着发生改变,实现了一个父子组件传值的效果。

  1. <template>
  2. <div>
  3. <h2>我是子组件 {{ sonNum }}</h2>
  4. <button @click="addNum">同步修改num+1</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name:'Son',
  10. data(){
  11. return{
  12. }
  13. },
  14. //Son子组件中使用一下store中的数据
  15. computed:{
  16. sonNum:function () {
  17. console.log(this);
  18. return this.$store.state.num
  19. }
  20. },
  21. methods:{
  22. //给button绑定了一个点击事件,这个事件做的事情是点击一下,给store中的state中的num + 1
  23. addNum(){
  24. //不能直接修改store中的数据,这是vuex限定死的,必须通过mutations里面的方法来修改
  25. this.$store.commit('setNum',1) //注意,commit里面的第一个参数是我们在vuex的store对象中的mutations里面定义的方法,第二个参数1就是要传给mutations中的setNum函数的数据,也就是第二个参数val
  26. },
  27.  
  28. }
  29. }
  30.  
  31. </script>
  32.  
  33. <style></style>

  然后我们看页面效果:

    

  这就是我们直接通过mutations中的函数完成的一个同步修改数据的操作,

  下面我们再来一个通过mutations中的函数完成做一个异步修改数据的操作,看一下上面说的会造成数据不可靠的问题的效果:

  首先看main.js的内容如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7. Vue.prototype.$https = Axios;
  8. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
  9. import ElementUI from 'element-ui';
  10. import 'element-ui/lib/theme-chalk/index.css';
  11. Vue.use(ElementUI);
  12. import '../static/global/index.css'
  13. Vue.config.productionTip = false;
  14.  
  15. //引入vuex
  16. import Vuex from 'vuex'
  17. Vue.use(Vuex); //必须use一下
  18. //创建vuex的store实例,这个实例就是我们的vuex仓库
  19. const store = new Vuex.Store({
  20. //这里面使用vuex的五大悍将
  21. state:{
  22. num:1, //state状态有个num=1,也就是有个共享数据num=1,组件中使用它
  23. },
  24. mutations:{//异步数据操作要执行的对应的mutations中的函数
  25. setNumAsync(state,val){
  26. setTimeout(()=>{ //还记得吗,定时器是个异步任务,你页面随便操作,它自己玩自己的
  27. state.num += val;
  28. },1000)
  29. }
  30. },
  31.  
  32. actions:{
  33.  
  34. }
  35. });
  36.  
  37. /* eslint-disable no-new */
  38. new Vue({
  39. el: '#app',
  40. router,
  41. store, // 必须将创建的vuex.Store对象挂载到vue实例中,那么相当于给我们的vue对象添加了一个$store属性,将来组件中通过this.$store就能调用这个对象,this虽然是组件对象,但是组件对象都是相当于继承了vue对象,还记得router吗,使用router的时候有两个对象,通过vue对象调用,this.$router,this.$route,和它类似的用法
  42. components: { App },
  43. template: '<App/>'
  44. });

  Home.vue中的代码没有变化,还是之前的代码,不用改,所以我们直接看Son.vue文件中的代码如下:

  1. <template>
  2. <div>
  3. <h2>我是子组件 {{ sonNum }}</h2>
  4.  
  5. <button @click="addNumAsync">异步修改num+5</button>
  6. </div>
  7. </template>
  8. <script>
  9. // <button @click="addNum">同步修改num+1</button>
  10. export default {
  11. name:'Son',
  12. data(){
  13. return{
  14. }
  15. },
  16. //Son子组件中使用一下store中的数据
  17. computed:{
  18. sonNum:function () {
  19. console.log(this);
  20. return this.$store.state.num
  21. }
  22. },
  23. methods:{
  24. //给button绑定了一个点击事件,这个事件做的事情是点击一下,给store中的state中的num + 1
  25. addNum(){
  26. //不能直接修改store中的数据,这是vuex限定死的,必须通过mutations里面的方法来修改
  27. this.$store.commit('setNum',1) //注意,commit里面的第一个参数是我们在vuex的store对象中的mutations里面定义的方法,第二个参数1就是要传给mutations中的setNum函数的数据,也就是第二个参数val
  28. },
  29. //异步提交数据加1操作
  30. addNumAsync(){
  31. this.$store.commit('setNum',5) //每次都加5
  32. }
  33. }
  34. }
  35.  
  36. </script>
  37.  
  38. <style></style>

  效果如下:

    

  所以,我们提交异步任务操作数据的时候,必须遵循人家vuex说的,异步的任务先提交给actions,再有actions提交给mutations,那么将来,同步的数据操作,我们也是先给actions,再给mutations,在mutations中进行数据操作,看代码,将异步和同步操作放到actions中:

  mian.js文件代码如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7. Vue.prototype.$https = Axios;
  8. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
  9. import ElementUI from 'element-ui';
  10. import 'element-ui/lib/theme-chalk/index.css';
  11. Vue.use(ElementUI);
  12. import '../static/global/index.css'
  13. Vue.config.productionTip = false;
  14.  
  15. //引入vuex
  16. import Vuex from 'vuex'
  17. Vue.use(Vuex); //必须use一下
  18. //创建vuex的store实例,这个实例就是我们的vuex仓库
  19. const store = new Vuex.Store({
  20. //这里面使用vuex的五大悍将
  21. state:{
  22. num:1, //state状态有个num=1,也就是有个共享数据num=1,组件中使用它
  23. },
  24. mutations:{
  25. //mutations里面的函数第一个参数就是上面这个state属性,其他的参数是调用这个这函数时给传的参数
  26. // setNum(state,val){
  27. // // console.log(val);
  28. // state.num += val;
  29. // },
  30. muNum(state,val){
  31. // console.log(val);
  32. state.num += val;
  33. },
  34. //在actions中调用这个方法,还是mutations中这个方法来进行数据操作
  35. muNumAsync(state,val){
  36. state.num += val;
  37. }
  38. },
  39.  
  40. actions:{
  41. //同步数据操作,我们也通过actions来提交给mutations
  42. setNum(context,val){
  43. context.commit('muNum',val);
  44. },
  45. //异步数据操作要执行的对应的actions中的函数,actions中的函数在调用mutations中的函数,actions中的函数的第一个参数是我们的store对象
  46. setNumAsync(context,val){
  47. setTimeout(()=>{ //还记得吗,定时器是个异步任务,你页面随便操作,它自己玩自己的
  48. context.commit('muNumAsync',val);
  49. },1000)
  50. }
  51. }
  52.  
  53. });
  54.  
  55. /* eslint-disable no-new */
  56. new Vue({
  57. el: '#app',
  58. router,
  59. store,
  60. components: { App },
  61. template: '<App/>'
  62. });

  Home.vue组件没有改动,所以我只把改动的Son.vue组件的代码拿出来了,看代码:

  1. <template>
  2. <div>
  3. <h2>我是子组件 {{ sonNum }}</h2>
  4. <button @click="addNum">同步修改num+1</button>
  5. <button @click="addNumAsync">异步修改num+5</button>
  6. </div>
  7. </template>
  8. <script>
  9. // <button @click="addNum">同步修改num+1</button>
  10. export default {
  11. name:'Son',
  12. data(){
  13. return{
  14. }
  15. },
  16. //Son子组件中使用一下store中的数据
  17. computed:{
  18. sonNum:function () {
  19. console.log(this);
  20. return this.$store.state.num
  21. }
  22. },
  23. methods:{
  24. //给button绑定了一个点击事件,这个事件做的事情是点击一下,给store中的state中的num + 1
  25. addNum(){
  26. //同步的数据操作,也通过dispatch方法调用actions中的函数,将数据操作提交给actions中的函数
  27. this.$store.dispatch('setNum',1)
  28. },
  29. //异步提交数据加1操作
  30. addNumAsync(){
  31. //为了必须通过actions来提交数据操作,这里面我们就不能直接commit了,而是用dispatch方法调用actions中的函数,将数据操作提交给actions中的函数
  32. // this.$store.commit('setNum',5)
  33. this.$store.dispatch('setNumAsync',5) //每次都加5,setNumAsync是actions中的函数
  34. }
  35. }
  36. }
  37.  
  38. </script>
  39.  
  40. <style></style>

  然后看页面效果:

    

  

  然后看一下上面代码的逻辑图:

    

  vuex用起来比较麻烦,但是对于组件传值来说方便了一些,哈哈,也没太大感觉昂,没关系,用熟练了就好了,现在再回去看一下vuex的那个图,应该就清晰了。好,这就是我们讲到的vuex仓库store,我们使用vuex来搞一搞异步操作,完成的事情就是点击对应课程,展示课程详细信息:

  先看目录结构:

    

  然后我们直接上代码了

  Coures.vue代码如下:

  1. <template>
  2. <!--<div>-->
  3. <!--这是Course页面-->
  4. <!--</div>-->
  5.  
  6. <div>
  7. <div class="categorylist">
  8. <span v-for="(item,index) in categoryList" :key="item.id" :class="{active:currentIndex===index}" @click="clickCategoryHandler(index,item.id)">{{ item.name }}</span>
  9. </div>
  10.  
  11. <div class="course">
  12. <ul>
  13. <li v-for="(course,index) in courseList" :key="course.id">
  14. <h3 @click="showDetail(course.id)">{{ course.name }}</h3> <!-- 点击它,跳转到对应课程的详情数据页面 -->
  15. </li>
  16. </ul>
  17. </div>
  18.  
  19. </div>
  20.  
  21. </template>
  22.  
  23. <script>
  24. export default {
  25. name: 'Course',
  26. data(){
  27. return{
  28. categoryList:[],//用来接收请求回来的数据,一般都和我们后端返回回来的数据的数据类型对应好,所以我写了个空列表(空数组),那么在使用这个数据属性的地方就变成了你请求回来的数据
  29. currentIndex:0, //为了让我们上面渲染的第一个span标签有个默认样式
  30. courseList:[], //存放免费课程页里面的课程分类标签页对应的数据
  31. courseId:0, //用来记录用户点击的哪个课程标签,根据这个id动态的往后端请求数据
  32. }
  33. },
  34.  
  35. methods:{
  36. getCategoryList(){
  37.  
  38. this.$https.get('course_sub/category/list/')
  39. .then((response)=>{
  40. if (response.data.error_no === 0){
  41. this.categoryList = response.data.data;
  42. let obj = {
  43. id:0,
  44. name:'全部',
  45. category:0,
  46. hide:false,
  47. };
  48. this.categoryList.unshift(obj);//数组头部追加元素
  49.  
  50. }
  51. })
  52. .catch((error)=>{
  53. console.log('获取数据失败,错误是:',error);//后端返回的错误
  54. })
  55.  
  56. },
  57. //获取标题栏中免费课程的标签页列表数据
  58. getCourseList(){
  59. this.$https.get(`courses/?sub_category=${this.courseId}&ordering=`)
  60. .then((response)=>{
  61.  
  62. if (response.data.error_no === 0){
  63. this.courseList = response.data.data;
  64. }
  65. })
  66. .catch((error)=>{
  67. console.log('标题栏中免费课程的标签页列表数据获取失败,错误是:',error)
  68. })
  69. },
  70. //点击span变色,获取对应的数据
  71. clickCategoryHandler(val,course_id){
  72. this.currentIndex = val;
  73. this.courseId = course_id;
  74. this.getCourseList();
  75.  
  76. },
  77.  
  78. //第一步:查看课程详细信息,获取详细信息数据,第二步要配置路由了
  79. showDetail(val){
  80. //跳转路由,加载对应组件
  81. this.$router.push({
  82. name:'coursedetail',
  83. //传的params参数
  84. params:{
  85. cid:val,
  86. }
  87. });
  88.  
  89. }
  90. },
  91.  
  92. created(){
  93. this.getCategoryList();
  94. this.getCourseList();
  95. }
  96. }
  97. </script>
  98.  
  99. <style scoped>
  100. span{
  101. padding: 0 20px;
  102. }
  103. span.active{
  104. color:blue;
  105. }
  106.  
  107. </style>

  路由配置信息index.js文件代码如下:

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. // import HelloWorld from '@/components/HelloWorld' //@还记得吗,表示src文件夹的根路径
  4. //引入组件
  5. import Course from '@/components/Course/Course'
  6. import Home from '@/components/Home/Home'
  7. import CourseDetail from '@/components/CourseDetail/CourseDetail'
  8. // console.log(Course);
  9. //给Vue添加vue-router功能,使用别人提供的功能都要用Vue来use一下
  10. Vue.use(Router)
  11.  
  12. //创建路由对象
  13. export default new Router({
  14. mode:'history', //去掉浏览器地址栏的#号
  15. //配置路由信息
  16. routes: [
  17.  
  18. {
  19. path: '/',
  20. // redirect:'Home' //直接跳转Home名字对应的path路径上
  21. redirect:'/home'
  22. },
  23. {
  24. path: '/home',
  25. name: 'Home',
  26. component: Home
  27. },
  28. {
  29. path: '/course',
  30. name: 'Course',
  31. component: Course
  32. },
  33. //第二步配置路由!
  34. {
  35. path: '/course/:cid/payment_info/', //动态params的动态路由
  36. name: 'coursedetail',
  37. component: CourseDetail
  38. }
  39.  
  40. ]
  41. })

  课程详细信息插件CoureDetail.vue文件代码如下:

  1. <template>
  2. <div>
  3. 我是课程详情组件
  4. <h3>
  5. {{ courseDetailShow }} <!-- 简单展示了详细信息数据中的一个name数据 -->
  6. </h3>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name:'coursedetail',
  12. data(){
  13. return{}
  14. },
  15. //第三步,调用actions中的函数,将courseid作为参数给他
  16. created(){
  17.  
  18. let cid = this.$route.params.cid;
  19. this.$store.dispatch('getDetailAsync',cid);
  20. },
  21. //第七步:在模板中使用vuex仓库中修改后的数据
  22. computed:{
  23. courseDetailShow(){
  24. console.log(this.$store.state.detailList);
  25. return this.$store.state.detailList;
  26. }
  27. }
  28. }
  29.  
  30. </script>
  31. <style></style>

  mian.js代码如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7.  
  8. Vue.prototype.$https = Axios;
  9. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
  10.  
  11. import ElementUI from 'element-ui';
  12. import 'element-ui/lib/theme-chalk/index.css';
  13.  
  14. Vue.use(ElementUI);
  15. import '../static/global/index.css'
  16.  
  17. Vue.config.productionTip = false;
  18.  
  19. //引入vuex
  20. import Vuex from 'vuex'
  21.  
  22. Vue.use(Vuex); //必须use一下
  23. //创建vuex的store实例,这个实例就是我们的vuex仓库
  24. const store = new Vuex.Store({
  25. //这里面使用vuex的五大悍将
  26. state: {
  27. num: 1,
  28. detailList:''
  29. },
  30. mutations: {
  31.  
  32. muNum(state, val) {
  33. // console.log(val);
  34. state.num += val;
  35. },
  36. muNumAsync(state, val) {
  37. state.num += val;
  38. },
  39. //第六步:修改state中的数据
  40. getCourseDetail(state, val) {
  41. // state.detailList = val;
  42. state.detailList = val.data.name; //这里我只是简单的获取了一下详情信息中的name属性的数据
  43. console.log('?????',state.detailList)
  44. }
  45. },
  46.  
  47. actions: {
  48. setNum(context, val) {
  49. context.commit('muNum', val);
  50. },
  51. setNumAsync(context, val) {
  52. setTimeout(() => {
  53. context.commit('muNumAsync', val);
  54. }, 1000)
  55. },
  56. //第四步:提交获取课程详细信息的数据操作
  57. getDetailAsync(context, val) {
  58. Axios.get(`course/${val}/payment_info/`)
  59. .then((response) => {
  60. // console.log(response.data);
  61. //第五步:调用mutations中的方法
  62. context.commit('getCourseDetail', response.data);
  63. })
  64. .catch((error) => {
  65. console.log(error);
  66. });
  67.  
  68. }
  69. }
  70.  
  71. });
  72.  
  73. /* eslint-disable no-new */
  74. new Vue({
  75. el: '#app',
  76. router,
  77. store, // 必须将创建的vuex.Store对象挂载到vue实例中,那么相当于给我们的vue对象添加了一个$store属性,将来组件中通过this.$store就能调用这个对象,this虽然是组件对象,但是组件对象都是相当于继承了vue对象,还记得router吗,使用router的时候有两个对象,通过vue对象调用,this.$router,this.$route,和它类似的用法
  78. components: {App},
  79. template: '<App/>'
  80. });

  代码流程图如下:

    

  再补充一点:

    将Home组件组成全局组件,在main.js中做,看main.js内容:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import Axios from 'axios'
  7.  
  8. Vue.prototype.$https = Axios;
  9. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
  10.  
  11. import ElementUI from 'element-ui';
  12. import 'element-ui/lib/theme-chalk/index.css';
  13.  
  14. Vue.use(ElementUI);
  15. import '../static/global/index.css'
  16.  
  17. Vue.config.productionTip = false;
  18.  
  19. //引入vuex
  20. import Vuex from 'vuex'
  21.  
  22. //将Home组件做成全局组件的方法,看这里看这里!!!!
  23. import Home from './components/Home/Home'
  24. Vue.component(Home.name,Home);
  25.  
  26. Vue.use(Vuex); //必须use一下
  27. //创建vuex的store实例,这个实例就是我们的vuex仓库
  28. const store = new Vuex.Store({
  29. //这里面使用vuex的五大悍将
  30. state: {
  31. num: 1,
  32. detailList:''
  33. },
  34. mutations: {
  35.  
  36. muNum(state, val) {
  37. // console.log(val);
  38. state.num += val;
  39. },
  40. muNumAsync(state, val) {
  41. state.num += val;
  42. },
  43. //第六步:修改state中的数据
  44. getCourseDetail(state, val) {
  45. // state.detailList = val;
  46. state.detailList = val.data.name;
  47. console.log('?????',state.detailList)
  48. }
  49. },
  50.  
  51. actions: {
  52. setNum(context, val) {
  53. context.commit('muNum', val);
  54. },
  55. setNumAsync(context, val) {
  56. setTimeout(() => {
  57. context.commit('muNumAsync', val);
  58. }, 1000)
  59. },
  60. //第四步:提交获取课程详细信息的数据操作
  61. getDetailAsync(context, val) {
  62. Axios.get(`course/${val}/payment_info/`)
  63. .then((response) => {
  64. // console.log(response.data);
  65. //第五步:调用mutations中的方法
  66. context.commit('getCourseDetail', response.data);
  67. })
  68. .catch((error) => {
  69. console.log(error);
  70. });
  71.  
  72. }
  73. }
  74.  
  75. });
  76.  
  77. /* eslint-disable no-new */
  78. new Vue({
  79. el: '#app',
  80. router,
  81. store, // 必须将创建的vuex.Store对象挂载到vue实例中,那么相当于给我们的vue对象添加了一个$store属性,将来组件中通过this.$store就能调用这个对象,this虽然是组件对象,但是组件对象都是相当于继承了vue对象,还记得router吗,使用router的时候有两个对象,通过vue对象调用,this.$router,this.$route,和它类似的用法
  82. components: {App},
  83. template: '<App/>'
  84. });

    在Course.vue组件中使用这个组件

  1. <template>
  2. <!--<div>-->
  3. <!--这是Course页面-->
  4. <!--</div>-->
  5.  
  6. <div>
  7. <div class="categorylist">
  8. <span v-for="(item,index) in categoryList" :key="item.id" :class="{active:currentIndex===index}" @click="clickCategoryHandler(index,item.id)">{{ item.name }}</span>
  9. </div>
  10.  
  11. <div class="course">
  12. <ul>
  13. <li v-for="(course,index) in courseList" :key="course.id">
  14. <h3 @click="showDetail(course.id)">{{ course.name }}</h3> <!-- 点击它,跳转到对应课程的详情数据页面 -->
  15. </li>
  16. </ul>
  17. </div>
  18. <div> <!-- 看这里看这里,全局组件不需要挂载,直接在template中使用 -->
  19. <Home></Home>
  20. </div>
  21. </div>
  22.  
  23. </template>
  24.  
  25. <script>
  26. export default {
  27. name: 'Course',
  28. data(){
  29. return{
  30. categoryList:[],//用来接收请求回来的数据,一般都和我们后端返回回来的数据的数据类型对应好,所以我写了个空列表(空数组),那么在使用这个数据属性的地方就变成了你请求回来的数据
  31. currentIndex:0, //为了让我们上面渲染的第一个span标签有个默认样式
  32. courseList:[], //存放免费课程页里面的课程分类标签页对应的数据
  33. courseId:0, //用来记录用户点击的哪个课程标签,根据这个id动态的往后端请求数据
  34. }
  35. },
  36.  
  37. methods:{
  38. getCategoryList(){
  39.  
  40. this.$https.get('course_sub/category/list/')
  41. .then((response)=>{
  42. if (response.data.error_no === 0){
  43. this.categoryList = response.data.data;
  44. let obj = {
  45. id:0,
  46. name:'全部',
  47. category:0,
  48. hide:false,
  49. };
  50. this.categoryList.unshift(obj);//数组头部追加元素
  51.  
  52. }
  53. })
  54. .catch((error)=>{
  55. console.log('获取数据失败,错误是:',error);//后端返回的错误
  56. })
  57.  
  58. },
  59. //获取标题栏中免费课程的标签页列表数据
  60. getCourseList(){
  61. this.$https.get(`courses/?sub_category=${this.courseId}&ordering=`)
  62. .then((response)=>{
  63.  
  64. if (response.data.error_no === 0){
  65. this.courseList = response.data.data;
  66. }
  67. })
  68. .catch((error)=>{
  69. console.log('标题栏中免费课程的标签页列表数据获取失败,错误是:',error)
  70. })
  71. },
  72. //点击span变色,获取对应的数据
  73. clickCategoryHandler(val,course_id){
  74. this.currentIndex = val;
  75. this.courseId = course_id;
  76. this.getCourseList();
  77.  
  78. },
  79.  
  80. //第一步:查看课程详细信息,获取详细信息数据,第二步要配置路由了
  81. showDetail(val){
  82. //跳转路由,加载对应组件
  83. this.$router.push({
  84. name:'coursedetail',
  85. //传的params参数
  86. params:{
  87. cid:val,
  88. }
  89. });
  90.  
  91. }
  92.  
  93. },
  94.  
  95. created(){
  96. this.getCategoryList();
  97. this.getCourseList();
  98. }
  99. }
  100. </script>
  101.  
  102. <style scoped>
  103. span{
  104. padding: 0 20px;
  105. }
  106. span.active{
  107. color:blue;
  108. }
  109.  
  110. </style>

  最后给大家说一些做单页面应用的问题:

    通过vue这种框架做的单页面应用,如果没有做vue服务器渲染(类似于django的模板渲染),SEO很难搜索到你的网站,爬虫也不能直接在你的页面上爬出数据,但是即便是单页面应用,前端页面的数据不是前端写死的,就是从接口获取的,我们找接口就行了,接口的数据不能完全满足你,你在爬页面上的。

    那SEO问题怎么办,nodejs服务器(前端做),,或者后端服务器配合渲染,前端和后端都要做vue,相互渲染来完成页面解析,让SEO能搜索到你所有的页面,有缺陷,如果在公司写这种单页面应用,一定要注意SEO的问题,那有这么多问题,怎么还用单页应用啊,单页应用可以提高用户体验,防止白屏现象,页面更好维护。

    那么SEO是什么呢,我找了一些资料给大家,看看就明白了,页面越多,百度收录的机会越大

 收录和索引的区别

    解决SEO的大概思路,看图:

      

还有一点跟大家说一下,以后我们通过脚手架的webpack模块创建完项目之后,下面这几个必须要有,没有的自己npm安装一下:

    

三 组件传值

直接看代码吧:

main.js代码如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6.  
  7. Vue.config.productionTip = false;
  8.  
  9. //平行组件传值,通过bus对象,然后我们将bus对象挂载到Vue对象上,那么其他组件通过this.$bus就能用这个公交车对象了
  10. let bus = new Vue();
  11. Vue.prototype.$bus = bus;
  12.  
  13. /* eslint-disable no-new */
  14. new Vue({
  15. el: '#app',
  16. router,
  17. components: { App },
  18. template: '<App/>'
  19. });

App.vue组件代码如下:

  1. <template>
  2. <div id="app">
  3.  
  4. <router-view/>
  5. </div>
  6. </template>
  7.  
  8. <script>
  9. export default {
  10. name: 'App'
  11. }
  12. </script>
  13.  
  14. <style>
  15.  
  16. </style>

Home.vue组件代码如下:

  1. <template>
  2. <div>
  3. 我是Home组件
  4. <!--<Son title="Jaden"></Son> 传静态数据给子组件 -->
  5. <Son :title="title" :msg="msg" @handler="handlerClick"></Son> <!-- 传动态数据给子组件 -->
  6. <button @click="busHandler">通过bus对象传值</button>
  7. </div>
  8. </template>
  9. <script>
  10. import Son from './Son'
  11. export default {
  12. name:'Home',
  13. data(){
  14. return{
  15. title:'test1',
  16. msg:'test2',
  17. }
  18. },
  19. components:{
  20. Son,
  21. },
  22. methods:{
  23. handlerClick(val){
  24. this.msg = val;
  25. },
  26. //平行组件传值
  27. busHandler(){
  28. this.$bus.$emit('homeClick',2);
  29. },
  30. }
  31.  
  32. }
  33.  
  34. </script>
  35. <style></style>

Son.vue组件代码如下,Son.vue组件是Home组件的父级组件,我们除了做了父子、子父组件传值外还做了平行组件传值,都在这几个文件的代码里面了

  1. <template>
  2. <div>
  3. 我是Son组件 {{ msg }} -- {{ title }} -- {{ number }}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name:'Son',
  9. data(){
  10. return{
  11. number:0,
  12. }
  13. },
  14. props:['msg','title'],
  15. created(){
  16. this.$emit('handler',1);
  17. this.$bus.$on('homeClick',(val)=>{
  18. this.number = val;
  19. })
  20. }
  21.  
  22. }
  23.  
  24. </script>
  25. <style></style>

router路由配置信息的index.js内容如下:

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Home from '@/components/Home/Home'
  4.  
  5. Vue.use(Router);
  6.  
  7. export default new Router({
  8. mode:'history',
  9. routes: [
  10. {
  11. path: '/',
  12. redirect:{name:'Home'}
  13. },
  14. {
  15. path: '/',
  16. name: 'Home',
  17. component: Home
  18. }
  19. ]
  20. })

四 xxx

五 xxx

六 xxx

七 xxx

八 xxx

  

回到顶部

 
 

day 84 Vue学习六之axios、vuex、脚手架中组件传值的更多相关文章

  1. Vue学习之--------深入理解Vuex之多组件共享数据(2022/9/4)

    在上篇文章的基础上:Vue学习之--------深入理解Vuex之getters.mapState.mapGetters 1.在state中新增用户数组 2.新增Person.vue组件 提示:这里使 ...

  2. day 87 Vue学习六之axios、vuex、脚手架中组件传值

      本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的使用 Axios 是一个基于 promise 的 HT ...

  3. vue学习六之vuex

    由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex. 什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 ...

  4. day 84 Vue学习四之过滤器、钩子函数、路由、全家桶等

      本节目录 一 vue过滤器 二 生命周期的钩子函数 三 vue的全家桶 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 Vue的过滤器 1 moment.js 在这里我们先介绍一个 ...

  5. vue学习【四】vuex快速入门

    大家好,我是一叶,今天我们继续踩坑.今天的内容是vuex快速入门,页面传值不多的话,不建议vuex,直接props进行父子间传值就行,使用vuex就显得比较臃肿. 我们先预览一下效果,如图1所示. 图 ...

  6. Vue学习之--------深入理解Vuex之模块化编码(2022/9/4)

    在以下文章的基础上 1.深入理解Vuex.原理详解.实战应用:https://blog.csdn.net/weixin_43304253/article/details/126651368 2.深入理 ...

  7. day 84 Vue学习之vue-cli脚手架下载安装及配置

    Vue学习之vue-cli脚手架下载安装及配置   1. 先下载node.js,下载地址:https://nodejs.org/en/download/ 找个目录保存,解压下载的文件,然后配置环境变量 ...

  8. vue 学习五 深入了解components(父子组件之间的传值)

    上一章记录了 如何在父组件中向子组件传值,但在实际应用中,往往子组件也要向父组件中传递数据,那么此时我们应该怎么办呢 1.在父组件内使用v-on监听子组件事件,并在子组件中使用$emit传递数据 // ...

  9. vue中组件传值方式汇总

    在应用复杂时,推荐使用vue官网推荐的vuex,以下讨论简单SPA中的组件间传值. 一.路由传值 路由对象如下图所示: 在跳转页面的时候,在js代码中的操作如下,在标签中使用<router-li ...

随机推荐

  1. Shell case in语句详解

    和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...

  2. delphi RichView的使用介绍

    RichView 组件 由 9 个组件模块组成,分别是: 1.TRVStyle:主要是定义RICHVIEW样式,定义后,其它RIHCVIEW都可以引用此样式.  2.TRichView :主要用于显示 ...

  3. Java——单例模式初步

    1.7 单例模式初步 好书推荐:java与模式 1.7.1 什么是设计模式 设计模式是在大量的实践中总结和理论化之后优选的代码结构.编程风格.以及解决问题的思考方式.设计模式就像是经典的棋谱,不同的棋 ...

  4. SPOJ:[DIVCNT3]Counting Divisors

    题目大意:求1~N的每个数因子数的立方和. 题解:由于N过大,我们不能直接通过线性筛求解.我们可以采用洲阁筛. 洲阁筛的式子可以写成: 对于F(1~√n),可以直接线性筛求解. 对于,我们进行以下DP ...

  5. bzoj1040题解

    [题意分析] 给你一个带权基环树森林,求它的点集的无邻点子集的最大权值和. [解题思路] 对于树的部分,做一遍拓扑排序+递推即可(f[i][j]表示第i个节点选取状态为j(0/1)可以得到的最大权值和 ...

  6. Ngui之UI框架的层级处理

    #region 处理层级问题 void DepthIncrease(UIWndBase uiWnd) { DepthIncrease(uiWnd.transform, UIFlag); } publi ...

  7. 20140321 sizeof 虚函数与虚函数表 静态数组空间 动态数组空间 位字段

    1.静态的数组空间char a[10];sizeof 不能用于1:函数类型 2:动态的数组空间new3:位字段 函数类型:int fun();sizeof(fun())计算的是返回类型的大小,并不是函 ...

  8. eclispe 创建maven 项目:Could not resolve archetype org.apache.maven.archetypes

    昨天新装eclispe 后,创建maven工程后出现 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-q ...

  9. 多线程中Runnable 和Thread关于synchronized的疑点

    学java时和同学碰到的一道题: 转自https://blog.csdn.net/qq_40857349/article/details/102809100 某公司组织年会,会议入场时有两个入口,在入 ...

  10. 【2018ACM/ICPC网络赛】沈阳赛区

    这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K  Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...