Vue.js - webpack相關配置

Vue.js - webpack相關配置

注意:

有時候使用npm i node-sass -D裝不上,這時候,就必須使用 cnpm i node-sass -D

在普通頁面中使用render函數渲染組件

在webpack中配置.vue組件頁面的解析

  1. 運行cnpm i vue -S將vue安裝爲運行依賴;

  2. 運行cnpm i vue-loader vue-template-compiler -D將解析轉換vue的包安裝爲開發依賴;

  3. 運行cnpm i style-loader css-loader -D將解析轉換CSS的包安裝爲開發依賴,因爲.vue文件中會寫CSS樣式;

  4. webpack.config.js中,添加如下module規則:


module: {

    rules: [

      { test: /\.css$/, use: ['style-loader', 'css-loader'] },

      { test: /\.vue$/, use: 'vue-loader' }

    ]

  }

  1. 創建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>

  1. 創建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項目中使用模板對象?

  1. webpack.config.js中添加resolve屬性:
resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }

ES6中語法使用總結

  1. 使用 export defaultexport 導出模塊中的成員; 對應ES5中的 module.exportsexport

  2. 使用 import ** from **import '路徑' 還有 import {a, b} from '模塊標識' 導入其他模塊

  3. 使用箭頭函數:(a, b)=> { return a-b; }

在vue組件頁面中,集成vue-router路由模塊

vue-router官網

  1. 導入路由模塊:

import VueRouter from 'vue-router'

  1. 安裝路由模塊:

Vue.use(VueRouter);

  1. 導入需要展示的組件:

import login from './components/account/login.vue'

import register from './components/account/register.vue'

  1. 創建路由對象:

var router = new VueRouter({

  routes: [

    { path: '/', redirect: '/login' },

    { path: '/login', component: login },

    { path: '/register', component: register }

  ]

});

  1. 將路由對象,掛載到 Vue 實例上:

var vm = new Vue({

  el: '#app',

  // render: c => { return c(App) }

  render(c) {

    return c(App);

  },

  router // 將路由對象,掛載到 Vue 實例上

});

  1. 改造App.vue組件,在 template 中,添加router-linkrouter-view

    <router-link to="/login">登錄</router-link>

    <router-link to="/register">註冊</router-link>



    <router-view></router-view>

組件中的css作用域問題

抽離路由爲單獨的模塊

使用 餓了麼的 MintUI 組件

Github 倉儲地址

Mint-UI官方文檔

  1. 導入所有MintUI組件:

import MintUI from 'mint-ui'

  1. 導入樣式表:

import 'mint-ui/lib/style.css'

  1. 在 vue 中使用 MintUI:

Vue.use(MintUI)

  1. 使用的例子:

<mt-button type="primary" size="large">primary</mt-button>

使用 MUI 組件

官網首頁

文檔地址

  1. 導入 MUI 的樣式表:

import '../lib/mui/css/mui.min.css'

  1. webpack.config.js中添加新的loader規則:

{ test: /\.(png|jpg|gif|ttf)$/, use: 'url-loader' }

  1. 根據官方提供的文檔和example,嘗試使用相關的組件

將項目源碼託管到oschina中

  1. 點擊頭像 -> 修改資料 -> SSH公鑰 如何生成SSH公鑰

  2. 創建自己的空倉儲,使用 git config --global user.name "用戶名"git config --global user.email ***@**.com 來全局配置提交時用戶的名稱和郵箱

  3. 使用 git init 在本地初始化項目

  4. 使用 touch README.mdtouch .gitignore 來創建項目的說明文件和忽略文件;

  5. 使用 git add . 將所有文件託管到 git 中

  6. 使用 git commit -m "init project" 將項目進行本地提交

  7. 使用 git remote add origin 倉儲地址將本地項目和遠程倉儲連接,並使用origin最爲遠程倉儲的別名

  8. 使用 git push -u origin master 將本地代碼push到倉儲中

App.vue 組件的基本設置

  1. 頭部的固定導航欄使用 Mint-UIHeader 組件;

  2. 底部的頁籤使用 muitabbar;

  3. 購物車的圖標,使用 icons-extra 中的 mui-icon-extra mui-icon-extra-cart,同時,應該把其依賴的字體圖標文件 mui-icons-extra.ttf,複製到 fonts 目錄下!

  4. 將底部的頁籤,改造成 router-link 來實現單頁面的切換;

  5. Tab Bar 路由激活時候設置高亮的兩種方式:

  • 全局設置樣式如下:

 	.router-link-active{

      	color:#007aff !important;

    }

  • 或者在 new VueRouter 的時候,通過 linkActiveClass 來指定高亮的類:

 	// 創建路由對象

    var router = new VueRouter({

      routes: [

        { path: '/', redirect: '/home' }

      ],

      linkActiveClass: 'mui-active'

    });

實現 tabbar 頁籤不同組件頁面的切換

  1. 將 tabbar 改造成 router-link 形式,並指定每個連接的 to 屬性;

  2. 在入口文件中導入需要展示的組件,並創建路由對象:


    // 導入需要展示的組件

    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 輪播圖組件

  1. 假數據:

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'

      ]

  1. 引入輪播圖組件:

<!-- 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獲取數據

  1. 運行cnpm i vue-resource -S安裝模塊

  2. 導入 vue-resource 組件


import VueResource from 'vue-resource'

  1. 在vue中使用 vue-resource 組件

Vue.use(VueResource);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章