欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue webpack多頁面構建的實例代碼

 更新時間:2018年09月11日 14:32:05   作者:ccyinghua  
這篇文章主要介紹了vue webpack多頁面構建的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下

項目示例地址: https://github.com/ccyinghua/webpack-multipage

項目運行:

下載項目之后

# 下載依賴
npm install
# 運行
npm run dev
http://localhost:3000/login.html
http://localhost:3000/index.html

一、開發(fā)環(huán)境

node v6.11.0

二、安裝vue-cli腳手架

npm install vue-cli@2.8.2 -g

三、初始化項目

vue init webpack webpack-multipage // 創(chuàng)建項目
cd webpack-multipage // 進入webpack-multipage目錄
npm install // 下載依賴
npm run dev // 運行

http://localhost:8080

四、修改配置支持多頁面

將項目根目錄index.html,src下的文件刪除,重新調(diào)整的src結構目錄:

|-- src
 |-- assets
 |-- components
 |-- entry
  |-- index // index模塊
   |-- components
    |-- Hello.vue
   |-- router
    |-- index.js
   |-- index.html
   |-- index.js
   |-- index.vue
  |-- login // login模塊
   |-- login.html
   |-- login.js
   |-- login.vue

(1) 修改build/util.js,在文件最后添加

# 先下載glob組件
npm install glob -D

將目錄映射成配置。如./src/entry/login/login.js變成映射{login: './src/entry/login/login.js'}

var glob = require('glob');
exports.getEntries = function (globPath) {
 var entries = {}
 glob.sync(globPath).forEach(function (entry) {
 var basename = path.basename(entry, path.extname(entry), 'router.js');
 entries[basename] = entry
 });
 return entries;
}

(2) 修改build/webpack.base.conf.js,找到entry屬性,使用了uitls.js文件中新添加的方法getEntries,將entry中的js都映射成程序的入口

module.exports = {
 entry: utils.getEntries('./src/entry/*/*.js'),
 ...
}

(3) 修改build/webpack.dev.conf.js

刪除文件內(nèi)原有的HtmlWebpackPlugin相關內(nèi)容

...
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
 filename: 'index.html',
 template: 'index.html',
 inject: true
}),
...

在文件最后添加

var pages = utils.getEntries('./src/entry/*/*.html')
for(var page in pages) {
 // 配置生成的html文件,定義路徑等
 var conf = {
 filename: page + '.html',
 template: pages[page], //模板路徑
 inject: true,
 // excludeChunks 允許跳過某些chunks, 而chunks告訴插件要引用entry里面的哪幾個入口
 // 如何更好的理解這塊呢?舉個例子:比如本demo中包含兩個模塊(index和about),最好的當然是各個模塊引入自己所需的js,
 // 而不是每個頁面都引入所有的js,你可以把下面這個excludeChunks去掉,然后npm run build,然后看編譯出來的index.html和about.html就知道了
 // filter:將數(shù)據(jù)過濾,然后返回符合要求的數(shù)據(jù),Object.keys是獲取JSON對象中的每個key
 excludeChunks: Object.keys(pages).filter(item => {
  return (item != page)
 })
 }
 // 需要生成幾個html文件,就配置幾個HtmlWebpackPlugin對象
 devWebpackConfig.plugins.push(new HtmlWebpackPlugin(conf))
}

(4) 修改build/webpack.prod.conf.js

刪除文件內(nèi)原有的HtmlWebpackPlugin相關內(nèi)容

...
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
 filename: config.build.index,
 template: 'index.html',
 inject: true,
 minify: {
  removeComments: true,
  collapseWhitespace: true,
  removeAttributeQuotes: true
  // more options:
  // https://github.com/kangax/html-minifier#options-quick-reference
 },
 // necessary to consistently work with multiple chunks via CommonsChunkPlugin
 chunksSortMode: 'dependency'
}),
...

在文件最后添加

var pages = utils.getEntries('./src/entry/*/*.html')
for(var page in pages) {
 // 配置生成的html文件,定義路徑等
 var conf = {
 filename: page + '.html',
 template: pages[page], //模板路徑
 inject: true,
 // excludeChunks 允許跳過某些chunks, 而chunks告訴插件要引用entry里面的哪幾個入口
 // 如何更好的理解這塊呢?舉個例子:比如本demo中包含兩個模塊(index和about),最好的當然是各個模塊引入自己所需的js,
 // 而不是每個頁面都引入所有的js,你可以把下面這個excludeChunks去掉,然后npm run build,然后看編譯出來的index.html和about.html就知道了
 // filter:將數(shù)據(jù)過濾,然后返回符合要求的數(shù)據(jù),Object.keys是獲取JSON對象中的每個key
 excludeChunks: Object.keys(pages).filter(item => {
  return (item != page)
 }),
 minify: {
  removeComments: true,
  collapseWhitespace: true,
  removeAttributeQuotes: true
  // more options:
  // https://github.com/kangax/html-minifier#options-quick-reference
  },
  // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  chunksSortMode: 'dependency'
 }
 // 需要生成幾個html文件,就配置幾個HtmlWebpackPlugin對象
 module.exports.plugins.push(new HtmlWebpackPlugin(conf))
}

