示例代碼

2020-12-29 10:53 更新

本頁面演示如何使用avm.js語法的兩種模式之一,單文件模式,定義一個頁面或者組件,并渲染到終端。

使用單文件模式,stml文件定義一個組件 / 頁面

?stml組件 / 頁面符合Vue單文件組件(SFC)規(guī)范,最終被編譯為JS組件 / 頁面,渲染到不同終端。

定義頁面或組件:

  1. // api-test.stml:
  2. <template>
  3. <view>
  4. <text>Hello APP</text>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'ApiTest'
  10. }
  11. </script>

添加樣式:

  1. <template>
  2. <view class='header'>
  3. <text>Hello APP</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'ApiTest'
  9. }
  10. </script>
  11. <style>
  12. .header{
  13. height: 45px;
  14. }
  15. </style>

數(shù)據(jù)綁定:

  1. <template>
  2. <view class='header'>
  3. <text>{this.data.title}</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'ApiTest',
  9. data(){
  10. return {
  11. title: 'Hello APP'
  12. }
  13. }
  14. }
  15. </script>
  16. <style>
  17. .header{
  18. height: 45px;
  19. }
  20. </style>

stml定義組件 / 頁面在被編譯為js組件過程中,會自動生成渲染代碼。

API和模塊使用

?avm.js兼容和繼承APICloud所有API、模塊、技術(shù)棧以及開發(fā)體驗,因此,api以及模塊的使用及調(diào)試方式遵循APICloud現(xiàn)有標(biāo)準(zhǔn)及習(xí)慣。

api對象使用:

  1. <template>
  2. <view class='header'>
  3. <text>{this.data.title}</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'ApiTest',
  9. apiready(){
  10. api.toast({msg: '網(wǎng)絡(luò)錯誤'});
  11. this.data.title = '網(wǎng)絡(luò)錯誤';
  12. },
  13. data(){
  14. return {
  15. title: 'Hello APP'
  16. }
  17. }
  18. }
  19. </script>
  20. <style>
  21. .header{
  22. height: 45px;
  23. }
  24. </style>

模塊使用:

  1. <template>
  2. <view class='header'>
  3. <text>{this.data.title}</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'ApiTest',
  9. apiready(){
  10. var mam = api.require('mam');
  11. mam.checkUpdate(function(ret, err){
  12. if (ret) {
  13. api.toast({msg: '成功'});
  14. } else {
  15. api.toast({msg: '失敗'});
  16. }
  17. });
  18. },
  19. data(){
  20. return {
  21. title: 'Hello APP'
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. .header{
  28. height: 45px;
  29. }
  30. </style>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號