stml頁面

2020-12-29 10:53 更新

stml介紹 stml(single template markup language)文件是一個專用的文件類型,其結(jié)構(gòu)和Vue的單文件組件 (SFC)相似,用類Html語法描述一個組件/頁面。

一個stml文件一般包含三種類型的頂級語言塊<template>,<script>和<style>,同時支持引入外部js或者css文件,以及其他stml組件。

stml文件最終被分別編譯為APP、小程序代碼。

App端加載stml頁面的方式

在config.xml中全局配置avm字段。配置后打開頁面默認(rèn)使用原生渲染引擎,當(dāng)打開標(biāo)準(zhǔn)H5頁面時,需將openWin、openFrame等方法的avm參數(shù)設(shè)置為false。

  1. <preference name="avm" value="true"/>

使用openWin、openFrame等方法打開頁面時設(shè)置avm參數(shù)。

  1. api.openWin({
  2. name: 'test',
  3. url: '../pages/test.stml',
  4. avm: true
  5. });

代碼示例

一個典型的.stml文件代碼如下:

  1. <template>
  2. <view>
  3. <view class="header">
  4. <text>{this.data.title}</text>
  5. </view>
  6. <view class="content">
  7. <text>{this.data.content}</text>
  8. </view>
  9. <view class="footer">
  10. <text>{this.data.footer}</text>
  11. </view>
  12. </view>
  13. </template>
  14. <style>
  15. .header {
  16. height: 45px;
  17. }
  18. .content {
  19. flex-direction:row;
  20. }
  21. .footer {
  22. height: 55px;
  23. }
  24. </style>
  25. <script>
  26. export default {
  27. name: 'api-test',
  28. apiready(){
  29. console.log("Hello APICloud");
  30. },
  31. data(){
  32. return {
  33. title: 'Hello App',
  34. content: 'this is content',
  35. footer: 'this is footer'
  36. }
  37. }
  38. }
  39. </script>

代碼塊描述 <template>:stml文件允許最多包含一個template塊,且template下僅允許包含一個子節(jié)點。template塊類似于標(biāo)準(zhǔn)HTML中的body標(biāo)簽。

<script>:stml文件允許最多包含一個script塊,script塊類似于標(biāo)準(zhǔn)Html的script標(biāo)簽。該塊內(nèi)可使用import方式引入外部js,被引入的js內(nèi)容將被編譯到最終的js組件中。例如:

  1. <script>
  2. import './path/utils.js';
  3. export default {
  4. name: 'api-test',
  5. }
  6. </script>

<style>:stml文件允許包含一個或多個style塊,style塊類似于標(biāo)準(zhǔn)Html的style標(biāo)簽。該塊支持以src的方式引入外部css,被引入的css內(nèi)容將被編譯到最終的js組件中。例如:

  1. <style src='./path/common.css' />
  2. <style>
  3. .header{
  4. height: 45px;
  5. }
  6. </style>

編譯器會解析stml文件,提取每個語言塊,分別編譯導(dǎo)出為APP(JS組件 / 頁面)、H5(SPA)、小程序(WXML / WXSS / JS)代碼,用于不同終端的渲染。

編譯后的App js代碼參考:

  1. class ApiTest extends Component {
  2. data = {
  3. title: 'Hello App',
  4. content: 'this is content',
  5. footer: 'this is footer'
  6. }
  7. apiready () {
  8. console.log("Hello APICloud");
  9. }
  10. render() {
  11. return (
  12. <view>
  13. <view class='header'>
  14. <text>{this.data.title}</text>
  15. </view>
  16. <view class='content'>
  17. <text>{this.data.content}</text>
  18. </view>
  19. <view class='footer'>
  20. <text>{this.data.footer}</text>
  21. </view>
  22. </view>
  23. );
  24. }
  25. }
  26. ApiTest.css = {
  27. '.header': {
  28. height: '45px'
  29. },
  30. '.content': {
  31. flex: 1
  32. },
  33. '.footer': {
  34. height: '55px'
  35. }
  36. }
  37. avm.define('api-test', ApiTest);
  38. avm.render(<api-test />, 'body');

該代碼為編譯中間過程的臨時代碼,是ES6規(guī)范的代碼,符合avm.js單語言片段模式要求。在輸出之前,還會經(jīng)過一系列的處理,最終被編譯為ES5規(guī)范的代碼,用于DeepEngine或者標(biāo)準(zhǔn)瀏覽器執(zhí)行。

avm.define avm.define函數(shù)用于聲明或者定義一個組件。接收兩個參數(shù),組件名(例如"a-button")和組件對象。

avm.render avm.render函數(shù)用于聲明將已定義的組件渲染到某元素上。默認(rèn)渲染到body,body為窗口或者頁面的根元素。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號