关注各种黑科技
B站UP主,编程教学,游戏玩家,挂B,金牛座

sweetalert This seems to be a pre-built javascript file 报错

接手一个vue项目报错

WARNING in ./src/assets/js/plugins/sweetalert/sweetalert.min.js
Critical dependencies:
16:78-86 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
@ ./src/assets/js/plugins/sweetalert/sweetalert.min.js 16:78-86

项目里自己又包了一层sweetalert
sweetalert.js
import了min

    import Swal from  '../assets/js/plugins/sweetalert/sweetalert.min';
    import '../assets/js/plugins/sweetalert/sweetalert.css';
    import '../assets/js/plugins/sweetalert/themes/bootstrap/bootstrap.css';
    import Swal from  '../assets/js/plugins/sweetalert/sweetalert.min';

    export default {
      install: function (vue) {
        "use strict";            

          //   let defaultParams = {
          //     title: '',
          //     text: '',
          //     type: null,
          //     allowOutsideClick: false,
          //     showConfirmButton: true,
          //     showCancelButton: false,
          //     closeOnConfirm: true,
          //     closeOnCancel: true,
          //     confirmButtonText: 'OK',
          //     confirmButtonColor: '#8CD4F5',
          //     cancelButtonText: 'Cancel',
          //     imageUrl: null,
          //     imageSize: null,
          //     timer: null,
          //     customClass: '',
          //     html: false,
          //     animation: true,
          //     allowEscapeKey: true,
          //     inputType: 'text',
          //     inputPlaceholder: '',
          //     inputValue: '',
          //     showLoaderOnConfirm: false
          // };

          vue.prototype.$swal = Swal;

          // alert
          vue.prototype.$alert = function(title="确定要这样吗?", text, callbackOk) {

            if (typeof text === 'function') {
              callbackOk = arguments[1];
              text = undefined;
            }

            return Swal({
              title: title,
              text: text,
              confirmButtonText:'确定'
            },function(){
             typeof callbackOk  === 'function' && callbackOk();
           });

          };

        // 确认框
        vue.prototype.$confirm = function(title="确定要这样吗?", text, callbackOk, callbackCancel) {
          if (typeof text === 'function') {
            callbackCancel = arguments[2];
            callbackOk = arguments[1];
            text = undefined;
          }
          return Swal({
            title: title,
            text: text,            
            showCancelButton: true,
            cancelButtonText:'取消',
            confirmButtonText:'确定'
          },function(isConfirm){
            if (isConfirm) {
              typeof callbackOk  === 'function' && callbackOk();
            }else{
              typeof callbackCancel  === 'function' && callbackCancel();
            }

          })
        };

        // prompt
        vue.prototype.$prompt = function(title="确定要这样吗?", text, callbackOk, callbackCancel) {
          if (typeof text === 'function') {
            callbackCancel = arguments[2];
            callbackOk = arguments[1];
            text = undefined;
          }
          return Swal({
            title: title,
            text: text,
            type: "input",
            showCancelButton: true,
            cancelButtonText:'取消',
            confirmButtonText:'确定',
            closeOnConfirm: false,
          },function(inputValue ){
            if (inputValue === false) {   
              typeof callbackCancel  === 'function' && callbackCancel();            
              return false;
            }else{
              typeof callbackOk  === 'function' && callbackOk(inputValue);                  
            }                

          })
        };

        // toast
        vue.prototype.$toast = function(title,time=3000){
          return Swal({
            title: title,
            text: '',
            type: "success",
            showCancelButton: false,
            showConfirmButton:false,
            timer:time,
            allowOutsideClick:true
          });
        }

      }

    }

引用
import Swal from '../../plugins/sweetalert';

另外sweetalert更新到2.0了 Swal函数改成了小写swal
https://sweetalert.js.org/

还发现用webstrom格式化代码 会出现Module build failed: SyntaxError: Unterminated string constant (267:126) 错误。vue这种混写htmljs的代码真是垃圾设计。

参考
https://segmentfault.com/q/1010000003047566

转载请注明出处
分享到: 更多 (0)

评论 0