前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何学习uni-app?

如何学习uni-app?

作者头像
达达前端
发布2022-04-29 10:40:03
7170
发布2022-04-29 10:40:03
举报
文章被收录于专栏:达达前端达达前端
file
file
file
file
file
file
file
file
file
file
file
file

uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架。

开发者通过编写 Vue.js 代码,uni-app 将其编译到iOS、Android、微信小程序、H5等多个平台,保证其正确运行并达到优秀体验。

file
file
代码语言:javascript
复制
<template>
 <view class="content">
 </view>
</template>
<script>
 export default {
  data: {
  }
 }
</script>
代码语言:javascript
复制
methods: {
 openinfo() {
  var newsid = e.currentsTarget.dataset.newsid;
    uni.navigateTo({
     url: '../info/info?newsid=' newsid
    })
 }
}
代码语言:javascript
复制
export defaults {
 onLoad: function(e){
  uni.request({
     url: '' e.newsid,
     method: 'GET',
     data: {},
     success: res => {
     }
     fail: () => {},
     complete: () => {}
    })
 }
}
代码语言:javascript
复制
<template>
 <view class="content">
  <view class="title"></view>
</view>
</template>

<rich-text class="richTest" :nodes="strings"></rich-text>
file
file
file
file

列表

返回数据格式post_id 新闻idtitle 标题created_at 创建时间author_avatar 图标

详情地址:

代码语言:javascript
复制
https://unidemo.dcloud.net.cn/api/news/36kr/   id

使用rich-text来展示新闻内容

代码语言:javascript
复制
<rich-text class="richText" :nodes="strings"></rich-text>

2018年,uni-app,Dcloud推出uni-app,下载了官方提供的hello示例教程

空白的项目要拷贝uni.css和uni.js,保存到common目录打开pages.json将文件中的navigationBarTitleText

v-for表示要循环的语句@tap表示绑定点击事件:data-postid表示绑定一个动态的数据而postid表示这个动态的数据属性是这个名字

编写js代码的时候,编译器会自动用eslint对代码进行检查onLoad是页面的生命周期

代码语言:javascript
复制
<template>
 <view class="content">
  <view class="uni-list">
     <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in news" :key="index" @tap="opennews" :data-postid="item.post_id">
     <view class="uni-media-list">
      <image class="uni-media-list-logo" :src="item.author_avatar"></image>
        <view class="uni-media-list-body">
         <view class="uni-media-list-text-top">{{item.title}}</view>
         <view class="uni-media-list-text-bottom uni-ellipsis">{{item.created_at}}</view>
         </view>
        </view>
    </view>
</view>
</view>
</template>

<script>
 export default {
  data() {
      return {
        news: []
      };
    },
    onLoad: function() {
     uni.request({
      url: 'https://',
        method: 'GET',
        data: {},
        success: res => {
         this.news = res.data;
        },
        fail: () => {},
        complete: () => {}
        });
    },
    methods: {
     opennews(e){
      uni.navigateTo({
         url: '../news/new?postid=' e.currentTarget.dataset.postid
         });
        }
        }
    }
</script>
<style>
.uni-media-list-body{height:auto;}
.uni-media-list-text-top{line-height: 1.6em;}
</style>
代码语言:javascript
复制
<template>
    <view class="wrap">
        <view class="title">
            {{title}}
        </view>
        <view class="content">
            <rich-text :nodes="content"></rich-text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: '',
                content: ''
            };
        },
        onLoad:function(e){
            uni.request({
                url: 'https://'  e.postid,
                method: 'GET',
                data: {},
                success: res => {
                    this.title = res.data.title;
                    this.content = res.data.content;
                },
                fail: () => {},
                complete: () => {}
            });
        }
    }
</script>

<style>
    .wrap{padding: 10upx 2%;width: 96%;flex-wrap: wrap;}
    .title{line-height: 2em;font-weight: bold;font-size: 40upx;}
    .content{line-height: 2em;}
</style>

网页大多是b/s服务端代码混合在页面里现在是c/s

代码语言:javascript
复制
<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8" />  
        <title></title>  
        <script type="text/javascript">  

        </script>  
        <style type="text/css">  

        </style>  
    </head>  
    <body>  

    </body>  
</html>  
代码语言:javascript
复制
<template>  
    <view>  

    </view>  
</template>  

<script>  
    export default {  

    }  
</script>  

<style>  

</style>  
代码语言:javascript
复制
<script>  
var util = require('../../../common/util.js');  //require这个js模块  
var formatedPlayTime = util.formatTime(playTime); //调用js模块的方法  
</script>  
代码语言:javascript
复制
function formatTime(time) {  
    return time;//这里没写逻辑  
}  

module.exports = {  
    formatTime: formatTime  
}  
代码语言:javascript
复制
var dateUtils = require('../../../common/util.js').dateUtils;
import * as echarts from '/components/echarts.simple.min.js';
代码语言:javascript
复制
<style>
 @import "./common/uni.css";
 .uni-hello-text {
  color: #7A7E83;
    }
</style>

导入一个角标的组件库

代码语言:javascript
复制
<template>
 <view>
  <uni-badge text="abc" :inverted="true"></uni-badge>
</view>
</template>

<script>
 import uniBadge from '../../../components/uni-badge.vue";
 export default {
  data() {
     return {
     }
    },
    components: {
     uniBadge
    }
}
</script>
代码语言:javascript
复制
// main.js
import pageHead from './components/page-head.vue' // 导入
Vue.component('page-head', pageHead)

div改view

span, font 改 text

a 改成 navigator

img 改 image

input, form, button, checkbox, radio, label, textarea, canvas, video

select 改 picker

iframe 改 web-view

scroll-viewswipericonrich-textprogresssliderswitchcameralive-playermapcover-view

