慕课网教程链接:https://coding.imooc.com/class/280.html
平时在有道云上做笔记,直接发到这里格式全乱了,有空整理,大家也可查看有道云链接:
http://note.youdao.com/noteshare?id=1851a98b26024767a1b43778d1c6849b&sub=B6EFBAE9D5A94097BF232EFAAC9EDAD9
搜索关键字推荐
import mongoose from 'mongoose' const poiSchema = mongoose.Schema({ name : {type:String}, province : {type:String}, city : {type:String}, county : {type:String}, areaCode : {type:String}, tel : {type:String}, area : {type:String}, add : {type:String}, type : {type:String}, module : {type:String}, longtide : {type:Number}, latitude : {type:Number} }) export default mongoose.model('poi',poiSchema)
import Router from 'koa-router' import Poi from '../dbs/models/poi' const router = new Router({prefix:'/search'}) router.get('/top', async (ctx,next)=>{ const result= await Poi.find({ // 注意这里获取get中参数的方法 city:ctx.query.city, // 传入正则表达式表示模糊查询 name:new RegExp(ctx.query.input) }) ctx.body ={ code:0, top:result.slice(0,10).map(item=>({ name:item.name, type:item.type })) } }) export default router
methods:{ …… input: _.debounce(async function(){ this.searchList = []; // 避免关键字为空时进行搜索导致多条数据闪烁 if(!this.search){ return } const {status,data:{top}} = await this.$axios.get('/search/top',{ // 注意所传参数要写在params内,而不是直接写 params:{ city:this.$store.state.geo.position.city.replace('市',''), input:this.search } }) this.searchList = top },300) }
热门搜索
router.get('/hotPlace',async (ctx,next)=>{ // 据说前后端共享一个store,如果这里有store的话可以直接获取store, // 但事实上ctx.store为undefined let city = ctx.store ? ctx.store.geo.position.city : ctx.query.city const result = await Poi.find({ city:city }).limit(10) // 取前10条数据 ctx.body = result.map(item=>({ name:item.name, type:item.type })) })
const state = ()=>({ menu:[], hotPlace:[] }), ……
const {status:status3,data} = await app.$axios.get('/search/hotPlace',{ params:{ // city:app.store.state.geo.position.city city:'三亚' } }) commit('home/setHotPlace',status3===200?data:[])
说明(坑的血泪史)
取参数
store获取