赞
踩
本博客为 [uni-app](
此门课的跟学笔记,目的是便于个人复习和对知识快速索引,源码素材可在均可在视频评论区找到
<template> <view> 图片上传 <button type="primary" @click="chooseImg">图片上传</button> <image v-for="item in imgArr" :src="item"></image> </view> </template> <script> export default { data() { return { imgArr: [] } }, methods: { chooseImg() { uni.chooseImage({ count: 5, // 最大选择图片的数量,默认为9 // 上传成功的回调函数 // 在success或error的回调函数中的this不再指向内置Vue对象 // 如果改成箭头函数就可以解决这个问题 // 或者利用上一篇博客中的方法,let self=this,记录一下Vue内置对象 success:res=> { // 若成功返回的是本地文件路径列表tempFilePaths console.log(res); this.imgArr=res.tempFilePaths; } }) } } } </script>
<template> <view> 图片上传 <button type="primary" @click="chooseImg">图片上传</button> <!-- 为每一张图片添加图片预览,传递自身路径参数 --> <image v-for="item in imgArr" :src="item" @click="previewImg(item)"></image> </view> </template> <script> export default { data() { return { imgArr: [] } }, methods: { chooseImg() { uni.chooseImage({ count: 5, // 最大选择图片的数量,默认为9 // 上传成功的回调函数 // 在success或error的回调函数中的this不再指向内置Vue对象 // 如果改成箭头函数就可以解决这个问题 // 或者利用上一篇博客中的方法,let self=this,记录一下Vue内置对象 success: res => { // 若成功返回的是本地文件路径列表tempFilePaths console.log(res); this.imgArr = res.tempFilePaths; } }) }, // 预览图片 previewImg(itemPath) { console.log(itemPath); uni.previewImage({ current: itemPath, urls: this.imgArr, // 要预览的图片列表 loop: true, // 循环显示[只在APP中有效] indicator: 'default', // 图片指示器(圆点)[只在APP中有效] longPressActions: true // 长按图片显示操作菜单,不填写默认为保存相册 }) } } } </script>
<template> <view> 跨端兼容 <!-- #ifdef H5 --> <view>只在H5中显示</view> <!-- #endif --> <!-- #ifdef MP-WEIXIN --> <view>只在微信小程序中显示</view> <!-- #endif --> </view> </template> <script> export default { data() { return { } }, methods: { // 页面加载时触发 onLoad() { // #ifdef H5 console.log('在h5中打印') // #endif // #ifdef MP-WEIXIN console.log('在微信小程序中打印') // #endif } } } </script> <style> /* 在H5中的样式 */ /* #ifdef H5 */ view { color: hotpink; } /* #endif */ /* 在微信小程序中的样式 */ /* #ifdef MP-WEIXIN */ view { color: skyblue; } /* #endif */ </style>
有两种跳转方式,声明式(利用组件)和编程式(利用API)
下面案例的注释非常详细,需要注意的要点有:
<template> <view> <view>导航跳转</view> <!-- 1:组件式 --> <!-- 不带超链接的蓝色字体和下划线的效果 --> <!-- 不在tabbar中的页面可以直接跳 --> <navigator url="/pages/detail/detail">跳转至详情页(非tabBar页面用这个)</navigator> <!-- 在tabbar中的页面需要设置open-type --> <navigator url="/pages/message/message" open-type="switchTab">跳转至信息页(tabBar页面用这个)</navigator> <!-- 2:编程式 --> <button @click="goDetail">跳转至详情页(非tabBar页面用这个)</button> <button @click="goMessage">跳转至详情页(tabBar页面用这个)</button> <button @click="redirectDetail">跳转到详情页并关闭当前页面(tabBar页面用这个)</button> <!-- 3:导航跳转传参 --> <!-- 在编程式中同理 --> <navigator url="/pages/detail/detail?id=80&age=20">导航跳转+传递参数</navigator> <!-- 在detail页面中可以在onLoad方法中传递参数options来接受这个参数 --> </view> </template> <script> export default { data() { return { } }, methods: { goDetail() { // 只能跳转到非tbBar页面 uni.navigateTo({ url: '/pages/detail/detail' }) }, goMessage() { // 跳转到tabBar页面,并卸载所有其他非tabBar页面 // 只要是非tabBar页面都卸载 uni.switchTab({ url: '/pages/message/message' }) }, redirectDetail() { // 跳转到tabBar页面,只卸载上一个页面 uni.redirectTo({ url: '/pages/detail/detail' }) } } } </script>
生命周期函数 | 解释 | 注意 |
---|---|---|
beforeCreate | 在实例初始化之后被调用。详见 | |
created | 在实例创建完成后被立即调用。详见 | |
beforeMount | 在挂载开始之前被调用。详见 | |
mounted | 挂载到实例上去之后调用。详见 注意:此处并不能确定子组件被全部挂载,如果需要子组件完全挂载之后在执行操作可以使用$nextTick Vue官方文档 | |
beforeUpdate | 数据更新时调用,发生在虚拟 DOM 打补丁之前。详见 | 仅H5平台支持 |
updated | 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。详见 | 仅H5平台支持 |
beforeDestroy | 实例销毁之前调用。在这一步,实例仍然完全可用。详见 | |
destroyed | Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。详见 |
<template> <view id="myView"> <text>test组件</text> </view> </template> <script> export default { name:"test", data() { return { num: 3 }; }, // 在实例创建完成后立即被调用 created() { console.log('created 生命周期,数据刚被渲染',this.num) console.log('在此生命周期之前是拿不到这个数据的') }, // 挂载到实例上去之后调用 mounted() { console.log('挂载就是虚拟DOM转成真实DOM后渲染到页面') console.log('mounted 生命周期,此时可以通过DOM获取到id为myView的元素',document.getElementById('myView')) } } </script>
<template> <view> <test :val="title">父向子传值</test> </view> </template> <script> import test from '../../components/test.vue' export default { components: { test }, data() { return { title: 'hello uni' } }, methods: { } } </script>
<template> <view id="myView"> <text>test组件</text><br /> <text>这是父组件传递过来的数据{{val}}</text> </view> </template> <script> export default { name:"test", data() { return { }; }, props: ['val'], // 子向父承接 } </script>
<template> <view id="myView"> <text>test组件</text><br /> <button type="primary" @click="sendNum">给父组件传值</button> </view> </template> <script> export default { name:"test", data() { return { num: 5 }; }, methods: { sendNum() { console.log('给父组件传值') this.$emit('myEvent',this.num) } } } </script>
<template> <view> <!-- 父组件拿到值 --> <test @myEvent="getNum"></test> {{num}} </view> </template> <script> import test from '../../components/test.vue' export default { components: { test }, data() { return { num: 0, } }, methods: { getNum(num) { console.log(num); // 把num存到我们自己的num里面 this.num=num } } } </script>
<template> <view> 这是a组件<button size="mini" @click="addNum">修改b组件的数据</button> </view> </template> <script> export default { name:"a", data() { return { }; }, methods: { addNum() { uni.$emit('updateNum',10) } } } </script>
<template> <view> 这是b组件,b组件中的数据:{{num}} </view> </template> <script> export default { name:"b", data() { return { num: 0 }; }, created() { // 注册一个全局事件 uni.$on('updateNum',num=>{ this.num+=num }) } } </script>
<template> <view> <test-a></test-a> <test-b></test-b> </view> </template> <script> import testA from '../../components/a.vue' import testB from '../../components/b.vue' export default { data() { return { } }, methods: { }, components: { "test-a": testA, "test-b": testB } } </script>
在真正开发中,uni中提供的组件可能不够我们使用并且使用起来比较繁琐
感觉官方给出的使用说明已经相当完善了:uni-app官网 (dcloud.net.cn)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。