(5)修改config/index.js

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
 dev: {
 env: require('./dev.env'), // 引入當前目錄下的dev.env.js,用來指明開發(fā)環(huán)境
 port: 3000, // dev-server的端口號,可以自行更改
 autoOpenBrowser: true, // 是否自定代開瀏覽器
 // Paths
 assetsSubDirectory: 'static',
 assetsPublicPath: '/',
 // 下面是代理表,作用是用來,建一個虛擬api服務器用來代理本機的請求,只能用于開發(fā)模式
 proxyTable: {
  "/demo/api":"http://localhost:8080"
 },
 // Various Dev Server settings
 host: 'localhost', // can be overwritten by process.env.HOST
 autoOpenBrowser: false,
 errorOverlay: true,
 notifyOnErrors: true,
 poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
 /**
  * Source Maps
  */
 // https://webpack.js.org/configuration/devtool/#development
 devtool: 'cheap-module-eval-source-map',
 // If you have problems debugging vue-files in devtools,
 // set this to false - it *may* help
 // https://vue-loader.vuejs.org/en/options.html#cachebusting
 cacheBusting: true,
 // CSS Sourcemaps off by default because relative paths are "buggy"
 // with this option, according to the CSS-Loader README
 // (https://github.com/webpack/css-loader#sourcemaps)
 // In our experience, they generally work as expected,
 // just be aware of this issue when enabling this option.
 // 是否生成css,map文件,上面這段英文就是說使用這個cssmap可能存在問題,但是按照經(jīng)驗,問題不大,可以使用
 cssSourceMap: false
 },
 build: {
 env: require('./prod.env'), // 導入prod.env.js配置文件,只要用來指定當前環(huán)境
 // Template for index.html
 index: path.resolve(__dirname, '../dist/index.html'), // 相對路徑的拼接
 // Paths
 assetsRoot: path.resolve(__dirname, '../dist'), // 靜態(tài)資源的根目錄 也就是dist目錄
 assetsSubDirectory: 'static', // 靜態(tài)資源根目錄的子目錄static,也就是dist目錄下面的static
 assetsPublicPath: '/', // 靜態(tài)資源的公開路徑,也就是真正的引用路徑
 /**
  * Source Maps
  */
 productionSourceMap: true, // 改成false運行時不會出現(xiàn)map調(diào)試文件。;是否生成生產(chǎn)環(huán)境的sourcmap,sourcmap是用來debug編譯后文件的,通過映射到編譯前文件來實現(xiàn)
 // https://webpack.js.org/configuration/devtool/#production
 devtool: '#source-map',
 // Gzip off by default as many popular static hosts such as
 // Surge or Netlify already gzip all static assets for you.
 // Before setting to `true`, make sure to:
 // npm install --save-dev compression-webpack-plugin
 productionGzip: false, // 是否在生產(chǎn)環(huán)境中壓縮代碼,如果要壓縮必須安裝compression-webpack-plugin
 productionGzipExtensions: ['js', 'css'], // 定義要壓縮哪些類型的文件
 // Run the build command with an extra argument to
 // View the bundle analyzer report after build finishes:
 // `npm run build --report`
 // Set to `true` or `false` to always turn it on or off
 // 下面是用來開啟編譯完成后的報告,可以通過設置值為true和false來開啟或關閉
 // 下面的process.env.npm_config_report表示定義的一個npm_config_report環(huán)境變量,可以自行設置
 bundleAnalyzerReport: process.env.npm_config_report
 }
}

assetsRoot:執(zhí)行npm run build之后,項目生成的文件放到哪個目錄中。vue生成的文件都是靜態(tài)文件,可以放在nginx中,也可以放到Spring Boot項目的resources/static目錄中。
assetsPublicPath:項目的根路徑。注意,這個屬性在build、dev兩個環(huán)境都有,修改時,應該同時修改兩處。
port:這里改成3000,這個是在開發(fā)時,webpack-dev-server運行的端口。
proxyTable:這個屬性用于將請求轉發(fā)到指定地址去。這里的配置意思是將所有以/demo/api開頭的請求,都轉發(fā)到http://localhost:8080地址。

五、建立頁面

index/index.html

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>index</title>
</head>
<body>
 <div id="app"></div>
</body>
</html>

index/index.js

