案例知识点

  1. 兄弟组件儿的通信     使用了Pubsub    订阅与发布
  2. ajax数据请求    获取前   获取中   获取后   获取为空    获取异常
  3. 获取成功后显示数据给到  原先定义号的 jsonData     users

    vue Search案例 消息订阅pubsub与ajax

    pubsub消息订阅组件,便于兄弟组件间调用

    npm install --save pubsub-js

    App.vue

     <template>
    <div id="app">
    <div class="container">
    <Search/>
    <users-main/>
    </div>
    </div>
    </template> <script>
    import Search from './components/Search.vue'
    import Main from './components/Main.vue'
    export default{
    components:{
    Search,
    // 内部定义的关键词名称 不可以使用 所以赋值一个名字
    UsersMain:Main }
    };
    </script> <style type="stylus"> </style>

    Main.vue

    // 由于请求状态在Main中,ajax写在main中以便同步更新4个状态。 写在search中不便更新状态。
    <template>
    <div>
    <!-- 搜索我有四种状态 -->
    <!-- 1.搜索之前 -->
    <h2 v-if="firstView">输入用户名搜索</h2>
    <!-- 2.搜索中.... -->
    <h2 v-if="loading">GitData...</h2>
    <!-- 4.没有搜索到 -->
    <h2 v-if="overNull">该关键字没有搜索到Data...</h2>
    <!-- 3.搜索失败error -->
    <h2 v-if="errorMsg">{{errorMsg}}</h2>
    <div v-else class="row" v-for="(user,index) in users" :key="index" :index="index">
    <div class="card">
    <a :href="user.url" target="_blank">
    <img :src="user.avatar_url" style='width: 100px'/>
    </a>
    <p class="card-text">{{user.name}}</p>
    </div>
    </div>
    </div>
    </template> <script type="text/ecmascript-6">
    import PubSub from 'pubsub-js'
    import axios from 'axios'
    export default{
    data(){
    return {
    firstView: true,
    loading:false,
    overNull:false,
    errorMsg: '',
    users:null
    // [
    // {url:'',avatar_url:'',name:''},
    // ] }
    },
    mounted(){
    // 是否在此发ajax消息 是点击search后
    // 订阅搜索的消息
    PubSub.subscribe('search',(msg,searchName)=>{
    const url = `https://api.github.com/search/users?q=${searchName}`
    // 更新失败(请求中)
    this.firstView = false
    this.loading = true
    this.overNull = false
    alert('请求中')
    // 发送ajax请求
    axios.get(url).then(response=> {
    // 获取数据 data
    const result = response.data
    // data中的items 里面有图片的路径 名称
    const users = result.items.map(item=>({
    url:item.html_url,
    avatar_url:item.avatar_url,
    name: item.login
    }))
    if(users.length !== 0){
    console.log(users)
    } // 成功更新状态(成功)
    this.loading = false
    this.users = users if(users.length == 0){
    this.overNull = true
    }
    // 失败更新状态(失败)
    }).catch(error=>{
    this.loading = false
    this.errorMsg = '请求失败'
    }) })
    } };
    </script> <style type="stylus" rel="stylesheet/stylus"> .card {
    float: left;
    width: 33.333%;
    padding: .75rem;
    margin-bottom: 2rem;
    border: 1px solid #efefef;
    text-align: center;
    } .card > img {
    margin-bottom: .75rem;
    border-radius: 100px;
    } .card-text {
    font-size: 85%;
    } </style>

    Search.vue

     <template>
    <div>
    <section class="jumbotron">
    <h3 class="jumbotron-heading">Search Github Users</h3>
    <div>
    <input type="text" placeholder="enter the name you search" v-model="searchName"/>
    <button @click="search">Search</button>
    </div>
    </section>
    </div>
    </template> <script type="text/ecmascript-6">
    import PubSub from 'pubsub-js'
    export default{
    data(){
    return {
    searchName:''
    }
    },
    methods:{
    search(){
    const searchName = this.searchName.trim()
    if(searchName){
    // 发布搜索的消息
    PubSub.publish('search',searchName)
    }
    }
    } };
    </script> <style type="stylus" rel="stylesheet/stylus"> </style>

Vue--- Vue(Pubsub + Ajax) 数据交互的更多相关文章

  1. Atitit vue.js 把ajax数据 绑定到form表单

    Atitit vue.js 把ajax数据 绑定到form表单 1.1. 使用场景:主要应用在编辑与提交场合..1 1.2. 绑定数据到form控件,可以使用jquery,不过vue.js更加简单1 ...

  2. 弹出层和ajax数据交互

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  3. Struts2与Ajax数据交互

    写在前面: ajax请求在项目中常常使用,今天就平时掌握的总结一下,关于使用ajax请求到Struts2中的action时,前台页面与后台action之间的数据传递交互问题. 这里我主要记录下自己所掌 ...

  4. ajax数据交互

    目录 一.ORM查询优化 1-1. only与defer 1-2. select_related与prefatch_related 二.MTV与MVC模型 三.choices参数 四.AJAX 4-1 ...

  5. django建立管理系统之五----单页ajax数据交互

    ajax数据提交: 需求: 1. 点击ajax方式提交后数据提交到后台数据库,并且在前台实现数据更新 a.可以用刷新页面来实现数据页面的更新 对应的html,实现局部刷新(可以用刷新页面实现,例如 $ ...

  6. vue.js 三(数据交互)isomorphic-fetch

    至于fetch的介绍,在这里就不多说了,官方和网络上的说明不少 之前使用jquery的Ajax朋友,可以尝试一下使用一下这个新的api 推荐使用isomorphic-fetch,兼容Node.js和浏 ...

  7. ajax数据交互(arcgis server)

    通过ajax来调用服务器map数据,来实现搜索功能. 效果: 1.我要搜索下中国移动的地理信息: 2.会搜出17条消息,然后把他们分页显示,一页6条: 3.每一页的6天数据,会在map生成一个6条ma ...

  8. jq ajax数据交互

    get 与 post 的区别 了解和使用 get和post是HTTP与服务器交互的方式, 说到方式,其实总共有四种:put,delete,post,get. 他们的作用分别是对服务器资源的增,删,改, ...

  9. jSon和Ajax登录功能,ajax数据交互案例

    ajax实例,检测用户与注册 检测用户名是否被占用: 在用户填写完用户名之后,ajax会异步向服务器发送请求,判断用户名是否存在 首先写好静态页面: index.html <!DOCTYPE h ...

  10. EChats+Ajax之柱状图的数据交互

    原文链接:https://blog.csdn.net/qq_37936542/article/details/79723710 一:下载 echarts.min.js 选择完整版进行下载,精简版和常用 ...

随机推荐

  1. 合约实战,代币合约,DAPP开发

    1. ERC20标准 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md pragma solidity ^; //定义接口 con ...

  2. artDialog组件应用学习(四)

    一.在对话框自定义操作按钮 预览: html调用代码: var btnArray = [ { value: '同意', callback: function () { this.content('你同 ...

  3. Redis 的Hashs(哈希表)数据类型

    在Memcached中,我们经常将一些结构化的信息打包成hashmap,在客户端序列化后存储为一个字符串的值,比如用户的昵称.年龄.性别.积分等,这时候在需要修改其中某一项时,通常需要将所有值取出反序 ...

  4. 【Linux】安装配置Tomcat7

    第一步:下载Tomcat安装包 下载地址:https://tomcat.apache.org/download-70.cgi [root@localhost ~]# wget http://mirro ...

  5. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

  6. JS的封装(JS插件的封装)

    JS中类的概念类,实际上就是一个function,同时也是这个类的构造方法,new创建该类的实例,new出的对象有属性有方法.方法也是一种特殊的对象. 类的方法在构造方法中初始化实例的方法(就是在构造 ...

  7. xshell连不上虚拟机linux的解决办法

    1.找到Linux系统的ip地址 输入命令   ifconfig 2.打开本地网络连接 将VMnet1的ip地址设置为和虚拟机ip同一网段的ip 比如虚拟机Linux系统的ip为   192.168. ...

  8. ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 3、安装Portal for ArcGIS

    安装Portal for ArcGIS 解压portal安装包,tar -xzvf Portal_for_ArcGIS_Linux_1051_156440.tar.gz 切换到arcgis账户静默安装 ...

  9. vector/list/set/map 遍历耗时统计

    #include<Windows.h> #include <iostream> #include<fstream> #include<vector> # ...

  10. 关于Cookie 的HttpOnly属性(java/web操作cookie+Tomcat操作jsessionid)

    关于Cookie的其它只是不在累述.本文主要讲讲自己在项目中遇到的cookie的HttpOnly属性问题 Cookie的HttpOnly属性说明 cookie的两个新的属性secure和Httponl ...