当前位置: 首页 > news >正文

南京网站建设学习扁平化网站导航ui模板

南京网站建设学习,扁平化网站导航ui模板,秦皇岛做网站哪家好,黄岗住房和城乡建设厅官方网站文章目录axios实现vue-resource实现上一篇:(三十六)Vue解决Ajax跨域问题 先看一个github搜索案例 有一个搜索框,输入相关用户名,就能模糊搜索出用户,展现到下方 第一步:我们用到了第三方样式库…

文章目录

  • axios实现
  • vue-resource实现

上一篇:(三十六)Vue解决Ajax跨域问题

先看一个github搜索案例
有一个搜索框,输入相关用户名,就能模糊搜索出用户,展现到下方
请添加图片描述

在这里插入图片描述
第一步:我们用到了第三方样式库bootstrap,首先需要在public文件夹见一个css文件夹,放入样式库,然后在index.html页面进行引入
请添加图片描述

    <!-- 引入第三方样式 --><link rel="stylesheet" href="<%=BASE_URL%>css/bootstrap.css">

第二步:拆分组件,搜索跟文本框为一个组件Search,展示用户为一个组件List

axios实现

通用的 Ajax 请求库, 官方推荐,使用比较广泛
axios安装命令:npm i axios
App组件

<template><div class="container"><Search/><List/></div>
</template>
<script>
import Search from "@/components/Search";
import List from "@/components/List";
export default {name: "App",components: {List, Search},
}
</script>
<style>
</style>

Seach组件

<template><section class="jumbotron"><h3 class="jumbotron-heading">Search Github Users</h3><div><input type="text" placeholder="enter the name you search" v-model="keyWord"/>&nbsp;<button @click="searchUsers">Search</button></div></section>
</template><script>import axios from "axios";export default {// eslint-disable-next-line vue/multi-word-component-namesname: "Search",data(){return{keyWord:''}},methods:{searchUsers(){if (this.keyWord === ""){alert("输入不能为空")return}//请求前更新List的数据this.$bus.$emit('updateListData',{isFirst:false,isLoading:true,errMsg:'',users:[]})axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(response => {//请求成功后更新List的数据this.$bus.$emit('updateListData',{isLoading:false,errMsg:'',users:response.data.items})},error => {console.log('请求失败',error.message)//请求后更新List的数据this.$bus.$emit('updateListData',{isLoading:false,errMsg:error.message,users:[]})})}}
}
</script>
<style scoped>
</style>

List组件

<template><div class="row"><!--展示用户列表--><div v-show="info.users.length" class="card" v-for="user in info.users" :key="user.login"><a :href="user.html_url" target="_blank"><img :src="user.avatar_url" style='width: 100px'/></a><p class="card-text">{{user.login}}</p></div><!--展示欢迎词--><h2 v-show="info.isFirst">欢迎使用</h2><!--展示加载中--><h2 v-show="info.isLoading">加载中....</h2><!--展示错误信息--><h2 v-show="info.errMsg">{{info.errMsg}}</h2></div>
</template><script>
export default {// eslint-disable-next-line vue/multi-word-component-namesname: "List",data(){return{info: {isFirst:true,isLoading:false,errMsg:'',users:[]}}},mounted() {this.$bus.$on('updateListData',(dataObj)=>{this.info = {...this.info,...dataObj}})}
}
</script><style scoped>
.album {min-height: 50rem; /* Can be removed; just added for demo purposes */padding-top: 3rem;padding-bottom: 3rem;background-color: #f7f7f7;
}.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>

vue-resource实现

vue-resource:是一个插件库,对ajax进行了封装,用法与axios差不多。
vue-resource安装命令:npm i vue-resource
使用这个插件之后Vue和VueComponent身上就多了个$http,通过$http.get$http.post进行Ajax请求
vue 插件库, vue1.x 使用广泛,官方已不维护
安装完之后,需要在mian.js进行引入

import vueResource from "vue-resource";
Vue.use(vueResource)

请添加图片描述
Seach组件改为:

<template><section class="jumbotron"><h3 class="jumbotron-heading">Search Github Users</h3><div><input type="text" placeholder="enter the name you search" v-model="keyWord"/>&nbsp;<button @click="searchUsers">Search</button></div></section>
</template><script>import axios from "axios";export default {// eslint-disable-next-line vue/multi-word-component-namesname: "Search",data(){return{keyWord:''}},methods:{searchUsers(){if (this.keyWord === ""){alert("输入不能为空")return}//请求前更新List的数据this.$bus.$emit('updateListData',{isFirst:false,isLoading:true,errMsg:'',users:[]})this.$http.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(response => {//请求成功后更新List的数据this.$bus.$emit('updateListData',{isLoading:false,errMsg:'',users:response.data.items})},error => {console.log('请求失败',error.message)//请求后更新List的数据this.$bus.$emit('updateListData',{isLoading:false,errMsg:error.message,users:[]})})}}
}
</script><style scoped></style>
http://www.yayakq.cn/news/205166/

相关文章:

  • 网站视图错位网站被跳转公告怎么写
  • 技术支持 东莞网站建设机械加工百度推广运营专员
  • 网站建设福州公司wordpress打开提速
  • 视频教做家常菜的网站手机兼职有哪些
  • 扶余市建设局网站wordpress主题中英文
  • 帝国做的网站打开速度拓普网站建设
  • 涟水网站建设宁晋县建设局网站
  • 厦门哪里有教网站建设WordPress关键词过滤
  • 南浔哪有做网站的金科做的网站
  • 东莞网站建设及外包室内装饰网站模板
  • 设计感网站襄阳营销型网站
  • 河南网站建设华企祥云不用花生壳做网站服务器
  • 朔州路桥建设有限责任公司网站设计师学校有哪些
  • 昆明网站建设系统浏览器禁止网站怎么做
  • 包装设计的网站网站常用字体
  • 网络游戏开发培训上海网站关键词优化
  • 轻量应用服务器搭建网站外发加工网有什么软件
  • 郴州做网站网站搭建代理
  • 建设局网站打不开盐城网站开发公司电话
  • 上海网站建设团队杨浦wordpress怎么清缓存
  • php做网站标题加链接网站搜索引擎优化的基本内容
  • 便宜购 网站建设代运营网站
  • 创可贴在线设计网站钢结构东莞网站建设
  • 邯郸高端网站建设高埗仿做网站
  • 营销型科技网站搜索关键词分析
  • 内网网站开发费用页游代理
  • 新闻类网站备案 100万网站开发需求分析中性能需求分析
  • 个人做网站备案多少钱wordpress模板插件
  • 网站管理的内容app开发公司有哪些
  • 潍坊做网站的百度无锡营销中心