import Vue from 'vue'
import IndexView from './index.vue'
import router from './router'
// import VueResource from 'vue-resource'; // 使用前先npm install vue-resource --save下載vue-resource
// Vue.use(VueResource);
new Vue({
 el: '#app',
 router,
 render: h => h(IndexView)
});

index/index.vue

<template>
 <div>
 <router-view></router-view>
 </div>
</template>

<script>
 export default {
 }
</script>

<style>
</style>

index/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Hello from '../components/Hello.vue'
Vue.use(Router);
export default new Router({
 routes: [
 {
  path: '/',
  name: 'Hello',
  component: Hello
 }
 ]
})

index/components/Hello.vue

<template>
 <div>
 Hello {{ name }}
 </div>
</template>

<script>
 export default {
 data(){
  return {
  name: "admin"
  }
 },
 mounted(){
  //this.$http.get("/demo/api/userinfo").then(resp =>{
  // this.name = resp.data.data;
  //});
 }
 }
</script>
<style>
</style>

login/login.html

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>login</title>
</head>
<body>
 <div id="app"></div>
</body>
</html>

login/login.js

import Vue from 'vue'
import LoginView from './login.vue'
// import VueResource from 'vue-resource';
// Vue.use(VueResource);
new Vue({
 el: '#app',
 render: h => h(LoginView)
})

login/login.vue

<template>
 <div>
 <form id="login-form">
  <label for="username">用戶名:</label>
  <input type="text" id="username" name="username">
  <br>
  <label for="password">密碼:</label>
  <input type="password" id="password" name="password"><br>
  <br>
  <button @click.prevent="submit">登錄</button>
 </form>
 </div>
</template>
<script>
 export default {
 methods:{
  submit(){
  window.location = "/demo/index.html";
  //let formData = new FormData(document.getElementById("login-form"));
  //this.$http.post("/demo/api/login", formData).then(resp => {
  // if (resp.data.status === 200){
  // window.location = "/index.html";
  // }else {
  // alert(resp.data.desc);
  // }
  //})
  }
 }
 }
</script>
<style>
</style>

六、運行

http://localhost:3000/login.html

http://localhost:3000/index.html

總結

以上所述是小編給大家介紹的vue webpack多頁面構建的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • Vue3從0搭建Vite打包組件庫使用詳解

    Vue3從0搭建Vite打包組件庫使用詳解

    這篇文章主要為大家介紹了Vue3從0搭建Vite打包組件庫使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 使用vue-cli3+typescript的項目模板創(chuàng)建工程的教程

    使用vue-cli3+typescript的項目模板創(chuàng)建工程的教程

    這篇文章主要介紹了使用vue-cli3+typescript的項目模板創(chuàng)建工程,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • JS圖片懶加載庫VueLazyLoad詳解

    JS圖片懶加載庫VueLazyLoad詳解

    這篇文章主要為大家介紹了JS圖片懶加載庫VueLazyLoad示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 詳解Vue 如何監(jiān)聽Array的變化

    詳解Vue 如何監(jiān)聽Array的變化

    這篇文章主要介紹了詳解Vue 如何監(jiān)聽Array的變化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • Vue實現(xiàn)插槽下渲染dom字符串的使用

    Vue實現(xiàn)插槽下渲染dom字符串的使用

    本文主要介紹了Vue實現(xiàn)插槽下渲染dom字符串的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • v-slot和slot、slot-scope之間相互替換實例

    v-slot和slot、slot-scope之間相互替換實例

    這篇文章主要介紹了v-slot和slot、slot-scope之間相互替換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 前端Vue3項目打包成Docker鏡像運行的詳細步驟

    前端Vue3項目打包成Docker鏡像運行的詳細步驟

    將Vue3項目打包、編寫Dockerfile、構建Docker鏡像和運行容器是部署Vue3項目到Docker的主要步驟,這篇文章主要介紹了前端Vue3項目打包成Docker鏡像運行的詳細步驟,需要的朋友可以參考下
    2024-09-09
  • 使用vue實現(xiàn)猜謎卡片游戲

    使用vue實現(xiàn)猜謎卡片游戲

    這篇文章主要為大家詳細介紹了如何使用vue實現(xiàn)簡單的猜謎卡片游戲,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以參考一下
    2023-09-09
  • Vue.extend實現(xiàn)掛載到實例上的方法

    Vue.extend實現(xiàn)掛載到實例上的方法

    這篇文章主要介紹了Vue.extend實現(xiàn)掛載到實例上的方法,結合實例形式分析了Vue.extend實現(xiàn)掛載到實例上的具體操作步驟與相關實現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • 基于Vue實現(xiàn)手勢簽名

    基于Vue實現(xiàn)手勢簽名

    這篇文章主要為大家詳細介紹了基于Vue實現(xiàn)手勢簽名,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論