uni-APP学习日记7
2020-08-05 08:53
866 查看
收藏功能实现
uni-app阻止冒泡<view class="icons" @click.stop="likeTap"> <!-- @click.stop 阻止自定义组件冒泡 -->
** ## *addToSet* ** 数组更新操作符。原子操作。给定一个或多个元素,除非数组中已存在该元素,否则添加进数组。 ******************************************************************* 示例代码 1:添加一个元素 如果 tags 数组中不包含 database,添加进去 const dbCmd = db.command let res = await db.collection('todos').doc('doc-id').update({ data: { tags: dbCmd.addToSet('database') } }) ******************************************************************* 示例代码 2:添加多个元素 需传入一个对象,其中有一个字段 each,其值为数组,每个元素就是要添加的元素 const dbCmd = db.command let res = await db.collection('todos').doc('doc-id').update({ data: { tags: dbCmd.addToSet({ each: ['database', 'cloud'] }) } })
搜索页面
vuex管理历史记录
*** ## store文件夹下 *** // Vuex 状态管理 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store =new Vuex({ }) export default store
*** ## 在main.js注册 *** import Vue from 'vue' import App from './App' import api from './common/api' import store from './store'//************ Vue.config.productionTip = false Vue.prototype.$api=api App.mpType = 'app' const app = new Vue({ store,//********** ...App }) app.$mount()
究极体
// Vuex 状态管理 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store =new Vuex.Store({ // 数据源 state:{ // 本地缓存搜索历史 historyList:uni.getStorageSync("_history")||[]//先取本地缓存的数据,没有数据则为空 }, mutations:{//类似methods,放置方法的 SET_HISTORY_LIST(state,history){ state.historyList=history }, CLEAR_HISTORY(state){ state.historyList=[] } }, actions:{//有点像main函数,供外界调用 set_history({commit,state},history){ let list=state.historyList list.unshift(history) uni.setStorageSync('_history',list)//会覆盖掉原来该 key 对应的内容,这是一个同步接口 commit('SET_HISTORY_LIST',list) }, clearHistory({commit}){ uni.removeStorageSync('_history') commit('CLEAR_HISTORY') } } }) export default store
搜索页面的调用
//**********先引用************************ import { mapState } from 'vuex' //*************methods中调用****************** setHistory() { this.$store.dispatch('set_history', { name: this.value }) }, clear(){ this.$store.dispatch('clearHistory') uni.showToast({ title:'已清空搜索历史' }) },
自定义搜索组件与搜索页面的value绑定
- 子组件获取输入框的值,发送给父组件
<input type="text" class="navbar-search_text" value="" placeholder="————新开的游泳馆?" v-model="val" @input="inputChange" /> inputChange(e) { const { value } = e.detail this.$emit('input',value) }
- 父组件接收,每隔700ms搜索一次
<navbar :isSearch="true" v-model="value" @input="input"></navbar> input(value) { console.log('键盘输入:', value) if (!value) { clearTimeout(this.timer) this.mark = false this.getSearch(value) return } // 做定时标记mark 不会在页面渲染,不用在data中声明 if (!this.mark) { this.mark = true this.timer = setTimeout(() => { this.mark = false this.getSearch(value) }, 700) } },
- 子组件实时接收父组件
//******功能:点击历史标签进入搜索************* props: { isSearch: { type: Boolean, default: false }, value:{ type:[String,Number], default:'' } }, watch:{ value(newVal){ this.val=newVal } }, **************v-modle双向数据绑定,将标签内容显示在搜素框***************
- 卡片组件在点击后添加标签
open(){ this.$emit('FromCard',this.item) }
相关文章推荐
- uni-app基础学习
- uni-app基础知识学习【4】下拉刷新+上拉加载+网络请求
- uniapp学习打卡-vue笔记
- uni-app学习资料整理-1.白话uni-app
- uniapp --自我学习
- 小程序踩坑日记(基于uniapp)
- uni-app学习笔记
- uni-app学习笔记
- uniapp学习打卡第1天
- uni-app基础知识学习【5】上传图片、预览图片+跨域兼容+导航跳转
- uni-app基础知识学习【6】组件的创建+组件通讯+uni-ui使用
- 微信小程序、UNI-app、taro-app学习总结
- [学习]uni-app返回上层小键盘不关闭
- 学习uni-app的错误以及解决方案
- [学习]uni-app监听原生标题页面跳转
- uni-app学习笔记——uni-app资源
- uniapp-接口学习【界面-未(下班前进行重点内容的修改概括)】
- uniapp学习打卡
- uni-app基础知识学习【0】基本了解