從webpack打包結構中我們知道,vue中有一個存放外部資源的檔案夾static,它里面的檔案是不會被打包編譯的,所以我們就可以利用外部引入js的方式將我們的想要的資料在index.html中以js檔案的方式引入,然后就可以全域使用,
具體的方法如下:
1.在專案中找到static檔案夾,在里面創建一個組態檔,起名為config.js
┌── build
├── config
├── node_modules
├── src
├── static
│ ├── config.js // 組態檔
│
├── index.html:
2.然后在創建的config.js組態檔中,宣告一個物件api,將它賦值給window,物件名字隨便取,只要不占用關鍵字即可,物件中存放你所需要配置的各種路徑和資料,如下:
window.api = {
apiURL: 'http://xxx.xxx.xxx.xxx:xxxx',
websocketURL: 'ws://xxx.xxx.xxx.xxx:xxxx/websocket'
}
3.將此組態檔在index.html 檔案中引入
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗</title>
<script src=https://www.cnblogs.com/bunuo/p/"static/config.js"></script>
<body>
