中小企业门户网站建设策略,北京市建设中心网站,wordpress登陆后台,图书网站建设论文文章目录axios实现vue-resource实现上一篇#xff1a;#xff08;三十六#xff09;Vue解决Ajax跨域问题
先看一个github搜索案例 有一个搜索框#xff0c;输入相关用户名#xff0c;就能模糊搜索出用户#xff0c;展现到下方 第一步#xff1a;我们用到了第三方样式库…
文章目录axios实现vue-resource实现上一篇三十六Vue解决Ajax跨域问题
先看一个github搜索案例 有一个搜索框输入相关用户名就能模糊搜索出用户展现到下方 第一步我们用到了第三方样式库bootstrap首先需要在public文件夹见一个css文件夹放入样式库然后在index.html页面进行引入 !-- 引入第三方样式 --link relstylesheet href%BASE_URL%css/bootstrap.css第二步拆分组件搜索跟文本框为一个组件Search展示用户为一个组件List
axios实现
通用的 Ajax 请求库, 官方推荐使用比较广泛 axios安装命令npm i axios App组件
templatediv classcontainerSearch/List//div
/template
script
import Search from /components/Search;
import List from /components/List;
export default {name: App,components: {List, Search},
}
/script
style
/styleSeach组件
templatesection classjumbotronh3 classjumbotron-headingSearch Github Users/h3divinput typetext placeholderenter the name you search v-modelkeyWord/nbsp;button clicksearchUsersSearch/button/div/section
/templatescriptimport 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
/styleList组件
templatediv classrow!--展示用户列表--div v-showinfo.users.length classcard v-foruser in info.users :keyuser.logina :hrefuser.html_url target_blankimg :srcuser.avatar_url stylewidth: 100px//ap classcard-text{{user.login}}/p/div!--展示欢迎词--h2 v-showinfo.isFirst欢迎使用/h2!--展示加载中--h2 v-showinfo.isLoading加载中..../h2!--展示错误信息--h2 v-showinfo.errMsg{{info.errMsg}}/h2/div
/templatescript
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}})}
}
/scriptstyle 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%;
}
/stylevue-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组件改为
templatesection classjumbotronh3 classjumbotron-headingSearch Github Users/h3divinput typetext placeholderenter the name you search v-modelkeyWord/nbsp;button clicksearchUsersSearch/button/div/section
/templatescriptimport 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:[]})})}}
}
/scriptstyle scoped/style