前期介紹:
Vue是什么,是一套構(gòu)建用戶界面的漸進(jìn)式框架
Vue兩大核心思想,組件化和數(shù)據(jù)驅(qū)動,組件化就是將一個整體合理拆分為一個一個小塊(組件),組件可重復(fù)使用,數(shù)據(jù)驅(qū)動是前端的未來發(fā)展方向,釋放了對DOM的操作,讓DOM隨著數(shù)據(jù)的變化自然而然的變化(尤神原話),不必過多的關(guān)注DOM,只需要將數(shù)據(jù)組織好即可。
模型為 mvvm
環(huán)境準(zhǔn)備:
安裝Node.js(最好版本不要太老, >=4.x , 6.x)和Git
node.js地址 https://nodejs.org/en/download/
git地址 https://git-scm.com/downloads? ? https://git-for-windows.github.io/
一、利用vue-cli構(gòu)建vue項(xiàng)目
顧名思義,第一件事就是安裝vue-cli
在某個文件夾下(工作空間)執(zhí)行命令 ? $ ?npm install -g vue-cli ??
注:-g為全局安裝,如果是mac按如下寫法:sudo npm install -g vue-cli
然后在創(chuàng)建項(xiàng)目(或者說模版項(xiàng)目)命令格式:vue init <template-name> <project-name> ?--- ?vue init 模版名稱 項(xiàng)目名稱(小寫英文字母)
執(zhí)行命令 vue init webpack vuetest ?命令窗口會出現(xiàn)提示 (全部默認(rèn)enter 就可以了)提示如下
?Project name (vuetest) ??
? Project name vuetest
? Project description (A Vue.js project)
? Project description A Vue.js project
? Author (songxibin)
? Author songxibin
? Vue build standalone
下邊的提示案情況選擇,是和否都可以試試,提示以此為vue-router(路由) eslint(代碼檢查)。。。等等,不在贅述
此時項(xiàng)目創(chuàng)建完畢,ctrl+c退出創(chuàng)建 cd vuetest進(jìn)入剛才創(chuàng)建的vuetest項(xiàng)目
執(zhí)行命令 npm install 安裝依賴,
依賴安裝完畢執(zhí)行命令 npm run dev 運(yùn)行項(xiàng)目,默認(rèn)為本地8080端口
二、vue2 + webpack 構(gòu)建
首先創(chuàng)建項(xiàng)目文件夾,進(jìn)入文件(空文件)
打開控制臺或git ?安裝webpack 和 webpack-dev-server
執(zhí)行命令 npm install -g webpack webpack-dev-server
項(xiàng)目初始化? 執(zhí)行命令 npm init -y
//項(xiàng)目目錄下安裝依賴
npm install webpack webpack-dev-server?style-loader css-loader babel-core babel-loader babel-plugin-transform-runtime babel-preset-es2015 babel-preset-stage-0 babel-runtime --save-dev
//vue相關(guān)依賴
npm install vue vue-loader vue-html-loader vue-style-loader ?vue-hot-reload-api vue-template-compiler --save-dev
//安裝vue
npm install vue --save
配置文件 webpack.config.js 內(nèi)容
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './src/app.js',
output: {
/* 輸出目錄,沒有則新建 */
path: path.resolve(__dirname, './dist'),
/* 靜態(tài)目錄,可以直接從這里取文件 */
publicPath: '/dist/',
/* 文件名 */
filename: 'build.js'
},
module: {
rules: [
/* 用來解析vue后綴的文件 */
{
test: /\.vue$/,
loader: 'vue-loader'
},
/* 用babel來解析js文件并把es6的語法轉(zhuǎn)換成瀏覽器認(rèn)識的語法 */
{
test: /\.js$/,
loader: 'babel-loader',
/* 排除模塊安裝目錄的文件 */
exclude: /node_modules/
}
]
}
}
app.js內(nèi)容
import Vue from 'vue'
//import?Hello?from?"../../src/components/Hello.vue";
//使用全局函數(shù)注入組件,就不用import和在創(chuàng)建Vue對象時定義components鍵值
Vue.component('Hello',?require("../../src/components/Hello.vue"));
new?Vue({
el:?"#app",
//定義template可以不用在html中插入""
//template:?'',
//components:?{?Hello?}
});
Hello.vue 內(nèi)容
{{msg}}
body{
background-color:#eee;
}
export?default{
data(){
return{
msg:'this?is?template?body?vue'
}
}
}
index.html 內(nèi)容
<!doctype html>
<html> <head> <meta charset='utf-8'> <meta name='viewport' content='width=device-width,initial-scale=1.0,user-scalable=0,mininum-scale=1.0,maxinum-scale=1.0'>
<title>vue-webpack</title>
</head>
<body><div id='app'></div>
<script src='./dist/build.js'></script>
</body>
</html>