覆盖原生组件,在map上加个遮罩,则需要使用cover-view组件

js变化,分为运行环境变化,数据绑定模式变化,api浏览器中的js是w3c组织基于js规范补充了window、document、navigator、location等专用对象。

代码语言:javascript
复制
<html>
 <head>
  <script type="text/javascript">
     document.addEventListener("DOMContentLoaded",function(){
      document.getElementById("spana").innerText="456"
     })
     function changtextvalue() {
      document.getElementById("spana").innerText="789"
     }
     </script>
    </head>
    <body>
     <span id="spana">123</span>
     <button type="button onclick="changetextvalue()">111</button>
     </body>
</html>
代码语言:javascript
复制
<template>
 <view>
  <text>{{textvalue}}</text>
    <button :type="buttontype" @click="changetextvalue()"></button>
    </view>
</template>
<script>
export default{
data() {
 return {
  textvalue: "123",
    buttontype: "primary"
 };
 },
 onLoad() {
  this.textvalue="456"
},
methods: {
 changetextvalue() {
  this.textvalue="789"
    }
    }
}
</script>

alert, confirm改成 uni.showmodelajax 改成 uni.request

cookie, session,rem只能用于h5注意背景图和字体文件尽量不要大于40k。

vue和微信小程序

file
file

小程序生命周期

file
file

onLoad: 页面加载一个页面只会调用一次,在onLoad中获取打开当前也迈进所调用的query参数

onShow页面显示每次打开页面都会调用一次

onReady: 页面初次渲染完成一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互

onHide: 页面隐藏当navigateTo或底部tab切换时调用

onUnload: 页面卸载当redirectTo或navigateBack的时候调用

vue一般会在created或者mounted中请求数据在小程序,会在onLoad或者onShow中请求数据

代码语言:javascript
复制
<img :src="imgSrc"/>
<image src="{{imgSrc}}"></image>
代码语言:javascript
复制
VUE
<img :src="imgSrc"/>
小程序
<image src="{{imgSrc}}"></image>
代码语言:javascript
复制
<ul id="example-1">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>

var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  }
})
代码语言:javascript
复制
<ul id="example-1">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>

var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  }
})

显示与隐藏元素

vue中,使用v-if 和v-show控制元素的显示和隐藏

小程序中,使用wx-if和hidden控制元素的显示和隐藏

代码语言:javascript
复制
<button v-on:click="counter  = 1">Add 1</button>
<button v-on:click.stop="counter =1">Add1</button>  //阻止事件冒泡
代码语言:javascript
复制
<button bindtap="noWork">明天不上班</button>
<button catchtap="noWork">明天不上班</button>  //阻止事件冒泡
代码语言:javascript
复制
<div id="app">
    <input v-model="reason" placeholder="填写理由" class='reason'/>
</div>

new Vue({
  el: '#app',
  data: {
   reason:''
  }
})
代码语言:javascript
复制
```
<div id="app">
    <input v-model="reason" placeholder="填写理由" class='reason'/>
</div>

new Vue({el: '#app',data: {reason:''}})

代码语言:javascript
复制

new Vue({el: '#app',data: {reason:''}})

代码语言:javascript
复制
vue中,通过this.reason取值

小程序中,通过this.data.reason取值

new Vue({el: '#app',methods:{say(arg){consloe.log(arg)}}})

代码语言:javascript
复制
通过e.currentTarget.dataset.*的方式获取

Page({data:{reason:''},toApprove(e) {let id = e.currentTarget.dataset.id;}})

代码语言:javascript
复制

//子组件 bar.vue

<script>export default{props:{title:{type:String,default:''}}},methods:{say(){console.log('明天不上班');this.$emit('helloWorld')}}</script>

// 父组件 foo.vue

<script>import Bar from './bar.vue'export default{data(){return{title:"我是标题"}},methods:{helloWorld(){console.log('我接收到子组件传递的事件了')}},components:{Bar}</script>

代码语言:javascript
复制

{"component": true}

代码语言:javascript
复制

"usingComponents": {"tab-bar": "../../components/tabBar/tabBar"}

代码语言:javascript
复制

//子组件 bar.vue

<script>export default{props:{title:{type:String,default:''}}},methods:{say(){console.log('明天不上班');this.$emit('helloWorld')}}</script>

// 父组件 foo.vue

<script>import Bar from './bar.vue'export default{data(){return{title:"我是标题"}},methods:{helloWorld(){console.log('我接收到子组件传递的事件了')}},components:{Bar}</script>

代码语言:javascript
复制

//子组件 bar.vue

<script>export default{props:{title:{type:String,default:''}}},methods:{say(){console.log('明天不上班');this.$emit('helloWorld')}}</script>

// 父组件 foo.vue

<script>import Bar from './bar.vue'export default{data(){return{title:"我是标题"}},methods:{helloWorld(){console.log('我接收到子组件传递的事件了')}},components:{Bar}</script>

代码语言:javascript
复制

// 父组件 foo.vue

<script>import Bar from './bar.vue'export default{data(){return{ title:"我是标题"}},components:{Bar}</script>

// 子组件bar.vue

<script>export default{props:{title:{type:String,default:''}}}</script>

代码语言:javascript
复制

properties: {// 弹窗标题currentpage: { // 属性名type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)value: 'index' // 属性初始值(可选),如果未指定则会根据类型选择一个}}

//子组件中methods: { // 传递给父组件cancelBut: function (e) {var that = this;var myEventDetail = { pickerShow: false, type: 'cancel' } // detail对象,提供给事件监听函数this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件,父组件中使用},}

//父组件中

// 获取子组件信息toggleToast(e){console.log(e.detail)}`

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 列表
  • vue和微信小程序
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档