vue-awesome-swiper的用法&同一页面有多个swiper如何使用
前言:
swiper.js 的vue版api跟cdn引入事一样的api用法,共用官网那套api文档,此篇写下时,swiper.js 的版本是 Swiper4.x 。 这篇用的也是4.x的版本,注意swiper4.x跟swiper3.x的api用法有部分不同,详细请参考swiper官网。
用 npm 安装:
npm install vue-awesome-swiper --save
全局注册 ,main.js 中
import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default global options } */)
局部组件注册 , 在每个自定义 .vue 文件里
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
components: {
swiper,
swiperSlide
}
}
在 SPA(single page web application 单页面应用)的组件中使用
<!-- The ref attr used to find the swiper instance -->
<template>
<swiper :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback">
<!-- slides -->
<swiper-slide>I'm Slide 1</swiper-slide>
<swiper-slide>I'm Slide 2</swiper-slide>
<swiper-slide>I'm Slide 3</swiper-slide>
<swiper-slide>I'm Slide 4</swiper-slide>
<swiper-slide>I'm Slide 5</swiper-slide>
<swiper-slide>I'm Slide 6</swiper-slide>
<swiper-slide>I'm Slide 7</swiper-slide>
<!-- Optional controls -->
<div class="swiper-pagination" slot="pagination"></div>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
<div class="swiper-scrollbar" slot="scrollbar"></div>
</swiper>
</template>
<script>
export default {
name: 'carrousel',
data() {
return {
swiperOption: {
// some swiper options/callbacks
// 所有的参数同 swiper 官方 api 参数
// ...
}
}
},
computed: {
swiper() {
return this.$refs.mySwiper.swiper
}
},
mounted() {
// current swiper instance
// 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
console.log('this is current swiper instance object', this.swiper)
this.swiper.slideTo(3, 1000, false)
}
}
</script>
以下是一个demo,效果是这样的:
情景:
第1个swiper,是左右划的独立的swiper,
第2个swiper 是上下划的,
第3个swiper 是左右划的,嵌套在第2个swiper里面。
第1个swiper的slide可点击,与第2个swiper联动 。
同一个页面里有三个 swiper 的 demo
项目结构是这样的:(刚创建的项目里没有dist这个文件夹,dist是打包后的项目文件夹)
项目结构
完整的代码是这样的,包含html、js、css ,文章末尾附上了vue router的配置内容,以及package.json 的内容 :
main.js 内容如下:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
import axios from 'axios'
import FastClick from 'fastclick'
import {ToastPlugin,LoadingPlugin,AlertPlugin} from 'vux'
Vue.use(VueAwesomeSwiper)
Vue.use(ToastPlugin)
Vue.use(LoadingPlugin)
Vue.use(AlertPlugin)
Vue.config.productionTip = false
axios.defaults.withCredentials=true
Vue.prototype.$ajax = axios
Vue.prototype.host = 'http://xxx.xxx.com' //接口base地址
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
/**
* 点击延迟
*/
FastClick.attach(document.body)
App.vue 的内容是这样的
<template>
<div id="app">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
body {
margin: 0;
padding: 0;
}
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>
home.vue 的内容是这样的(使用vue-awesome-swiper的页面)
<template>
<div class="wrap" v-show="isShowWrap">
<div class="top-menu" ref="topMenu">
<p class="header">
我是
</p>
<div class="menu-block">
<swiper :options="menuListOption" ref="menuListSwiper">
<swiper-slide v-for="item in menuItems" :key="item.id" :data-id="item.id"
:style="{backgroundImage:'linear-gradient(135deg,'+item.colour1+','+item.colour2+')'}">
<div v-if="item.name.length>3" class="double-text">
<span>{{item.name.substring(0,2)}}</span>
<span>{{item.name.substring(2,item.name.length)}}</span>
</div>
<span v-else class="single-text">{{item.name}}</span>
</swiper-slide>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</div>
</div>
<div class="content" :style="{height:menuContentHeight+'px'}">
<swiper :options="menuContentOption" ref="menuContentSwiper">
<swiper-slide v-for="(mItem,mIndex) in menuItems" :key="mItem.id" :data-page="mItem.totalPage"
:class="[mIndex==0?'mc-paddingtop':'']" :data-id="mItem.id">
<div class="mc-top">
<div class="icon-main" :style="{backgroundImage:'linear-gradient(135deg,'+mItem.colour1+','+mItem.colour2+')'}"
v-if="mItem.name.length>3" >
<span class="icon-main-double">{{mItem.name.substring(0,2)}}</span>
<span class="icon-main-double">{{mItem.name.substring(2,mItem.name.length)}}</span>
</div>
<span class="icon-main icon-main-single" :style="{backgroundImage:'linear-gradient(135deg,'+mItem.colour1+','+mItem.colour2+')'}" v-else>{{mItem.name}}</span>
<span class="icon-sub" @click="jumpToJoinUs(mItem.id,mItem.name)">加入我们</span>
</div>
<div v-if="mItem.userList.length!='0'">
<swiper :options="menuDetailOption" class="md-wrap" ref="menuDetailSwiper">
<swiper-slide v-for="(item,index) in mItem.userList" :key="index">
<div class="mc-middle">
<div class="the-desc" v-if="typeof item.backUrl!='undefined' && item.backUrl!='' && item.backUrl!=null">
<img :src="item.backUrl" alt="">
</div>
<div class="the-desc" v-else>
<span :style="'color:'+mItem.fontColor">{{item.remark}}</span>
<img :src="mItem.backUrl" alt="">
</div>
<div class="the-intro">
<div class="img-user" v-if="item.logoUrl!=='' && item.logoUrl!==null&& item.logoUrl!==undefined"
:style="{backgroundImage:'url('+item.logoUrl+')'}">
</div>
<div class="img-user" v-else :style="{backgroundImage:'url('+defaultLogo+')'}"></div>
<div class="the-intro-block">
<span class="txt-name">{{item.name}}</span>
<a :href="'tel:'+item.mobile" class="txt-tel" v-show="item.mobile!==''&& item.mobile!==null&& item.mobile!==undefined">{{item.mobile}}</a>
</div>
<div class="the-erweima">
<img :src="item.qrImgUrl" alt="" v-if="item.qrImgUrl!=='' && item.qrImgUrl!==null&& item.qrImgUrl!==undefined">
</div>
</div>
</div>
</swiper-slide>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</div>
<div v-else>
<swiper :options="menuDetailOption" class="md-wrap" ref="menuDetailSwiper">
<swiper-slide>
<div class="mc-middle">
<div class="the-desc">
<img :src="menuItems[mIndex].backUrl" alt="" v-if="menuItems[mIndex].backUrl!=='' && menuItems[mIndex].backUrl!==null&& menuItems[mIndex].backUrl!==undefined">
<img :src="defaultMCbg" alt="" v-else>
<span :style="{color:menuItems[mIndex].fontColor}">{{mItem.nullData}}</span>
</div>
<div class="the-intro">
<div class="img-user" :style="{backgroundImage:'url('+defaultLogo+')'}"></div>
<div class="the-intro-block">
<span class="txt-name">虚位以待</span>
</div>
<div class="the-erweima">
<img :src="defaultEwm" alt="">
</div>
</div>
</div>
</swiper-slide>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</div>
<span class="mc-bottom" v-if="mIndex<menuItems.length"></span>
</swiper-slide>
</swiper>
<div class="transform-icon">
<img :src="defaultIconJiantou" alt="">
</div>
</div>
</div>
</template>
<script>
import { setTimeout } from 'timers'; //setTimeout 所在库,不引用会报错
import { debug } from 'util'; //debug模式,代码调试语句debugger; 这个库不引用会报错
export default {
data() {
const vm = this; // 很重要,不然在swiper插件的事件里拿不到vue的实例对象
return {
isShowWrap: false,
menuListSwiper: '',
menuContentSwiper: '',
isSwipe: false, // 自定义变量,区分是否是滑动触发,onReachEnd事件需要用到,因为onReachBeginning/End 事件在组件初始化的时候会被执行,不知道为毛。
pageSize: 10,
menuListOption: {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
slidesPerView: 'auto',
spaceBetween: 9,
on: {
init: function() {},
tap: function(event) {
if (this.clickedIndex != undefined) {
vm.theIndexSlideTo = this.clickedIndex;
vm.menuItemIdClicked = this.clickedSlide.dataset.id;
vm.menuContentSwiper.slideTo(this.clickedIndex);
}
}
}
},
menuContentOption: {
direction: 'vertical',
spaceBetween: 0
},
menuDetailOption: {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
on: {
touchStart: function() {
vm.isSwipe = true;
},
reachEnd: function() {
if (vm.isSwipe) {
//获取上下滑动的那个swiper
let menuContentSwiper = vm.menuContentSwiper;
//获取上下滑动的那个swiper当前slide是哪一个,即获取activeIndex
let mcActiveIndex = menuContentSwiper.activeIndex;
//获取roleId
let roleId = menuContentSwiper.slides[mcActiveIndex].dataset.id;
//获取当前角色下的用户总数totalPage
let totalPage =
menuContentSwiper.slides[mcActiveIndex].dataset.page;
let theItems = vm.menuContentItems[0][mcActiveIndex];
//如果已经加载了全部,则不继续请求,否则请求下一页
let theItemsLen=theItems.length;
if (theItemsLen < totalPage) {
let pageIndex = theItemsLen / 10;
vm.getUserByRole(roleId, pageIndex).then(data => {
let userList=data.userList;
//因为vue里面concat() 不会更新数组触发Dom更新,所以用Vue.set()循环合并新旧数组。
for(let k=0;k<userList.length;k++){
vm.$set(theItems,(theItemsLen+k),userList[k]);
}
});
}
}
}
}
},
windowHeight: '',
topMenuHeight: '', //顶部列表高度
menuContentHeight: '', //除列表外的视口中间这部分高度
menuItems: [],
theIndexSlideTo: '', //顶部菜单被点击的slide的index
menuItemIdClicked: '', //顶部菜单被点击的角色id
menuContentItems: [], //二元数组,每个角色对应一个数组元素
defaultMCbg: require('../../../static/img/mc-bg.jpg'), //由于服务器根目录下有其他项目,故这个项目只好放服务的一个子目录下,于是需要改config文件夹下的index.js 对build的配置,同样,对静态图片的引用就要用require来操作。对config文件夹下的index.js 的配置放在了文章最后。
defaultLogo: require('../../../static/img/mc-logo.png'),
defaultEwm: require('../../../static/img/rss-ewm.jpg'),
defaultIconJiantou: require('../../../static/img/icon-right-red.png')
};
},
created() {
let that = this;
that.$vux.loading.show({
text: '载入中...'
});
that.getRoles(); // 获取所有角色
},
mounted() {
this.$nextTick(() => {
//...
});
},
methods: {
jumpToJoinUs(id, name) {
let theName = encodeURI(name);
// 跳转到 加入我们
this.$router.push({
path: 'joinUs',
query: {
roleId: id,
roleName: theName
}
});
},
//获取所有角色
getRoles: function() {
let that = this;
this.$ajax({
method: 'GET',
url: this.host + '/promote/getRoles'
}).then(response => {
let result = response.data;
if (result.code == 0) {
that.$vux.loading.hide();
that.isShowWrap = true;
let data = result.data;
that.menuItems = data;
let userList = [];
for (let i = 0; i < data.length; i++) {
userList.push(data[i].userList);
}
//显示每个角色下初始的用户列表
that.menuContentItems.push(userList);
that.$nextTick(function() {
that.menuListSwiper = that.$refs.menuListSwiper.swiper;
that.menuContentSwiper = that.$refs.menuContentSwiper.swiper;
that.windowHeight = document.documentElement.clientHeight;
that.topMenuHeight = that.$refs.topMenu.offsetHeight;
that.menuContentHeight = that.windowHeight - that.topMenuHeight;
}, 100);
} else {
that.showAlert = true;
that.alertMsg = result.msg;
}
});
},
//获得指定角色下的用户列表
getUserByRole: function(roleId, pageIndex) {
let that = this;
return new Promise((resolve, reject) => {
that
.$ajax({
method: 'GET',
url: this.host + '/promote/getUserByRole',
params: {
roleId: roleId,
pageIndex: ++pageIndex,
pageSize: 10
}
})
.then(response => {
let result = response.data;
if (result.code == 0) {
resolve(result.data);
} else {
that.showAlert = true;
that.alertMsg = result.msg;
}
})
.catch(function(error) {
that.showToastValue = true;
that.alertMsg = error;
});
});
}
}
};
</script>
<style scoped>
.wrap {
}
.top-menu {
padding-bottom: 1rem;
}
.menu-block {
position: relative;
}
.header {
margin: 0;
line-height: 2.625rem;
font-size: 0.875rem;
color: #000;
padding: 0 5%;
}
.menu-block .swiper-container {
margin: 0 8%;
position: static;
}
.menu-block .swiper-slide {
text-align: center;
border-radius: 100%;
color: #fff;
font-size: 0.813rem;
width: 2.813rem !important;
height: 2.813rem;
position: relative;
}
.single-text {
line-height: 2.813rem;
}
.double-text {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
line-height: 1.1rem;
width: 100%;
}
.double-text > span {
display: block;
}
.menu-block .swiper-button-prev,
.menu-block .swiper-button-next {
width: 0;
height: 0;
background-image: none;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
margin-top: 0;
outline: 0;
}
.menu-block .swiper-button-prev {
border-width: 0.563rem 0.563rem 0.563rem 0;
border-style: solid;
border-color: transparent #d8d8d8 transparent transparent;
}
.menu-block .swiper-button-next {
border-width: 0.563rem 0 0.563rem 0.563rem;
border-style: solid;
border-color: transparent transparent transparent #d8d8d8;
}
</style>
<style scoped>
.content {
background-color: #e9e9e9;
}
.content .swiper-container {
width: 100%;
height: 100%;
}
.mc-top {
padding: 0 5% 0.74rem;
position: relative;
}
.mc-paddingtop {
padding-top: 1rem;
}
.icon-main {
display: inline-block;
border: 0.188rem solid #fff;
border-radius: 100%;
background-image: linear-gradient(135deg, green, yellow);
color: #fff;
font-size: 0.813rem;
width: 3.75rem;
height: 3.75rem;
text-align: center;
margin-right: 0.625rem;
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
.icon-main:after {
content: '';
width: 0.625rem;
height: 0.875rem;
background-color: #fff;
display: inline-block;
position: absolute;
bottom: -1rem;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
z-index: -1;
}
.icon-main > span {
display: block;
}
.icon-main-double {
line-height: 1.4rem;
-webkit-transform: translateY(0.5rem);
transform: translateY(0.5rem);
}
.icon-main-single {
line-height: 3.75rem;
}
.icon-sub {
display: inline-block;
border: 1px solid #333;
border-radius: 100%;
background-color: #fff;
color: #d0021b;
font-size: 0.813rem;
width: 1.75rem;
height: 1.75rem;
line-height: 1.1rem;
padding: 0.438rem 0.625rem 0.813rem 0.625rem;
position: absolute;
left: 61.5%;
top: 0.625rem;
z-index: 1;
}
.mc-middle {
margin: 0 9%;
background-color: #fff;
border-radius: 0.25rem;
border: 1px solid #cfcfcf;
}
.the-desc {
color: #000;
font-size: 0.875rem;
border-top-left-radius: 0.25rem;
border-top-right-radius: 0.25rem;
position: relative;
}
.the-desc > img {
border: 0;
display: block;
width: 100%;
}
.the-desc > span {
position: absolute;
top: 1.2rem;
left: 0.75rem;
right: 0.75rem;
bottom: 1rem;
z-index: 2;
word-break: break-all;
display: -webkit-box;
-webkit-line-clamp: 10;
-webkit-box-orient: vertical;
overflow: hidden;
}
.the-intro {
padding: 0.938rem 0;
position: relative;
height: 5rem;
}
.img-user,
.the-intro-block,
.the-erweima {
position: absolute;
top: 50%;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
.img-user {
width: 22%;
height: 61.1%;
left: 3%;
background-position: center center;
background-size: 100%;
border-radius: 100%;
border: 1px solid #efefef;
}
.the-intro-block {
width: 43%;
left: 28%;
}
.txt-name,
.txt-tel {
color: #333;
font-size: 0.875rem;
line-height: 1.4rem;
display: block;
}
.the-erweima {
width: 24%;
left: 73%;
}
.the-erweima > img {
border: 0;
display: block;
width: 100%;
}
.mc-bottom {
width: 0.625rem;
height: 22rem;
background-color: #fff;
display: inline-block;
position: absolute;
bottom: -1rem;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
.md-wrap .swiper-button-prev,
.md-wrap .swiper-button-next {
background-image: url(../../../static/img/icon-jiantou.png) !important;
width: 0.875rem;
background-size: 0.875rem 2.75rem;
}
.md-wrap .swiper-button-next {
-webkit-transform: rotateY(180deg);
transform: rotateY(180deg);
}
@media screen and (max-width: 320px) {
.icon-sub {
left: 63%;
}
.the-desc > span {
padding: 0.9rem 0.75rem;
}
}
@media screen and (width: 375px) and (height: 812px) {
.icon-sub {
left: 61%;
}
}
@media screen and (min-width: 414px) {
.icon-sub {
left: 60.5%;
}
.img-user {
height: 67.5%;
}
}
@media screen and (min-width: 768px) {
.icon-sub {
left: 57%;
}
}
</style>
<style scoped>
.transform-icon {
position: fixed;
bottom: 5px;
left: 0;
right: 0;
z-index: 20;
text-align: center;
}
@keyframes start {
0%,
30% {
opacity: 0;
-webkit-transform: translateY(10px) rotate(-90deg);
transform: translateY(10px) rotate(-90deg);
}
60% {
opacity: 1;
-webkit-transform: translate(0) rotate(-90deg);
transform: translate(0) rotate(-90deg);
}
100% {
opacity: 0;
-webkit-transform: translateY(-8px) rotate(-90deg);
transform: translateY(-8px) rotate(-90deg);
}
}
.transform-icon > img {
width: 1.2rem;
height: 1.6rem;
position: relative;
-webkit-animation: start 1.5s infinite ease-in-out;
animation: start 1.5s infinite ease-in-out;
}
</style>
文章最后顺手放上router文件夹下的两个js文件的内容:
index.js的
import Vue from "vue";
import VueRouter from "vue-router";
import routesList from "./router";
Vue.use(VueRouter);
const router = new VueRouter({
mode: "history",
base:'/connection/',
routes: routesList.routes
});
//Vue单页应用,使用vue-router设置每个页面的title
router.beforeEach((to, from, next) => {
/* 路由发生变化修改页面title */
if (to.meta.title) {
document.title = to.meta.title;
}
next();
});
export default router;
router.js的
const Home = r =>
require.ensure([], () => r(require('../page/home/home.vue')), 'home');
const JoinUs = r =>
require.ensure([], () => r(require('../page/joinUs/joinUs.vue')), 'joinUs');
const routesList = {
routes: [
{
path: '/',
name: 'Home',
component: Home,
meta: {
keepAlive: true,
title: '哒哒哒哒哒' //配合router文件夹里的index.js文件配置每个页面的title
}
},
{
path: '/Home',
name: 'Home',
component: Home,
meta: {
title: '首页' //配合router文件夹里的index.js文件配置每个页面的title
}
},
{
path: '/JoinUs',
name: 'JoinUs',
component: JoinUs,
meta: {
title: '加入我们'
}
}
]
};
export default routesList;
以及 package.json 的内容:
{
"name": "lalala",
"version": "1.0.0",
"description": "A Vue.js project",
"author": "",
"private": true,
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"lint": "eslint --ext .js,.vue src",
"build": "node build/build.js"
},
"dependencies": {
"axios": "^0.18.0",
"cropperjs": "^1.4.0",
"exif-js": "^2.3.0",
"fastclick": "^1.0.6",
"vue": "^2.5.2",
"vue-awesome-swiper": "^3.1.3",
"vue-router": "^3.0.1",
"vux": "^2.9.2",
"vux-uploader": "^0.1.9"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-eslint": "^7.1.1",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-loader": "^6.2.10",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
"babel-runtime": "^6.20.0",
"chalk": "^2.0.1",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.25.0",
"eslint": "^4.15.0",
"eslint-config-standard": "^10.2.1",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.2.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-plugin-vue": "^4.0.0",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.9.0",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^2.30.1",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"node-notifier": "^5.1.2",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.2.0",
"portfinder": "^1.0.13",
"postcss-import": "^11.0.0",
"postcss-loader": "^2.0.8",
"postcss-url": "^7.2.1",
"rimraf": "^2.6.0",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"uglifyjs-webpack-plugin": "^1.1.1",
"url-loader": "^0.5.8",
"vue-loader": "^13.3.0",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.5.2",
"vux-loader": "^1.2.9",
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.1.0",
"yaml-loader": "^0.5.0"
},
"engines": {
"node": ">= 6.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
项目打包:
执行命令: npm run build
执行之后,会自动生成一个跟 src 文件夹同级的 dist 目录,打成压缩包发给运维放服务器对应位置即可。
项目打包之后因为要放到服务器的一个子目录里,根目录下已经有一个项目了,所以要对config文件夹下的index.js 的build对象作一个修改
只需要修改 build 对象的 assetsPublicPath 值, 原配置是:assetsPublicPath: '/'
要修改成这样:assetsPublicPath: '/服务器里放这个项目的文件夹名称/'
'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// Various Dev Server settings
host: '192.168.2.121', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: true,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
cssSourceMap: true
},
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/connection/',
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
}
公众号:青梅煮码
小程序:青梅煮码
个人博客:www.dxel.cn