VantWeapp的dialog組件在mpvue小程序中使

問題

Dialog組件支持函數(shù)調用和組件調用兩種形式,而一般的組件僅支持后者。顯然,函數(shù)調用方式的支持增加了組件使用的靈活性,但是也隨之出現(xiàn)另外幾個值得注意的問題。

成都創(chuàng)新互聯(lián)公司提供高防服務器租用、云服務器、香港服務器、成都移動機房托管

兩種方式使用舉例

在我的mpvue工程測試中,針對dialog組件我專門創(chuàng)建了一個測試文件夾test_dialog,其中包含如下三個文件:

  • index.vue
  • main.js
  • main.json

上述三個文件的作用相信各位都明白。注意,我把vant-weapp組件庫×××后存放到static目錄下:
/static/vant/各個組件對應子文件夾。

其中,main.json內容如下:

{
  "navigationBarTitleText": "test_tabbar_page",
  "usingComponents": {
    "van-button": "/static/vant/button/index",
    "van-icon": "/static/vant/icon/index",
    "van-area": "/static/vant/area/index",
    "van-dialog":"/static/vant/dialog/index",
    "van-field": "/static/vant/field/index"
  }
}

main.js文件內容固定不變。
index.vue文件內容如下:

<template>
  <div>
    <div>
      <van-button
        plain
        type="primary"
        class="demo-margin-right"
        @click="onClickAlert"
      >
        消息提示
      </van-button>
      <van-dialog id="van-dialog" />
    </div>

    <div >
        <van-button
          plain
          type="danger"
          @click="showCustomDialog"
        >
          組件調用
        </van-button>
      <van-dialog
        use-slot
        async-close
        :show="show"
        show-cancel-button
        confirm-button-open-type="getUserInfo"
        @close="onClose"
        @getuserinfo="getUserInfo"
      >
        <van-field
          :value="username"
          label="用戶名"
          placeholder="請輸入用戶名"
        />
        <van-field
          :value="password"
          type="password"
          label="密碼"
          border="false"
          placeholder="請輸入密碼"
        />
      </van-dialog>
    </div>
  </div>
</template>

<script>
  import Dialog from '@/../static/vant/dialog/dialog'
  const message = '有贊是一家零售科技公司,致力于成為商家服務領域里最被信任的引領者'
  export default {
    data: {
      show: false,
      username: '',
      password: ''
    },
    methods:{
      onClickAlert(){
        Dialog.alert({
          title: '標題',
          message
        })
      },
      showCustomDialog() {
        this.show=true
      },
      getUserInfo(event) {
        console.log(event.mp.detail)
      },
      onClose(event) {
        if (event.mp.detail === 'confirm') {
          // 異步關閉彈窗
          setTimeout(() => {
            this.show=false
          }, 1000);
        } else {
          this.show=false
        }
      }
    }
  }
</script>

為了對比方便,我在上述頁面中既使用了組件調用方式又使用了函數(shù)調用方式。其中,組件調用方式大家都熟悉,不必贅述。
值得注意的是后者。

函數(shù)調用方式使用注意事項

有如下幾點:

1,必須放置一個dialog的聲明方式定義:
<van-dialog id="van-dialog" />

2,使用import命令中不能使用絕對路徑方式:

import Dialog from '@/../static/vant/dialog/dialog'

這里的@代表項目中的src目錄。

然后就可以使用更靈活的函數(shù)調用方式了:
Dialog.alert({
title: '標題',
message
})

名稱欄目:VantWeapp的dialog組件在mpvue小程序中使
網(wǎng)頁路徑:http://bm7419.com/article8/jjehip.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、定制開發(fā)、網(wǎng)站設計公司、、外貿建站、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化