Web前端 教程 ·

vue+elementui项目打包后样式变化问题

刚解决了Vue设置路由为History模式,打包后访问404问题或index.html空白问题,打开项目页面又发现了样式出了问题,样式与开发版本有些不同,有些样式没有生效。通过网上查资料找到了问题所在以及解决办法:
main.js中的引入顺序决定了打包后css的顺序,组件内的样式没有生效可能是被第三方组件样式覆盖了,所以把第三方组件放在前面引入,router和app放在后面引入,就可以实现组件样式在第三方样式之后渲染。代码如下:

// 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 VueCookie from 'vue-cookie'
import ajax from '@/utils/ajax'
import global from '@/utils/global'
import 'font-awesome/css/font-awesome.css'
import ueditor from '@/components/ueditor'
import cloneDeep from 'lodash/cloneDeep'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 后引入router
import router from './router'
import store from '@/store'
import App from './App'

Vue.config.productionTip = false

Vue.use(ElementUI)
Vue.use(VueCookie)
Vue.use(ueditor)

Vue.component('ueditor', ueditor)

Vue.prototype.$http = ajax
Vue.prototype.$global = global
require('@/mock')

// 保存整站vuex本地储存初始状态
window.SITE_CONFIG['storeState'] = cloneDeep(store.state)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

这样修改之后样式问题就解决了,打包后的版本与开发版本显示一样。

参与评论