Vue使用element-ui/plus组件布局容器container失效解决方法

这个错我遇到了好几次,每一次都花了很长时间解决,还是决定写个笔记记录一下。

在使用elementu官网的布局容器时,他是这样的。

<el-container>
  <el-header>Header</el-header>
  <el-main>Main</el-main>
</el-container>

<el-container>
  <el-header>Header</el-header>
  <el-main>Main</el-main>
  <el-footer>Footer</el-footer>
</el-container>

<el-container>
  <el-aside width="200px">Aside</el-aside>
  <el-main>Main</el-main>
</el-container>

<el-container>
  <el-header>Header</el-header>
  <el-container>
    <el-aside width="200px">Aside</el-aside>
    <el-main>Main</el-main>
  </el-container>
</el-container>

<el-container>
  <el-header>Header</el-header>
  <el-container>
    <el-aside width="200px">Aside</el-aside>
    <el-container>
      <el-main>Main</el-main>
      <el-footer>Footer</el-footer>
    </el-container>
  </el-container>
</el-container>

<el-container>
  <el-aside width="200px">Aside</el-aside>
  <el-container>
    <el-header>Header</el-header>
    <el-main>Main</el-main>
  </el-container>
</el-container>

<el-container>
  <el-aside width="200px">Aside</el-aside>
  <el-container>
    <el-header>Header</el-header>
    <el-main>Main</el-main>
    <el-footer>Footer</el-footer>
  </el-container>
</el-container>

<style>
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
  
  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }
  
  body > .el-container {
    margin-bottom: 40px;
  }
  
  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }
  
  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
</style>

我在确定elementui引入正确的情况下是这样写的。

<template>
  <el-container>
    <SideNav></SideNav>
    <el-container>
      <el-main><Main></Main></el-main>
      <el-footer><Footer></Footer></el-footer>
    </el-container>
  </el-container>
</template>

<script>
import SideNav from "@/components/SideNav";
import HomeHeader from "@/pages/Home/HomeHeader";
import "element-ui/lib/theme-chalk/index.css";
import Header from "@/components/Header";
export default {
  name: "Home",
  components: { SideNav, HomeHeader, Header },
};
</script>
<style  scoped>
.baseContainer {
  height: 100%;
  width: 100%;
  position: relative;
  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;
}

.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

注意,我在SideNav,Header,Main,Footer组件套了el-aside,el-header...然后暴露出来在Home中引用。

当然错误,布局一片混乱。想了一下午终于懂了。

原来,elementui不支持这种写法,在Home里面应该完全遵循官网的写法,el-aside,el-header,el-mian,el-footer等必须写在container的同一个组件,不能写在子组件里面然后引入,不然elementui不会识别为布局容器。

正确写法

<template>
  <el-container>
    <el-aside>
      <SideNav></SideNav>
    </el-aside>
    <el-container>
      <el-main><Main></Main></el-main>
      <el-footer><Footer></Footer></el-footer>
    </el-container>
  </el-container>
</template>

<script>
import SideNav from "@/components/SideNav";
import HomeHeader from "@/pages/Home/HomeHeader";
import "element-ui/lib/theme-chalk/index.css";
import Header from "@/components/Header";
export default {
  name: "Home",
  components: { SideNav, HomeHeader, Header },
};
</script>
<style  scoped>
.baseContainer {
  height: 100%;
  width: 100%;
  position: relative;
  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;
}

.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

言语稚嫩,希望对你有帮助!

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知在Vue2.0中引用element-ui组件库需要引入样式文件,并且如果出现报错可以在webpack.config.js中配置file_loader。如果仍然出现问题,可以在项目根目录下的build->webpack.dev.conf.js中将usePostCSS改为false即可。 关于Vueelement-ui/lib/theme-chalk/index.css和webpack的介绍和演示如下: Vue是一套用于构建用户界面的渐进式框架,它的核心库只关注视图层,易于上手,同时也便于与第三方库或既有项目整合。Vue也提供了一套完整的工具链,包括构建工具、脚手架、路由、状态管理等,可以帮助开发者更好地构建大型单页应用。 element-ui是一套基于Vue2.0的组件库,它提供了丰富的UI组件,包括表单、弹窗、导航、布局等,可以帮助开发者快速构建美观、易用的Web应用。 webpack是一个现代化的JavaScript应用程序静态模块打包器,它可以将多个模块打包成一个文件,以便在浏览器中使用。webpack支持各种各样的模块类型,包括CommonJS、AMD、ES6等,还可以通过loader和plugin扩展其功能。 演示如下: 1.在Vue项目中引入element-ui组件库 ```javascript // 引入element-ui样式文件 import 'element-ui/lib/theme-chalk/index.css'; // 引入element-ui组件库 import ElementUI from 'element-ui'; // 使用element-ui组件Vue.use(ElementUI); ``` 2.在webpack.config.js中配置file_loader ```javascript module.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'file-loader', options: { name: utils.assetsPath('img/[name].[hash:7].[ext]') } } ] } } ``` 3.在build->webpack.dev.conf.js中将usePostCSS改为false ```javascript module.exports = merge(baseWebpackConfig, { module: { rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: false }) } }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值