uniapp升級到Vue3自定義封裝的js文件導出方式與Vue2的格式不一樣
封裝js的 export 導出和 在其他頁面使用import引入的方式導致的報錯
Vue2
/** * 消息框,錯誤框,確認框,等待框等封裝 */ import base from '@/common/js-base.js'; let alert = { /** * @description 提示消息,一會就自動消失 * @param {string} msg 要顯示的消息 * @param {number} second 顯示時間(毫秒,默認1000秒) */ showInfo: function(msg, second) { if (base.isNull(second)) second = 1000; uni.showToast({ title: msg, icon: 'none', duration: second }); }, /** * @description 提示錯誤消息,需要點擊確認后關閉 * @param {string} msg 錯誤消息 * @param {type} title 錯誤標題[默認'提示'] */ showError: function(msg, title) { if (base.isNull(title)) title = "提示"; uni.showModal({ title: title, content: msg, showCancel: false }); } } export default alert;
Vue3
/** * 消息框,錯誤框,確認框,等待框等封裝 */ import * as base from '@/common/js-base.js'; /** * @description 提示消息,一會就自動消失 * @param {string} msg 要顯示的消息 * @param {number} second 顯示時間(毫秒,默認1000秒) */ function showInfo(msg, second) { if (base.isNull(second)) second = 1000; uni.showToast({ title: msg, icon: 'none', duration: second }); } /** * @description 提示錯誤消息,需要點擊確認后關閉 * @param {string} msg 錯誤消息 * @param {type} title 錯誤標題[默認'提示'] */ function showError(msg, title) { if (base.isNull(title)) title = "提示"; uni.showModal({ title: title, content: msg, showCancel: false }); } export{ showInfo, showError }
Vue3導入封裝方法:
import * as base from '@/common/js-base.js'
不能使用以下方式,因為封裝方法沒使用 export default
import base from '@/common/js-base.js'