2017-02-03 6 views
1

私のコードスニペットはこのように見えますが、私はpackage.jsonのsweetalertを持っています。reactJs、Redux、import sweetalertの使用中にウィンドウが定義されていません

import swal from 'sweetalert'; export function delete() { 
return (dispatch, getState) => { 
    swal({ 
      title: "Delete API?", 
      text: "You will not be able to recover this API!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: false 
     });} 

ウィンドウが定義されていないエラーがパブリック/ server.js

if(window.console){ 

では、これはsweetalertにrealtedされます。

私はしばらくの間、この作業を持っていましたが、私が行った時にきれいなNPMの実行がそれを構築するには、私はいくつかの提案をする場合、サーバー側でそれをレンダリングしないように見server.jsを置き換え、コードのこの新しいラインはserver.js

に追加されますreact/reduxを使用していますが、レンダリング中にsweetalertパッケージをロードしない方法を探しています。

ご協力いただきまして誠にありがとうございます。ありがとう

+0

オリジナルのSweetalertプラグインはサポートされていません。[Sweetalert2](https://github.com/limonte/sweetalert2)をご覧ください。 –

+0

@limonte私もsweetalert2を試しました。上記。私はあなたがsweetalert2で多く働いているのを見ました。私はあなたがそのパッケージを所有していると思います。私がsweetalert2を使用しているときに、public/server.js – NagaRajendra

答えて

0

webpackbuildsが生成され、public/server.jsを上回ったときにreactal/reduxが "sweetalert"をレンダリングしようとしていますが、サーバーサイドのレンダリングを停止するためにwindowを見つけられず、 sweetalert、私はこれは私が何それがないと、サーバーサイドでsweetalertをレンダリング停止され、githubの上のいくつかのスレッドでそれを見つけた私のために働いた

const isBrowser = typeof window !== 'undefined'; 
const swal = isBrowser ? require('sweetalert') : undefined; 

import swal from "sweetalert"; 

を置き換えます。お役に立てれば!!

関連する問題