六十八、完成vue项目推荐和周末游组件,并使用ajax发起ajax请求 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
六十八、完成vue项目推荐和周末游组件,并使用ajax发起ajax请求
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
@author:runsen
2020/10/27、 周二、今天又是奋斗的一天。 |
写在前面:我是「runsen」,热爱技术、热爱开源、热爱编程。技术是开源的、知识是共享的。大四弃算法转前端,需要每天的日积月累, 每天都要加油!!!
今天将完成vue项目推荐和周末游组件,并使用ajax发起ajax请求。
<template><div><home-header></home-header><home-swiper></home-swiper><home-icons></home-icons><home-recommend></home-recommend><home-weekend></home-weekend></div> </template><script> // 局部组件需要插入到components中,由于键和值都是一样,所以写成homeheader import homeheader from './components/header' import homeswiper from './components/swiper' import homeicons from './components/icons' // 在文件夹components新建recommend和weekend import homerecommend from './components/recommend' import homeweekend from './components/weekend'export default {name: 'home',components: {homeheader,homeswiper,homeicons,homerecommend,homeweekend} } </script><style> </style> <template><div><div class="title">热销推荐</div><ul><liclass="item border-bottom"v-for="item of recommendlist":key="item.id"><img class="item-img" :src="item.imgurl" /><div class="item-info"><p class="item-title">{{item.title}}</p><p class="item-desc">{{item.desc}}</p><button class="item-button">查看详情</button></div></li></ul></div> </template><script> export default {name: 'homerecommend',data () {return {recommendlist: [{id: '0001',imgurl: 'http://img1.qunarzz.com/sight/p0/201404/23/04b92c99462687fa1ba45c1b5ba4ad77.jpg_140x140_73fda71d.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}, {id: '0002',imgurl: 'http://img1.qunarzz.com/sight/p0/201404/23/04b92c99462687fa1ba45c1b5ba4ad77.jpg_140x140_73fda71d.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}, {id: '0003',imgurl: 'http://img1.qunarzz.com/sight/p0/201404/23/04b92c99462687fa1ba45c1b5ba4ad77.jpg_140x140_73fda71d.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}]}} } </script><style lang="stylus" scoped>@import '~styles/mixins.styl'.titlemargin-top: .2remline-height: .8rembackground: #eeetext-indent: .2rem.itemoverflow: hiddendisplay: flexheight: 1.9rem.item-imgwidth: 1.7remheight: 1.7rempadding: .1rem.item-infoflex: 1padding: .1remmin-width: 0.item-titleline-height: .54remfont-size: .32remellipsis().item-descline-height: .4remcolor: #cccellipsis().item-buttonline-height: .44remmargin-top: .16rembackground: #ff9300padding: 0 .2remborder-radius: .06remcolor: #fff </style> template><div><div class="title">周末去哪儿</div><ul><liclass="item border-bottom"v-for="item of recommendlist":key="item.id"><div class="item-img-wrapper"><img class="item-img" :src="item.imgurl" /></div><div class="item-info"><p class="item-title">{{item.title}}</p><p class="item-desc">{{item.desc}}</p></div></li></ul></div> </template><script> export default {name: 'homeweekend',data () {return {recommendlist: [{id: '0001',imgurl: 'http://img1.qunarzz.com/sight/source/1505/9f/f585152825459.jpg_r_640x214_5d46e4cc.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}, {id: '0002',imgurl: 'http://img1.qunarzz.com/sight/source/1505/9f/f585152825459.jpg_r_640x214_5d46e4cc.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}, {id: '0003',imgurl: 'http://img1.qunarzz.com/sight/source/1505/9f/f585152825459.jpg_r_640x214_5d46e4cc.jpg',title: '大连圣亚海洋世界',desc: '浪漫大连首站,浪漫的海洋主题乐园'}]}} } </script><style lang="stylus" scoped>@import '~styles/mixins.styl'.titlemargin-top: .2remline-height: .8rembackground: #eeetext-indent: .2rem.item-img-wrapperoverflow: hiddenheight: 0padding-bottom: 33.9%.item-imgwidth: 100%.item-infopadding: .1rem.item-titleline-height: .54remfont-size: .32remellipsis().item-descline-height: .4remcolor: #cccellipsis() </style>vue本身不支持ajax请求,需要使用“axios”的第三方插件(2.0),axios是基于promise的http请求客户端,用来发送请求,是vue2.0推荐使用的,同时不再对vue-resource进行更新和维护。也可以使用vue-resource进行跨域请求。
安装:npm install axios --save
在home.vue
<template><div><home-header :city="city"></home-header><home-swiper :list="swiperlist"></home-swiper><home-icons :list="iconlist"></home-icons><home-recommend :list="recommendlist"></home-recommend><home-weekend :list="weekendlist"></home-weekend></div> </template><script> import homeheader from './components/header' import homeswiper from './components/swiper' import homeicons from './components/icons' import homerecommend from './components/recommend' import homeweekend from './components/weekend' import axios from 'axios' export default {name: 'home',components: {homeheader,homeswiper,homeicons,homerecommend,homeweekend},data () {return {city: '',swiperlist: [],iconlist: [],recommendlist: [],weekendlist: []}},methods: {gethomeinfo () {// 接口路径是/api/index.json,需要在webpack config目录下的index.js中的proxytable设置axios.get('/api/index.json').then(this.gethomeinfosucc)},gethomeinfosucc (res) {console.log(res)res = res.dataif (res.ret && res.data) {const data = res.datathis.city = data.citythis.swiperlist = data.swiperlistthis.iconlist = data.iconlistthis.recommendlist = data.recommendlistthis.weekendlist = data.weekendlist}}},// 当组件挂载是的钩子函数mountedmounted () {this.gethomeinfo()} } </script><style> </style>在static目录下创建接口的json数据。访问本地json的话,本地json必须放到static下面
配置了json,可以直接在8080端口访问数据,放回的原页面。
在一般的项目,不可能直接将static路由暴露,需要重定向,在config/index.js配置即可
在配置文件发生改变,需要重启应用
header.vue
<template><div class="header"><div class="header-left"><div class="iconfont back-icon">icons.vue
<template><div class="icons"><swiper :options="swiperoption"><swiper-slide v-for="(page, index) of pages" :key="index"><divclass="icon"v-for="item of page":key="item.id"><div class='icon-img'><img class='icon-img-content' :src='item.imgurl' /></div><p class="icon-desc">{{item.desc}}</p></div></swiper-slide></swiper></div> </template><script> export default {name: 'homeicons',props: {list: array},data () {return {swiperoption: {autoplay: false}}},computed: {pages () {const pages = []this.list.foreach((item, index) => {const page = math.floor(index / 8)if (!pages[page]) {pages[page] = []}pages[page].push(item)})return pages}} } </script><style lang="stylus" scoped>@import '~styles/varibles.styl'@import '~styles/mixins.styl'.icons >>> .swiper-containerheight: 0padding-bottom: 50%.iconsmargin-top: .1rem.iconposition: relativeoverflow: hiddenfloat: leftwidth: 25%height: 0padding-bottom: 25%.icon-imgposition: absolutetop: 0left: 0right: 0bottom: .44rembox-sizing: border-boxpadding: .1rem.icon-img-contentdisplay: blockmargin: 0 autoheight: 100%.icon-descposition: absoluteleft: 0right: 0bottom: 0height: .44remline-height: .44remtext-align: centercolor: $darktextcolorellipsis() </style>recommend.vue
<template><div><div class="title">热销推荐</div><ul><liclass="item border-bottom"v-for="item of list":key="item.id"><img class="item-img" :src="item.imgurl" /><div class="item-info"><p class="item-title">{{item.title}}</p><p class="item-desc">{{item.desc}}</p><button class="item-button">查看详情</button></div></li></ul></div> </template><script> export default {name: 'homerecommend',props: {list: array} } </script><style lang="stylus" scoped>@import '~styles/mixins.styl'.titlemargin-top: .2remline-height: .8rembackground: #eeetext-indent: .2rem.itemoverflow: hiddendisplay: flexheight: 1.9rem.item-imgwidth: 1.7remheight: 1.7rempadding: .1rem.item-infoflex: 1padding: .1remmin-width: 0.item-titleline-height: .54remfont-size: .32remellipsis().item-descline-height: .4remcolor: #cccellipsis().item-buttonline-height: .44remmargin-top: .16rembackground: #ff9300padding: 0 .2remborder-radius: .06remcolor: #fff </style>swiper.vue
<template><div class="wrapper"><swiper :options="swiperoption" v-if="showswiper"><swiper-slide v-for="item of list" :key="item.id"><img class="swiper-img" :src="item.imgurl" /></swiper-slide><div class="swiper-pagination" slot="pagination"></div></swiper></div> </template><script> export default {name: 'homeswiper',props: {list: array},data () {return {swiperoption: {pagination: '.swiper-pagination',loop: true}}},computed: {showswiper () {return this.list.length}} } </script><style lang="stylus" scoped>.wrapper >>> .swiper-pagination-bullet-activebackground: #fff.wrapperoverflow: hiddenwidth: 100%height: 0padding-bottom: 31.25�ckground: #eee.swiper-imgwidth: 100% </style>weekend.vue
<template><div><div class="title">周末去哪儿</div><ul><liclass="item border-bottom"v-for="item of list":key="item.id"><div class="item-img-wrapper"><img class="item-img" :src="item.imgurl" /></div><div class="item-info"><p class="item-title">{{item.title}}</p><p class="item-desc">{{item.desc}}</p></div></li></ul></div> </template><script> export default {name: 'homeweekend',props: {list: array} } </script><style lang="stylus" scoped>@import '~styles/mixins.styl'.titleline-height: .8rembackground: #eeetext-indent: .2rem.item-img-wrapperoverflow: hiddenheight: 0padding-bottom: 37.09%.item-imgwidth: 100%.item-infopadding: .1rem.item-titleline-height: .54remfont-size: .32remellipsis().item-descline-height: .4remcolor: #cccellipsis() </style>参考课程:慕课网vue2.5->2.6->3.0 开发去哪儿网app 从零基础入门到实战项目开发
请一键三连!!!!! |
总结
以上是尊龙游戏旗舰厅官网为你收集整理的六十八、完成vue项目推荐和周末游组件,并使用ajax发起ajax请求的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: