小編給大家分享一下vue+vueRouter+webpack的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

最近vue更新的2.0版本,唉,我是在2.0版本前學習的,現(xiàn)在更新了又要看一遍了,關鍵是我之前看了3個星期2.0就更新了,vux還沒同步更新,導致我用vux時要將vue的版本降回1.x,vue-router也要降回1.0才能使用~~~所以今天就寫一個單頁的下方tabbar的實例來記錄一下吧,也希望各位在用vue全家桶時少點坑吧,當然不是用vux= =…只是仿造而已
這里的demo我會使用vue2.0的simple-template作為腳手架,vue-router版本也是2.0的,如果想使用vux作為組件庫的話,大家就降版本吧~哦對了,如果大家正式寫項目的話,記得要用vuex,不是開玩笑,我之前寫了個簡單的單頁應用就沒用vuex也沒用組件庫都是手寫,然后組件之間的通信各種煩,你能想象一直向上廣播事件$boardCast之后,再一直向下分發(fā) $emit的無語嗎……到最后自己都亂了,所以不是自己寫demo而是開始項目的話還是推薦使用vuex了,用過react的同學的話就知道了,vuex跟redux是一樣的~只是一個用于vue,一個用于react而已.
好了,開始構建吧~
Prerequisites: Node.js (>=4.x, 6.x preferred) and Git.
前提當然是裝了node且版本已經(jīng)升級為6.x,在尤大大的vue-cli的使用教程中有說明的,對這里我們是采用自動化構建的方式創(chuàng)建配置模板
首先從零開始,打開打算創(chuàng)建的項目根目錄,再打開git的命令行吧~
1、全局安裝vue-cli腳手架
npm install -g vue-cli
2、初始化webpack+vue的腳手架模板,這里我是用的簡化版模板,不帶單元測試的~因為多出來的很多我看不懂……….簡化版的我大概能看懂,也是我菜的原因= =…
vue init webpack-simple <project-name>這里我定個名字就叫test吧
vue init webpack-simple test
3、按照步驟來就好
cd test
npm install這里會安裝babel、vue的加載器等各類依賴,這里要等一會,有點慢
npm run dev這里跑一下本地文件,看看是否搭建完成,如果出現(xiàn)vue的頁面就完畢了
4、安裝vue-router與需要的組件庫,這里我裝一個餓了么的組件庫ElementUI吧,地址http://element.eleme.io/,因為已經(jīng)兼容了vue2.0的版本,所以暫時拿來用用吧~官方文檔齊全,需要什么自己去看吧,我這里就簡單用一點
npm install vue-router npm i element-ui -D
5、記得安裝css的加載器,如果你是用less或者sass的話,自己對應裝了添加到加載器就好
npm install style-loader css-loader 如果沒錯的話,你的加載器現(xiàn)在應該是這樣的,最后在package.json里面依賴文件要加上element-ui
//package.json
"dependencies": {
"element-ui": "^1.0.4",
"vue": "^2.1.0"
}
//webpack.config.js
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
// vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{//添上這條規(guī)則,這是elementUI要用到的
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
}6、分模塊,寫組件
下面先展示我的文件目錄
test dist build.js node_modules … src App.vue discovery.vue index.vue info.vue main.js setting.vue .babelrc .gitignore index.html package.json README.md webpack.config.js
//App.vue(這里仿制vux的下方tabbar寫了一個組件,所以有點多,代碼有點爛,請原諒)
<template>
<div id="app">
<router-view></router-view>
<div class="tabbar" @click="select">
<router-link :class="{'selected':indexPage === 'index'}" to="/index">
<img src="https://o84lhz5xo.qnssl.com/master/src/assets/demo/icon_nav_button.png" alt="">
<label>主頁</label>
</router-link>
<router-link :class="{'selected':indexPage === 'info'}" to="/info">
<img src="https://o84lhz5xo.qnssl.com/master/src/assets/demo/icon_nav_msg.png" alt="">
<label>信息</label>
</router-link>
<router-link :class="{'selected':indexPage === 'discovery'}" to="/discovery">
<img src="https://o84lhz5xo.qnssl.com/master/src/assets/demo/icon_nav_article.png" alt="">
<label>發(fā)現(xiàn)</label>
</router-link>
<router-link :class="{'selected':indexPage === 'setting'}" to="/setting">
<img src="https://o84lhz5xo.qnssl.com/master/src/assets/demo/icon_nav_cell.png" alt="">
<label>設置</label>
</router-link>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
radio:'1',
indexPage:'index'
}
},
methods:{
select(event){
function findA(target){
if(target.nodeName != 'A'){
return findA(target.parentNode)
}
return target;
}
var modules = findA(event.target).lastElementChild.innerHTML;
if(modules == '主頁'){
this.indexPage='index';
}
else if(modules == '信息'){
this.indexPage='info';
}
else if(modules == '發(fā)現(xiàn)'){
this.indexPage='discovery';
}
else if(modules == '設置'){
this.indexPage='setting';
}
}
}
}
</script>
<style>
html,body{
margin:0;
padding:0;
}
#app {
font-family: 'microsoft yahei', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
.tabbar{
position: fixed;
bottom:0;
display: flex;
width:100%;
height:55px;
flex-direction:row;
background: rgba(247,247,250,.9);
font-size:12px;
}
.tabbar:before{
content: " ";
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 1px;
border-top: 1px solid #979797;
color: #979797;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleY(.5);
transform: scaleY(.5);
background-color: white;
}
.tabbar a{
flex:1;
color: #888;
}
.tabbar a img{
display: block;
width:24px;
height:24px;
margin:3px auto;
padding-top:5px;
}
.selected{
color: #09bb07 !important;
}
h2, h3 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
text-decoration: none;
}
</style>//index.vue(主頁模塊,套了一點elementUI,有點東西好看點= =..)
<template>
<div>
<h4>我是主頁模塊</h4>
<el-menu theme="dark" default-active="1" class="el-menu-demo" mode="horizontal" @select="handleSelect">
<el-menu-item index="1">處理中心</el-menu-item>
<el-submenu index="2">
<template slot="title">我的工作臺</template>
<el-menu-item index="2-1">選項1</el-menu-item>
<el-menu-item index="2-2">選項2</el-menu-item>
<el-menu-item index="2-3">選項3</el-menu-item>
</el-submenu>
<el-menu-item index="3">訂單管理</el-menu-item>
</el-menu>
</div>
</template>
<script>
export default {
methods:{
handleSelect:function(key,keyPath){
console.log(key,keyPath);
}
}
}
</script>//info.vue(主頁模塊,套了一點elementUI,有點東西好看點= =..)
<template>
<h4>{{msg}}</h4>
<div>
<el-alert
title="成功提示的文案"
type="success">
</el-alert>
<el-alert
title="消息提示的文案"
type="info">
</el-alert>
<el-alert
title="警告提示的文案"
type="warning">
</el-alert>
<el-alert
title="錯誤提示的文案"
type="error">
</el-alert>
</div>
</template>
<script>
export default {
data(){
return {
msg:'我是信息模塊'
}
}
}
</script>//discovery.vue(發(fā)現(xiàn)模塊)
<template>
<div>
<h3>{{msg}}</h3>
<el-steps :space="100" :active="active" finish-status="success">
<el-step title="步驟 1"></el-step>
<el-step title="步驟 2"></el-step>
<el-step title="步驟 3"></el-step>
</el-steps>
<el-button @click="next">下一步</el-button>
</div>
</template>
<script>
export default {
data(){
return {
active:0,
msg:'我是發(fā)現(xiàn)模塊'
}
},
methods:{
next:function(){
if(this.active++ > 2) this.active = 0
}
}
}
</script>//setting.vue(設置模塊)
<template>
<div class="block">
<h4>{{msg}}</h4>
<el-rate
v-model="value2"
:colors="['#99A9BF', '#F7BA2A', '#FF9900']"
:allow-half="true">
</el-rate>
<span>{{value2}}</span>
</div>
</template>
<script>
export default {
data() {
return {
value2: null,
msg:'我是設置模塊'
}
}
}
</script>//main.js(主文件,聲明全局router)
import Vue from 'vue'
import Router from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import App from './App.vue'
import index from './index.vue'
import info from './info.vue'
import discovery from './discovery.vue'
import setting from './setting.vue'
Vue.use(Router);
Vue.use(ElementUI);
const router = new Router({
routes:[
{
path:'/',
component:index
},
{
path:'/index',
component:index
},
{
path:'/info',
component:info
},
{
path:'/discovery',
component:discovery
},
{
path:'/setting',
component:setting
}
]
});
new Vue({
el: '#app',
render: h => h(App),
router:router
});最后就是webpack的入口文件必然是要改成main.js的,出口文件的文件夾為dist,名字就你自己定了,在index.html里加上就好~具體可以在我的另一篇筆記”初識webpack “中有寫過
最后npm run dev 查看效果就ok~如果想改綁定的端口號或者主機號,則在package.json中對應改就好
example:
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --port 8181 --open --inline --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
}其中端口號是dev中的 --port <port>,主機號則為--host <hostname/ip>就比如我這里則綁定的為8181端口。
最后給大家展示一下效果圖吧~沒看過vue-router的同學請自行看文檔= =…我這里只是最基礎的展示了而已
http://localhost:8181/#/index
http://localhost:8181/#/info
http://localhost:8181/#/discovery
以上是“vue+vueRouter+webpack的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁題目:vue+vueRouter+webpack的示例分析-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.chinadenli.net/article14/djcode.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站收錄、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設、用戶體驗、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容