Vue.js - webpack相關配置
注意:
有時候使用npm i node-sass -D
裝不上,這時候,就必須使用 cnpm i node-sass -D
在普通頁面中使用render函數渲染組件
在webpack中配置.vue組件頁面的解析
-
運行
cnpm i vue -S
將vue安裝爲運行依賴; -
運行
cnpm i vue-loader vue-template-compiler -D
將解析轉換vue的包安裝爲開發依賴; -
運行
cnpm i style-loader css-loader -D
將解析轉換CSS的包安裝爲開發依賴,因爲.vue文件中會寫CSS樣式; -
在
webpack.config.js
中,添加如下module
規則:
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
{ test: /\.vue$/, use: 'vue-loader' }
]
}
- 創建
App.js
組件頁面:
<template>
<!-- 注意:在 .vue 的組件中,template 中必須有且只有唯一的根元素進行包裹,一般都用 div 當作唯一的根元素 -->
<div>
<h1>這是APP組件 - {{msg}}</h1>
<h3>我是h3</h3>
</div>
</template>
<script>
// 注意:在 .vue 的組件中,通過 script 標籤來定義組件的行爲,需要使用 ES6 中提供的 export default 方式,導出一個vue實例對象
export default {
data() {
return {
msg: 'OK'
}
}
}
</script>
<style scoped>
h1 {
color: red;
}
</style>
- 創建
main.js
入口文件:
// 導入 Vue 組件
import Vue from 'vue'
// 導入 App組件
import App from './components/App.vue'
// 創建一個 Vue 實例,使用 render 函數,渲染指定的組件
var vm = new Vue({
el: '#app',
render: c => c(App)
});
在使用webpack構建的Vue項目中使用模板對象?
- 在
webpack.config.js
中添加resolve
屬性:
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
ES6中語法使用總結
-
使用
export default
和export
導出模塊中的成員; 對應ES5中的module.exports
和export
-
使用
import ** from **
和import '路徑'
還有import {a, b} from '模塊標識'
導入其他模塊 -
使用箭頭函數:
(a, b)=> { return a-b; }
在vue組件頁面中,集成vue-router路由模塊
- 導入路由模塊:
import VueRouter from 'vue-router'
- 安裝路由模塊:
Vue.use(VueRouter);
- 導入需要展示的組件:
import login from './components/account/login.vue'
import register from './components/account/register.vue'
- 創建路由對象:
var router = new VueRouter({
routes: [
{ path: '/', redirect: '/login' },
{ path: '/login', component: login },
{ path: '/register', component: register }
]
});
- 將路由對象,掛載到 Vue 實例上:
var vm = new Vue({
el: '#app',
// render: c => { return c(App) }
render(c) {
return c(App);
},
router // 將路由對象,掛載到 Vue 實例上
});
- 改造App.vue組件,在 template 中,添加
router-link
和router-view
:
<router-link to="/login">登錄</router-link>
<router-link to="/register">註冊</router-link>
<router-view></router-view>
組件中的css作用域問題
抽離路由爲單獨的模塊
使用 餓了麼的 MintUI 組件
- 導入所有MintUI組件:
import MintUI from 'mint-ui'
- 導入樣式表:
import 'mint-ui/lib/style.css'
- 在 vue 中使用 MintUI:
Vue.use(MintUI)
- 使用的例子:
<mt-button type="primary" size="large">primary</mt-button>
使用 MUI 組件
- 導入 MUI 的樣式表:
import '../lib/mui/css/mui.min.css'
- 在
webpack.config.js
中添加新的loader規則:
{ test: /\.(png|jpg|gif|ttf)$/, use: 'url-loader' }
- 根據官方提供的文檔和example,嘗試使用相關的組件
將項目源碼託管到oschina中
-
點擊頭像 -> 修改資料 -> SSH公鑰 如何生成SSH公鑰
-
創建自己的空倉儲,使用
git config --global user.name "用戶名"
和git config --global user.email ***@**.com
來全局配置提交時用戶的名稱和郵箱 -
使用
git init
在本地初始化項目 -
使用
touch README.md
和touch .gitignore
來創建項目的說明文件和忽略文件; -
使用
git add .
將所有文件託管到 git 中 -
使用
git commit -m "init project"
將項目進行本地提交 -
使用
git remote add origin 倉儲地址
將本地項目和遠程倉儲連接,並使用origin最爲遠程倉儲的別名 -
使用
git push -u origin master
將本地代碼push到倉儲中
App.vue 組件的基本設置
-
頭部的固定導航欄使用
Mint-UI
的Header
組件; -
底部的頁籤使用
mui
的tabbar
; -
購物車的圖標,使用
icons-extra
中的mui-icon-extra mui-icon-extra-cart
,同時,應該把其依賴的字體圖標文件mui-icons-extra.ttf
,複製到fonts
目錄下! -
將底部的頁籤,改造成
router-link
來實現單頁面的切換; -
Tab Bar 路由激活時候設置高亮的兩種方式:
- 全局設置樣式如下:
.router-link-active{
color:#007aff !important;
}
- 或者在
new VueRouter
的時候,通過linkActiveClass
來指定高亮的類:
// 創建路由對象
var router = new VueRouter({
routes: [
{ path: '/', redirect: '/home' }
],
linkActiveClass: 'mui-active'
});
實現 tabbar 頁籤不同組件頁面的切換
-
將 tabbar 改造成
router-link
形式,並指定每個連接的to
屬性; -
在入口文件中導入需要展示的組件,並創建路由對象:
// 導入需要展示的組件
import Home from './components/home/home.vue'
import Member from './components/member/member.vue'
import Shopcar from './components/shopcar/shopcar.vue'
import Search from './components/search/search.vue'
// 創建路由對象
var router = new VueRouter({
routes: [
{ path: '/', redirect: '/home' },
{ path: '/home', component: Home },
{ path: '/member', component: Member },
{ path: '/shopcar', component: Shopcar },
{ path: '/search', component: Search }
],
linkActiveClass: 'mui-active'
});
使用 mt-swipe 輪播圖組件
- 假數據:
lunbo: [
'http://www.itcast.cn/images/slidead/BEIJING/2017440109442800.jpg',
'http://www.itcast.cn/images/slidead/BEIJING/2017511009514700.jpg',
'http://www.itcast.cn/images/slidead/BEIJING/2017421414422600.jpg'
]
- 引入輪播圖組件:
<!-- Mint-UI 輪播圖組件 -->
<div class="home-swipe">
<mt-swipe :auto="4000">
<mt-swipe-item v-for="(item, i) in lunbo" :key="i">
<img :src="item" alt="">
</mt-swipe-item>
</mt-swipe>
</div>
</div>
在.vue
組件中使用vue-resource
獲取數據
-
運行
cnpm i vue-resource -S
安裝模塊 -
導入 vue-resource 組件
import VueResource from 'vue-resource'
- 在vue中使用 vue-resource 組件
Vue.use(VueResource);