使用webpack搭建vue開發環境

來源:互聯網
上載者:User

標籤:

最近幾天項目上使用了vue.js作為一個主要的開發架構,並且為了發布的方便搭配了webpack一起使用。CSS架構使用的是vue-strap(vue 對bootstrap控制項做了封裝)
這篇文章主要總結一下具體搭建的過程,和途中遇到的一些問題的解決辦法

主要用到的工具
  • Vue
  • webpack
  • vue-strap
  • vue-router
搭建步驟1. 首先建立項目的目錄結構
  • demo_project/
    • dist/ webpack產生的檔案
    • src/ 原始碼檔案
      • img/ 圖片檔案
      • css/ css檔案
      • js/ js檔案
      • components/ 定義的vue組件
      • views/ 視圖檔案,其實也是vue組件
    • webpack.config.js webpack設定檔
2. 安裝vue 和webpack
# 進入到 demo_project 目錄下
npm install vue --save
npm install webpack --save
3. 添加webpack設定檔
module.exports = {
entry: ‘./src/index.js‘,
output: {
path: __dirname + ‘/dist‘,
filename: ‘bundle.js‘
},
module: {
loaders: [{
test: /\.css$/,
loader: ‘style!css‘
}]
}
}
4. 添加package.json檔案
# cd 到 demo_project 目錄下
npm init #跟著步驟一步一步的完成

最後產生的package.json檔案如下

{
"name": "vuedemo",
"version": "1.0.0",
"description": "a vue demo with webpack",
"main": "webpack.config.js",
"dependencies": {
"vue": "^1.0.4",
"webpack": "^1.12.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start":"webpack --color --progress"
//注意,這是我產生後加上的。用於執行webpack.如果webpack 安裝在全域可以直接行動webpack進行打包
},
"author": "frank"
}

 

5. 新增html和js入口檔案
<!-- src/index.html -->
<html>
<head>
<meta charset="utf-8">
</head>

<body>
<script type="text/javascript" src="../dist/bundle.js"></script>
</body>
/* src/index.js  */
document.write(‘hello. webpack runs ok!‘)
6. 驗證webpack是否成功
npm start

在瀏覽器中開啟index.html .看到hello. webpack runs ok! 表示webpack成功了運行了

後面就是使用vue的loader載入的vue檔案了.下次再寫
接著來安裝 vue-loader

7.安裝vue-loader
  • 安裝

    npm install vue-loader --save
  • 修改設定檔

    //webpack.config.js
    loaders[
    ...
    {
    test:/\.vue$/,
    loader:‘vue‘
    }
    ...
  • 在views目錄下建立測試的vue檔案

    <!-- hello.vue -->
    <template>
    <strong>{{vueMsg}}</strong>
    </template>
    <script>
    module.exports = {
    data() {
    return {vueMsg:‘Vue hello world‘}
    }
    }
    </script>
    <style scoped>
    strong{

    }
    </style>
  • 在index.js檔案中引入vue

    //index.js
    var Vue = require(‘vue‘)
    var helloVue = require(‘./views/hello.vue‘)
    new Vue({
    el: "body",
    components: {
    hello: helloVue
    }
    })
  • 修改index.html加入body中加入demo的vue節點

    ...
    <body>
    <hello></hello>
    ...
    </body>
    ...
8.測試vue
#根目錄執行
npm start

開啟index.html,看到紅色背景的Vue hello world

使用webpack搭建vue開發環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.