uni-app的基本語法和規(guī)范

生命周期函數(shù)

image.png
<template>
    <view class="content">
        
            <uni-calendar 
            :insert="true"
            :lunar="true" 
            :start-date="'2019-3-2'"
            :end-date="'2019-5-20'"
             ></uni-calendar>
        
        
        
        
        <testList :title="msg" @myEdits="getNums"></testList>
        <text>子組件傳遞的數(shù)據(jù){{num}}</text>
        <testa> </testa>
        <testb> </testb>




        <view class="text-area">
            <image v-for="(item,i) in imgArrs" :src="item" @click="perivImgs(item)"></image>
            樣式
        </view>
        <view class="iconfont icon-ningmeng-01" style="color:red"></view>
        <button @click="but(2,$event)">點擊$event</button>
        <button @click="pull">點擊頁面刷新</button>
        <button @click="getUrls">發(fā)送數(shù)據(jù)請求</button>
        <button @click="setSlosge">異步設(shè)置存儲</button>
        <button @click="getSlosge">異步獲取存儲</button>
        <button @click="removeSlosge">異步移除存儲</button>
        <button @click="asyncLoacl">同步設(shè)置數(shù)據(jù)存儲</button>
        <button @click="chooseImsgs">上傳圖片</button>

        <navigator url="/pages/datail/datail">跳轉(zhuǎn)詳情頁面</navigator>
        <navigator url="/pages/message/message" open-type="switchTab">跳轉(zhuǎn)tabar頁面</navigator>
        <navigator url="/pages/datail/datail" open-type="redirect">跳轉(zhuǎn)(沒有箭頭返回)</navigator>
        <button @click="goUrls">編程導(dǎo)航叶洞,點擊跳轉(zhuǎn)</button>
        <button @click="messUrs">編程導(dǎo)航,跳轉(zhuǎn)taber頁面</button>
        <button @click="deatisur">編程導(dǎo)航禀崖,跳轉(zhuǎn)詳情衩辟,關(guān)閉當(dāng)前頁面</button>

        <!-- #ifdef H5 -->
        <view>在h5可見(條件編譯)</view>
        <!-- #endif -->
        <!-- #ifdef MP-WEIXIN -->
        <view>在小程序中可見(條件編譯)</view>
        <!-- #endif -->



    </view>
</template>

<script>
    //引入一個組件
    import test from '../../components/test.vue'
    import testa from '../../components/a.vue'
    import testb from '../../components/b.vue'

    //下載導(dǎo)入日歷組件
    import uniCalendar from '../../components/uni-calendar/uni-calendar.vue'


    export default {
        components: {
            testList: test,
            testa,
            testb,
            uniCalendar,
        },
        data() {
            return {
                msg: 'msg(父組件)',
                title: '允許選中',
                imgArrs: [],
                num: '',
            }
        },
        //監(jiān)聽頁面下拉刷新
        onPullDownRefresh: function() {
            console.log('觸發(fā)了下拉刷新')
            setTimeout(() => {
                console.log('延遲下拉刷新')
                uni.stopPullDownRefresh()
            }, 2000)
        },
        //監(jiān)聽頁面上拉加載
        onReachBottom: function() {
            console.log('頁面觸底了')
        },
        onLoad(options) {
            // #ifdef H5
            console.log('h5打印')
            // #endif
            // #ifdef MP-WEIXIN
            console.log('微信打印')
            // #endif


            console.log('頁面加載')
            console.log('可以獲取上個頁面?zhèn)鬟f的參數(shù)', options)
        },
        onShow: function() {
            console.log('頁面顯示了')
        },
        onReady: function() {
            console.log('頁面初次加載完成')
        },
        onHide: function() {
            console.log('頁面隱藏了')
        },
        methods: {
              change(e) {
                        console.log(e);
                    },
            but(n, e) {
                console.log(n, e)
            },
            //點擊刷新方法:
            pull() {
                uni.startPullDownRefresh()
            },
            //發(fā)送數(shù)據(jù)請求:
            getUrls() {
                uni.request({
                    url: 'https://api.apiopen.top/getJoke?page=1&count=2&type=video',
                    success(res) {
                        console.log(res)
                    }
                })
            },
            //設(shè)置存儲數(shù)據(jù)
            setSlosge() {
                uni.setStorage({
                    key: 'id',
                    data: 80,
                    success() {
                        console.log('存儲成功')
                    }
                })
            },
            //獲取數(shù)據(jù)
            getSlosge() {
                uni.getStorage({
                    key: 'id',
                    success(res) {
                        console.log('獲取成功', res)
                    }
                })
            },
            //移除存儲:
            removeSlosge() {
                uni.removeStorage({
                    key: 'id',
                    success() {
                        console.log('刪除成功')
                    }
                })
            },
            //同步設(shè)置存儲
            asyncLoacl() {
                //設(shè)置
                uni.setStorageSync('num', 101)
                //獲取
                const res1 = uni.getStorageSync('num')
                console.log(res1)
                //刪除
                setTimeout(() => {
                    uni.removeStorageSync('num')
                }, 2000)

            },
            //上傳圖片:
            chooseImsgs() {
                uni.chooseImage({
                    count: 5, //h5中限制不住
                    success: res => {
                        console.log(res)
                        this.imgArrs = res.tempFilePaths
                    }
                })

            },
            //點擊圖片預(yù)覽:
            perivImgs(current) {
                console.log(current)
                uni.previewImage({
                    current,
                    urls: this.imgArrs,
                    indicator: 'number'
                })

            },
            //編程導(dǎo)航點擊跳轉(zhuǎn)
            goUrls() {

                uni.navigateTo({
                    url: '/pages/datail/datail?id=80'
                })
            },
            //編程導(dǎo)航跳轉(zhuǎn)tabar
            messUrs() {
                uni.switchTab({
                    url: '/pages/message/message'
                })

            },
            //跳轉(zhuǎn)到詳情關(guān)閉當(dāng)前頁面
            deatisur() {
                uni.redirectTo({
                    url: '/pages/datail/datail'
                })
            },
            //獲取子組件數(shù)據(jù):
            getNums(num) {
                console.log(num)
                this.num = num
            }

        }


    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    /* #ifdef  H5*/
    .text-area {
        width: 750rpx;
        height: 1350rpx;
        background: red;
    }

    /* #endif */


    /* #ifdef  MP-WEIXIN*/
    .text-area {
        width: 750rpx;
        height: 1350rpx;
        background: #007AFF;
    }

    /* #endif */

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }

    .box-active {
        background: red;
    }
