uni-app中使用renderjs的詳細(xì)步驟講解
1.為什么要使用 renderjs
- 某些h5端使用的插件涉及到大量的dom操作,無(wú)法跨端使用。
- 直接運(yùn)行在視圖層,解決了視圖層與邏輯層頻繁通信導(dǎo)致的性能折損,讓動(dòng)畫更流暢。
- renderjs是一種可以直接運(yùn)行在視圖層(webview)中的js技術(shù),可以在視圖層操作dom。
2.兼容性
App | H5 |
2.5.5+,僅支持vue | √ |
3.使用方式
設(shè)置 script 節(jié)點(diǎn)的 lang 為 renderjs
注意:module="openlayers" 中 openlayers 這是一個(gè)自定義屬性,用于指定腳本模塊的名稱或標(biāo)識(shí)符,寫什么都可以。
<script module="openlayers" lang="renderjs"> export default { mounted() { // ... }, methods: { // ... } } </script>
4.注意事項(xiàng)
- 目前僅支持內(nèi)聯(lián)使用。
- 不要直接引用大型類庫(kù),推薦通過(guò)動(dòng)態(tài)創(chuàng)建 script 方式引用。
- 可以使用 vue 組件的生命周期(不支持 beforeDestroy、destroyed、beforeUnmount、unmounted),不可以使用 App、Page 的生命周期
- 視圖層和邏輯層通訊方式與 WXS 一致,另外可以通過(guò) this.$ownerInstance 獲取當(dāng)前組件的 ComponentDescriptor 實(shí)例。
- this.$ownerInstance.callMethod() 僅支持調(diào)用邏輯層vue選項(xiàng)式中的 methods 中定義的方法。
- 注意邏輯層給數(shù)據(jù)時(shí)最好一次性給到渲染層,而不是不停從邏輯層向渲染層發(fā)消息,那樣還是會(huì)產(chǎn)生邏輯層和視圖層的多次通信,還是會(huì)卡
- 觀測(cè)更新的數(shù)據(jù)在視圖層可以直接訪問(wèn)到。
- APP 端視圖層的頁(yè)面引用資源的路徑相對(duì)于根目錄計(jì)算,例如:./static/test.js。
- APP 端可以使用 dom、bom API,不可直接訪問(wèn)邏輯層數(shù)據(jù),不可以使用 uni 相關(guān)接口(如:uni.request)
- H5 端邏輯層和視圖層實(shí)際運(yùn)行在同一個(gè)環(huán)境中,相當(dāng)于使用 mixin 方式,可以直接訪問(wèn)邏輯層數(shù)據(jù)。
- vue3 項(xiàng)目不支持
setup script
用法。
5.renderjs模塊內(nèi)的生命周期
<!-- H5:所有 UNI 框架的生命周期都可使用 APP:僅可使用 VUE 組件生命周期 視圖層與邏輯層可以重復(fù)定義生命周期,都會(huì)執(zhí)行。 --> <!-- 邏輯層 --> <script> export default { onLoad() { console.log('邏輯層生命周期 - onLoad'); }, created() { console.log('邏輯層生命周期 - created'); }, mounted() { console.log('邏輯層生命周期 - mounted'); } } </script> <!-- 視圖層 vue3版本下不會(huì)走下面的方法 --> <script module="openlayers" lang="renderjs" type="module" > export default { onLoad() { console.log('視圖層生命周期 - onLoad'); // 頁(yè)面生命周期 APP 不會(huì)執(zhí)行 H5 會(huì)執(zhí)行 }, created() { console.log('視圖層生命周期 - created'); // 組件生命周期 H5 APP 都會(huì)執(zhí)行,重復(fù)定義也會(huì)執(zhí)行,不會(huì)被覆蓋。 }, mounted() { console.log('視圖層生命周期 - mounted'); // 組件生命周期 H5 APP 都會(huì)執(zhí)行,重復(fù)定義也會(huì)執(zhí)行,不會(huì)被覆蓋。 } } </script>
H5效果圖
APP效果圖(我這里用的夜神模擬器)
6.renderjs模塊內(nèi)的this指向
<!-- 邏輯層 --> <script> export default { data() { return { str:"邏輯層的數(shù)據(jù)" } }, methods: { test() { return "調(diào)用邏輯層的方法" } } } </script> <!-- 視圖層 --> <script module="openlayers" lang="renderjs" type="module" > export default { mounted() { console.log("嘗試獲取邏輯層的數(shù)據(jù)",this.str) // H5端可正常打印 APP打印undefined console.log("嘗試調(diào)用邏輯層的方法",this.test()) // H5端可正常調(diào)用 APP端報(bào)錯(cuò) } } </script>
H5效果圖
APP效果圖(我這里用的夜神模擬器)
7.視圖層和邏輯層的通信方式
- 視圖層可以通過(guò)
this.$ownerInstance.callMethod('方法名', 傳的值)
來(lái)和邏輯層進(jìn)行通信。- 代碼中
:變量名="變量值" :change:變量名="renderjs模塊上的方法"
- change 就是監(jiān)聽(tīng)定義的變量發(fā)生改變 -> 觸發(fā)視圖層上的方法 -> 通過(guò)
this.$ownerInstance.callMethod('方法名', 傳的值)
給頁(yè)面?zhèn)鲄?-> 頁(yè)面接收值 -> 渲染頁(yè)面。
<template> <view> <!-- 監(jiān)聽(tīng)變量 operation 的變化,operation 發(fā)生改變時(shí),調(diào)用 openlayers 模塊的 loadOperation 方法 --> <view :operation="operation" :change:operation="openlayers.loadOperation"></view> <view>點(diǎn)擊了 {{total}} 次</view> <button @click="onClick">點(diǎn)擊</button> </view> </template> <script> export default { data() { return { operation: true, total: 0, } }, methods: { /** * 點(diǎn)擊事件 */ onClick() { this.operation = !this.operation }, /** * 接收 renderjs 傳過(guò)來(lái)的數(shù)據(jù) * @param {Number} num 接收的內(nèi)容 */ reciveMessage(data) { this.total = data; } } } </script> <script module="openlayers" lang="renderjs"> export default { data() { return { count: 0 } }, methods: { /** * @param {*} newValue 新的值或狀態(tài) * @param {*} oldValue 舊的值或狀態(tài) * @param {*} ownerInstance 擁有該數(shù)據(jù)或組件的實(shí)例 * @param {*} instance 當(dāng)前操作的具體實(shí)例 */ loadOperation(newValue, oldValue, ownerInstance, instance) { console.log(newValue, oldValue, ownerInstance, instance); this.$ownerInstance.callMethod('reciveMessage', ++this.count); // 向uni-app頁(yè)面組件發(fā)送信息 }, } } </script>
8.調(diào)用視圖層renderJS中的方法
例:當(dāng)前要增加一個(gè)地圖放大功能,之前的寫法是通過(guò)調(diào)用邏輯層的事件改變對(duì)象里的值觸發(fā)視圖層change事件,達(dá)到觸發(fā)地圖的方法。
現(xiàn)在可改成在點(diǎn)擊事件內(nèi)直接觸發(fā)視圖層的方法,以此來(lái)調(diào)用地圖方法,不用在經(jīng)過(guò)邏輯層和視圖層通信這一步驟
<template> <view> <view id="mymap" class="mymap"></view> <view class="but_sty"> <button @click="openlayers.zoomIn">放大</button> <button @click="openlayers.zoomOut">縮小</button> </view> </view> </template> <script> export default { data() { return {} } }, </script> <script module="openlayers" lang="renderjs"> import { mapObject } from "@/common/openlayers/openlayerMap.js"; export default { methods: { /** * 地圖放大 */ zoomIn() { console.log('放大'); mapObject.mapZoom('in'); }, /** * 地圖縮小 */ zoomOut() { console.log('縮小'); mapObject.mapZoom('out'); }, } } </script>
總結(jié)
到此這篇關(guān)于uni-app中使用renderjs的文章就介紹到這了,更多相關(guān)uni-app使用renderjs內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS控制只能輸入數(shù)字并且最多允許小數(shù)點(diǎn)兩位
這篇文章主要介紹了JS控制只能輸入數(shù)字并且最多允許小數(shù)點(diǎn)兩位,本文給大家提到j(luò)s如何限制input輸入框只能輸入數(shù)字問(wèn)題,需要的朋友可以參考下2019-11-11JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法(jQuery和class)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法:jQuery和class,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12前端根據(jù)鏈接生成二維碼的方案及相關(guān)常用API
這篇文章主要介紹了qrcode插件的使用方法,包括ES6/ES7示例、常用API及Vue示例,同時(shí)還討論了二維碼的優(yōu)點(diǎn)和缺點(diǎn),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01Bootstrap實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄效果
這篇文章主要介紹了Bootstrap實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄效果,導(dǎo)航欄是一個(gè)很好的功能,是Bootstrap網(wǎng)站的一個(gè)突出特點(diǎn),本文帶領(lǐng)大家學(xué)習(xí)實(shí)現(xiàn)Bootstrap導(dǎo)航欄,需要的朋友可以參考下2015-12-12Babylon使用麥克風(fēng)并處理常見(jiàn)問(wèn)題解決
這篇文章主要為大家介紹了Babylon使用麥克風(fēng)并處理常見(jiàn)問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04你可能不需要在JavaScript使用switch語(yǔ)句
這篇文章主要介紹了你可能不需要在JavaScript使用switch語(yǔ)句,對(duì)switch性能感興趣的同學(xué),可以參考下2021-04-04利用js實(shí)現(xiàn)前后臺(tái)傳送Json的示例代碼
下面小編就為大家分享一篇利用js實(shí)現(xiàn)前后臺(tái)傳送Json的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03List Information About the Binary Files Used by an Applicati
List Information About the Binary Files Used by an Application...2007-06-06