vue中组件之间使用axios传值
2019-05-24 17:56
453 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43339643/article/details/90520833
1.使用axios传值的首先可以通过npm安装
npm install axios from 'axios'
2.安装成功后启动项目
使用语句 npm run dev
接下来进入项目中,在main.js里面引入并实例化
import axios from 'axios' Vue.prototype.axios=axios
3.使用方法
我找了一个开源的api做了新闻列表接入,如下
html部分:
<ul> <li v-for="(item,key) in list" style="list-style:none;"> <router-link :to="'/newscontent/'+(item.aid)">{{item.title}}</router-link> </li> </ul>
调用部分:
tips:在.then()里面最好使用ES6的箭头函数,目的是统一方法内外的两个this对象,使外面的this对象等于指向里面的this对象
methods: { getDate() { var api = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1"; this.axios.get(api).then( respon => { this.list = respon.data.result; }, error => {} ); } }, mounted() { this.getDate(); }
接收部分
1:当然,首先要使用路由布置传递方式,这里我使用的是动态路由匹配
在index.js文件中这样写
{ path: '/newscontent/:aid',//动态路由匹配 name: 'newscontent', component: Newscontent , },
2:在要接受的组件中这样写
<template> <div> <div v-for="(item,key) in msg"> <div v-html="item.content"></div> </div> </div> </template> <script> export default { data(){ return{ msg:[] } }, methods:{ getDate(value){ var api='http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid='+value this.axios.get(api).then((respon)=>{ this.msg=respon.data.result },(error)=>{ }) } }, mounted(){ var aid=this.$route.params.aid;//这里是动态路由接受参数的方式 this.getDate(aid) } } </script>
写的不好或者不对的地方,欢迎指正
相关文章推荐
- Vue2.0与 [百度地图] 结合使用———vue+webpack+axios+百度地图实现组件之间的通信
- Vue2.0与 [百度地图] 结合使用———vue+webpack+axios+百度地图实现组件之间的通信
- Vue中父子组件之间传值
- Vue组件之间传值
- vue.js+koa2项目实战(五)axios 及 vue2.0 子组件和父组件之间的传值
- 深入理解Vue 组件之间传值
- 详解vue 组件之间使用eventbus传值
- Vue官方推荐AJAX组件axios.js使用方法详解与API
- vue.js使用props在父子组件之间传参
- vue2.0组件之间的传值
- VUE 父子组件之间传值记录。
- **Vue父子组件之间传值:*
- Vue 组件之间传值
- Vue1.0学习总结(4)———Vue1.0自定义组件、Vue1.0组件之间的通信、slot的使用
- Vue 使用Props属性实现父子组件的动态传值
- Vue 事件总线 兄弟组件之间传值
- Vue-router单页应用,以及组件之间的嵌套使用
- vue 父传子、子传父、非父子关系之间的组件传值
- vue组件之间互相传值:父传子,子传父
- Vue.js父与子组件之间传参 父向子组件传参 例子:App.vue为父,引入componetA组件之后,则可以在template中使用标签(注意驼峰写法要改成componet-a写法,因为ht