</style>

<template>
    <view>
        a組件{{num}}
        <button @click="addNums">修改B組件數(shù)據(jù)</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
             num:1
            }
        },
    
        beforeCreate() {
            
            
        },
        methods: {
        addNums(){
            uni.$emit('updateNum',10)
        }
        },
        created() {
        
            
        },
        mounted() {
        
        }
    }
</script>

<style>

</style>

<template>
    <view>
        b組件{{num}}
    </view>
</template>

<script>
    export default {
        data() {
            return {
            num:2
            }
        },
    
        beforeCreate() {
            
            
        },
        methods: {
        
        },
        created() {
            uni.$on('updateNum',num=>{
                this.num+=num
            })
        
            
        },
        mounted() {
        
        }
    }
</script>

<style>

</style>

<template>
    <view>
        test
        <text>父組件傳遞的數(shù)據(jù):{{title}}</text>
        <button @click="dataFuter()">點擊(給父組件傳數(shù)據(jù))</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                nums:68
                
            }
        },
        props:['title'],
        beforeCreate() {
            console.log('組件初始化')
            
        },
        methods: {
            dataFuter(){
                //定義自定義事件
            this.$emit('myEdits',this.nums)
            }
            
        },
        created() {
                console.log('數(shù)據(jù)')
            
        },
        mounted() {
            console.log('demo')
        }
    }
</script>

<style>

</style>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市波附,隨后出現(xiàn)的幾起案子艺晴,更是在濱河造成了極大的恐慌,老刑警劉巖掸屡,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件封寞,死亡現(xiàn)場離奇詭異,居然都是意外死亡折晦,警方通過查閱死者的電腦和手機钥星,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來满着,“玉大人谦炒,你說我怎么就攤上這事》缋” “怎么了宁改?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長魂莫。 經(jīng)常有香客問我还蹲,道長,這世上最難降的妖魔是什么耙考? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任谜喊,我火速辦了婚禮,結(jié)果婚禮上倦始,老公的妹妹穿的比我還像新娘斗遏。我一直安慰自己,他們只是感情好鞋邑,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布诵次。 她就那樣靜靜地躺著账蓉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪逾一。 梳的紋絲不亂的頭發(fā)上铸本,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機與錄音遵堵,去河邊找鬼箱玷。 笑死,一個胖子當(dāng)著我的面吹牛鄙早,可吹牛的內(nèi)容都是我干的汪茧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼限番,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了呀舔?” 一聲冷哼從身側(cè)響起弥虐,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎媚赖,沒想到半個月后霜瘪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡惧磺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年颖对,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磨隘。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡缤底,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出番捂,到底是詐尸還是另有隱情个唧,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布设预,位于F島的核電站徙歼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鳖枕。R本人自食惡果不足惜魄梯,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宾符。 院中可真熱鬧酿秸,春花似錦、人聲如沸吸奴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至考润,卻和暖如春狭园,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背糊治。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工唱矛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人井辜。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓绎谦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親粥脚。 傳聞我的和親對象是個殘疾皇子窃肠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容