Vue使用Vant框架


1. 安裝 Vue Cli

npm install -g @vue/cli

2.創建一個項目

vue create hello-world


3.創建完成后,可以通過命令打開圖形化界面,如下圖所示

vue ui


 

 訪問http://localhost:8000

 

 選擇剛才創建的helloword

 

選擇import

 

選擇依賴,安裝依賴

 

 

 

 安裝vant

 

 

 

 

4. 通過 npm 安裝

在現有項目中使用 Vant 時,可以通過npmyarn安裝

# 通過 npm 安裝 npm i vant -S # 通過 yarn 安裝 yarn add vant 

 5. babel-plugin-import 是一款 babel 插件,它會在編譯過程中將 import 的寫法自動轉換為按需引入的方式

# 安裝插件 npm i babel-plugin-import -D

6.
如果你的項目用中還沒有安裝vue-router可以先進行一個安裝:
npm install vue-router --save

7. 在babel.config.js 中添加配置
module.exports = {
  plugins: [
    ['import', {
      libraryName: 'vant',
      libraryDirectory: 'es',
      style: true
    }, 'vant']
  ]
};


8.在src下創建一個router的文件夾用來存放路由,在router文件夾下創建一個index.js用來配置路由,相當於路由的一個入口
import Vue from 'vue'
import Router from 'vue-router'

import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]
})

9.修改APP.vue

<template>
  <div>
    <p>This is Index</p>
    <ul>
      <li>
        <!--路由鏈接-->
        <router-link to="/home">Home</router-link>
      </li>
      <li>
        <router-link to="/about">about</router-link>
      </li>
    </ul>

    <div>
      <!-- 路由對應組件的顯示 -->
      <router-view></router-view>
    </div>
  </div>
</template>


<script>

export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 

10.在main.js中配置

import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = falsenew Vue({
  router,
  render: h => h(App),
}).$mount('#app')

11.使用vant main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import { Button } from 'vant';

Vue.config.productionTip = false

Vue.use(Button);
new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

helloword.vue

<template>
  <div class="hello">
    <van-button type="default">默認按鈕1</van-button>
    <van-button type="primary" @click="clickEvent">主要按鈕</van-button>
    <van-button type="info">信息按鈕</van-button>
    <van-button type="warning">警告按鈕</van-button>
    <van-button type="danger">危險按鈕</van-button>
  </div>
</template>

 運行:npm run serve

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM