如何學習uni-app?


file

file

file

file

file

file

uni-app 是一個使用 Vue.js 開發跨平台應用的前端框架。

開發者通過編寫 Vue.js 代碼,uni-app 將其編譯到iOS、Android、微信小程序、H5等多個平台,保證其正確運行並達到優秀體驗。
file

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

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

file

file

列表

返回數據格式
post_id 新聞id
title 標題
created_at 創建時間
author_avatar 圖標

詳情
地址:

https://unidemo.dcloud.net.cn/api/news/36kr/ + id

使用rich-text來展示新聞內容

<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是頁面的生命周期

<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>
<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

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8" />  
        <title></title>  
        <script type="text/javascript">  

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

        </style>  
    </head>  
    <body>  

    </body>  
</html>  
<template>  
    <view>  

    </view>  
</template>  

<script>  
    export default {  

    }  
</script>  

<style>  

</style>  
<script>  
var util = require('../../../common/util.js');  //require這個js模塊  
var formatedPlayTime = util.formatTime(playTime); //調用js模塊的方法  
</script>  
function formatTime(time) {  
    return time;//這里沒寫邏輯  
}  

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

導入一個角標的組件庫

<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>
// 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-view
swiper
icon
rich-text
progress
slider
switch
camera
live-player
map
cover-view

覆蓋原生組件,在map上加個遮罩,則需要使用cover-view組件

js變化,分為運行環境變化,數據綁定模式變化,api
瀏覽器中的js是w3c組織基於js規范補充了window、document、navigator、location等專用對象。

<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>
<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.showmodel
ajax 改成 uni.request

cookie, session,
rem只能用於h5
注意背景圖和字體文件盡量不要大於40k。

vue和微信小程序

file

小程序生命周期
file

onLoad: 頁面加載
一個頁面只會調用一次,在onLoad中獲取打開當前也邁進所調用的query參數

onShow頁面顯示
每次打開頁面都會調用一次

onReady: 頁面初次渲染完成
一個頁面只會調用一次,代表頁面已經准備妥當,可以和視圖層進行交互

onHide: 頁面隱藏
當navigateTo或底部tab切換時調用

onUnload: 頁面卸載
當redirectTo或navigateBack的時候調用

vue一般會在created或者mounted中請求數據
在小程序,會在onLoad或者onShow中請求數據

<img :src="imgSrc"/>
<image src="{{imgSrc}}"></image>
VUE
<img :src="imgSrc"/>
小程序
<image src="{{imgSrc}}"></image>
<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' }
    ]
  }
})

<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控制元素的顯示和隱藏

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

new Vue({
  el: '#app',
  data: {
   reason:''
  }
})
<div id="app">
    <input v-model="reason" placeholder="填寫理由" class='reason'/>
</div>

new Vue({
  el: '#app',
  data: {
   reason:''
  }
})
<div id="app">
    <input v-model="reason" placeholder="填寫理由" class='reason'/>
</div>

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

vue中,通過this.reason取值

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

<button @click="say('明天不上班')"></button>

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

通過e.currentTarget.dataset.*的方式獲取

<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view>
Page({
data:{
    reason:''
},
toApprove(e) {
    let id = e.currentTarget.dataset.id;
  }
})
//子組件 bar.vue
<template>
  <div class="search-box">
    <div @click="say" :title="title" class="icon-dismiss"></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
},
methods:{
    say(){
       console.log('明天不上班');
       this.$emit('helloWorld')
    }
}
</script>

// 父組件 foo.vue
<template>
  <div class="container">
    <bar :title="title" @helloWorld="helloWorld"></bar>
  </div>
</template>

<script>
import Bar from './bar.vue'
export default{
data(){
    return{
        title:"我是標題"
    }
},
methods:{
    helloWorld(){
        console.log('我接收到子組件傳遞的事件了')
    }
},
components:{
    Bar
}
</script>

{
  "component": true
}
"usingComponents": {
    "tab-bar": "../../components/tabBar/tabBar"
  }
	
<tab-bar currentpage="index"></tab-bar>
//子組件 bar.vue
<template>
  <div class="search-box">
    <div @click="say" :title="title" class="icon-dismiss"></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
},
methods:{
    say(){
       console.log('明天不上班');
       this.$emit('helloWorld')
    }
}
</script>

// 父組件 foo.vue
<template>
  <div class="container">
    <bar :title="title" @helloWorld="helloWorld"></bar>
  </div>
</template>

<script>
import Bar from './bar.vue'
export default{
data(){
    return{
        title:"我是標題"
    }
},
methods:{
    helloWorld(){
        console.log('我接收到子組件傳遞的事件了')
    }
},
components:{
    Bar
}
</script>

//子組件 bar.vue
<template>
  <div class="search-box">
    <div @click="say" :title="title" class="icon-dismiss"></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
},
methods:{
    say(){
       console.log('明天不上班');
       this.$emit('helloWorld')
    }
}
</script>

// 父組件 foo.vue
<template>
  <div class="container">
    <bar :title="title" @helloWorld="helloWorld"></bar>
  </div>
</template>

<script>
import Bar from './bar.vue'
export default{
data(){
    return{
        title:"我是標題"
    }
},
methods:{
    helloWorld(){
        console.log('我接收到子組件傳遞的事件了')
    }
},
components:{
    Bar
}
</script>

// 父組件 foo.vue
<template>
  <div class="container">
    <bar :title="title"></bar>
  </div>
</template>
<script>
import Bar from './bar.vue'
export default{
data(){
    return{        
        title:"我是標題"
    }
},
components:{
    Bar
}
</script>

// 子組件bar.vue
<template>
  <div class="search-box">
    <div :title="title" ></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
}
</script>
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自定義名稱事件,父組件中使用
    },
}

//父組件中
<bar bind:myevent="toggleToast"></bar>

// 獲取子組件信息
toggleToast(e){
    console.log(e.detail)
}

若本號內容有做得不到位的地方(比如:涉及版權或其他問題),請及時聯系我們進行整改即可,會在第一時間進行處理。


請點贊!因為你們的贊同/鼓勵是我寫作的最大動力!

歡迎關注達叔小生的簡書!

這是一個有質量,有態度的博客

博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM