SolidZORO
V2EX  ›  问与答

請教 js 模塊化問題。各模塊 js 被 gulp 壓成 app.js 後, HTML 頁面如何判斷並讀取 app.js 的其中一塊代碼?

  •  
  •   SolidZORO · May 14, 2016 · 1735 views
    This topic created in 3680 days ago, the information mentioned may be changed or developed.

    比如有 article, product 兩個模塊

    目錄結構:

    - article
    |---- index.html
    |---- article.js
    |---- article.css
    - product
    |---- index.html
    |---- product.js
    |---- product.css
    

    載入任意模塊 index.html 都只讀取 app.js ,比如讀取 article/index.html 的時候,只想 article/article.js 裡的代碼,而 product/product.js 不要執行。

    現在我的做法是:

    1 / 在 article/index.html<body> 上加入 class ,變成 <body class="body-article">

    2 / article/article.js 裡 寫上

    if (!$_body.hasClass("body-article")) {
    	return false;
    }
    

    但感覺這樣的方法不規範,這導致 IDE (eg: PhpStorm) 不能很好的分析 js 之間的關係。請問怎樣的寫法才規範, IDE 能正常分析 js 呢?

    1 replies    2016-05-14 14:48:35 +08:00
    hxtheone
        1
    hxtheone  
       May 14, 2016
    这个感觉也只能从代码层面做隔离, 话说为什么有这种需求呢? 全局变量被污染了?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1495 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:55 · PVG 00:55 · LAX 09:55 · JFK 12:55
    ♥ Do have faith in what you're doing.