第 三 方 组 件 e l e m e n t - u i[Vue]

一、组件之间的传值

  • 组件可以由内部的Data提供数据,也可以由父组件通过prop的方式传值。

  • 兄弟组件之间可以通过Vuex等统一数据源提供数据共享

第一种

Movie.vue
复制代码
 <template>
     <div>
         <h1>我才不要和你做朋友</h1>
     </div>
 </template>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie></Movie>
   </div>
 </template>
 ​
 <script>
 import Movie from './components/Movie.vue'
 ​
 export default {
   name: 'App',
   components: {
     Movie
   }
 }
 </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>
 ​

第二种

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     data:function(){
         return{
             title:"我才不要跟你做朋友"
         }
     }
 }
 </script>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie></Movie>
   </div>
 </template>
 ​
 <script>
 import Movie from './components/Movie.vue'
 Movie.name //
 export default {
   name: 'App',
   components: {
     Movie
   }
 }
 </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>
 ​

第三种

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     props:["title"],
     data:function(){
         return{
             
         }
     }
 }
 </script>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie title="我才不要跟你做朋友"></Movie>
   </div>
 </template>
 ​
 <script>
 import Movie from './components/Movie.vue'
 Movie.name
 export default {
   name: 'App',
   components: {
     Movie
   }
 }
 </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>
 ​
显示效果(前三种一样):

第四种:

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
         <span>{{rating}}</span>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     props:["title","rating"],
     data:function(){
         return{
             
         }
     }
 }
 </script>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie title="我才不要跟你做朋友" rating="6.8"></Movie>
   </div>
 </template>
 ​
 <script>
 import Movie from './components/Movie.vue'
 Movie.name
 export default {
   name: 'App',
   components: {
     Movie
   }
 }
 </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>
 ​
显示效果:

第五种(关于组件的传值):

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
         <span>{{rating}}</span>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     props:["title","rating"],
     data:function(){
         return{
             
         }
     }
 }
 </script>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie  v-for="movie in movies" :key="movie.id" :title="movie.title" :rating="movie.rating"></Movie>
   </div>
 </template>
 ​
 ​
 <script>
 import Movie from './components/Movie.vue'
 Movie.name
 export default {
   name: 'App',
   data:function(){
     return{
       movies:[
         {id:1,title:"我才不要跟你做朋友1",rating:8.8},
         {id:2,title:"我才不要跟你做朋友2",rating:8.9},
         {id:3,title:"我才不要跟你做朋友3",rating:8.7},
       ]
     }
   },
   components: {
     Movie
   }
 }
 </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>
 ​
显示效果:

第六种(加按钮,弹窗):

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
         <span>{{rating}}</span>
         <button @click="fun">点击收藏</button>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     props:["title","rating"],
     data:function(){
         return{
             
         }
     },
     methods:{
         fun(){
             alert("收藏成功")
         }
     }  
 }
 </script>
 ​
App.vue
复制代码
 <template>
   <div id="app">
     <Movie  v-for="movie in movies" :key="movie.id" :title="movie.title" :rating="movie.rating"></Movie>
   </div>
 </template>
 ​
 ​
 <script>
 import Movie from './components/Movie.vue'
 Movie.name
 export default {
   name: 'App',
   data:function(){
     return{
       movies:[
         {id:1,title:"我才不要跟你做朋友1",rating:8.8},
         {id:2,title:"我才不要跟你做朋友2",rating:8.9},
         {id:3,title:"我才不要跟你做朋友3",rating:8.7},
       ]
     }
   },
   components: {
     Movie
   }
 }
 </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>
显示效果:

第七种(兄弟组件)

Movie.vue
复制代码
 <template>
     <div>
         <h1>{{title}}</h1>
         <span>{{rating}}</span>
         <button @click="fun">点击收藏</button>
     </div>
 </template>
 ​
 <script>
 export default {
     name: "Movie",
     props:["title","rating"],
     data:function(){
         return{
             
         }
     },
     methods:{
         fun(){
             alert("收藏成功")
         }
     }  
 }
 </script>
 ​
APP.vue
复制代码
 <template>
   <div id="app">
     <Movie  v-for="movie in movies" :key="movie.id" :title="movie.title" :rating="movie.rating"></Movie>
     <Hello></Hello>
   </div>
 </template>
 ​
 ​
 <script>
 import Movie from './components/Movie.vue'
 import Hello from './components/Hello.vue'
 Movie.name
 export default {
   name: 'App',
   data:function(){
     return{
       movies:[
         {id:1,title:"我才不要跟你做朋友1",rating:8.8},
         {id:2,title:"我才不要跟你做朋友2",rating:8.9},
         {id:3,title:"我才不要跟你做朋友3",rating:8.7},
       ]
     }
   },
   components: {
     Movie,
     Hello
   }
 }
 </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>
 ​
Hello.vue
复制代码
 <template>
     <h3>hello</h3>
 </template>
显示效果:

二、element-ui介绍

Element是国内饿了么公司提供的一套开源前端框架,简洁优雅,提供了Vue、React、Angular等多个版本。

下载好后,所以安装好的东西都会在node_mdules目录下

  • 引入 Element(在main.js):
复制代码
 import Vue from 'vue';
 import ElementUI from 'element-ui';
 import 'element-ui/lib/theme-chalk/index.css';
 import App from './App.vue';
 ​
 Vue.use(ElementUI);
 ​
 new Vue({
   el: '#app',
   render: h => h(App)
 });

三、组件的使用

main.js

复制代码
 import Vue from 'vue'
 import App from './App.vue'
 import ElementUI from 'element-ui'; 
 import 'element-ui/lib/theme-chalk/index.css';
 ​
 Vue.config.productionTip = false
 Vue.use(ElementUI);
 new Vue({
   render: h => h(App),
 }).$mount('#app')
 ​

Hello.vue

复制代码
 <template>
     <div>
     <el-table
     :data="tableData"
     style="width: 100%"
     :row-class-name="tableRowClassName">
     <el-table-column
       prop="id"
       label="编号"
       width="180">
     </el-table-column>
     <el-table-column
       prop="username"
       label="姓名"
       width="180">
     </el-table-column>
     <el-table-column
       prop="birthday"
       label="生日">
     </el-table-column>
   </el-table>
    <el-date-picker
       v-model="value1"
       type="date"
       placeholder="选择日期">
     </el-date-picker>
     <i class="fa fa-camera-retro"></i>
     </div>
     
 </template>
 ​
 <script>
 ​
 export default {
      methods: {
       tableRowClassName({row, rowIndex}) {
         if (rowIndex === 1) {
           return 'warning-row';
         } else if (rowIndex === 3) {
           return 'success-row';
         }
         return '';
       }
     },
     created:function(){
       this.$http.get("/user/findAll").then((response)=>{
         this.tableData = response.data
       })
     },
     data() {
       return {
         tableData: []
       }
     }
 }
 </script>
 ​
 <style>
   .el-table .warning-row {
     background: oldlace;
   }
 ​
   .el-table .success-row {
     background: #f0f9eb;
   }
 </style>
显示结果:

修改Hello.vue(隔行颜色不同)

复制代码
 <template>
     <el-table
     :data="tableData"
     style="width: 100%"
     :row-class-name="tableRowClassName">
     <el-table-column
       prop="date"
       label="日期"
       width="180">
     </el-table-column>
     <el-table-column
       prop="name"
       label="姓名"
       width="180">
     </el-table-column>
     <el-table-column
       prop="address"
       label="地址">
     </el-table-column>
   </el-table>
 </template>
 <script>
 export default {
     methods: {
       tableRowClassName({row, rowIndex}) {
         if (rowIndex === 1) {
           return 'warning-row';
         } else if (rowIndex === 3) {
           return 'success-row';
         }
         return '';
       }
     },
     data() {
       return {
         tableData: [{
           date: '2016-05-02',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄',
         }, {
           date: '2016-05-04',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
         }, {
           date: '2016-05-01',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄',
         }, {
           date: '2016-05-03',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
         }]
       }
     }
   
 }
 </script>
 <style>
   .el-table .warning-row {
     background: oldlace;
   }
 ​
   .el-table .success-row {
     background: #f0f9eb;
   }
 </style>
 ​
测试结果:

修改Hello.vue(添加日期选择器)

复制代码
 <template>
     <div><el-table
     :data="tableData"
     style="width: 100%"
     :row-class-name="tableRowClassName">
     <el-table-column
       prop="date"
       label="日期"
       width="180">
     </el-table-column>
     <el-table-column
       prop="name"
       label="姓名"
       width="180">
     </el-table-column>
     <el-table-column
       prop="address"
       label="地址">
     </el-table-column>
   </el-table>
   <el-date-picker
       v-model="value1"
       type="date"
       placeholder="选择日期">
     </el-date-picker>
     </div>
 </template>
显示结果

四、图标的使用

第三方图标库

由于Element UI提供的字体图符较少,一般会采用其他图表库,如著名的FontAwesome

Font Awesome提供了675个可缩放的矢量图标,可以使用CSS所提供的所有特性对它们进行更改,包括大小、颜色、阴影或者其他任何支持的效果。

文档地址:http://fontawesome.dashgame.com/

  • 安装:npm install font-awesome
  • 使用:import 'font-awesome/css/font-awesome.min.css

测试结果(添加图标):

相关推荐
陈随易3 小时前
Postman 被曝自动往 Claude Code 和 Codex 装插件
前端·后端·程序员
Dream-Y.ocean5 小时前
鸿蒙PC平台宅匠 3D 室内设计适配实战:基于 Electron 壳方案的 Three.js 双渲染引擎与参数化户型编辑器开发
javascript·3d·harmonyos
Csvn6 小时前
性能监控
前端
Highcharts.js6 小时前
Highcharts Gantt使用符号的自定义数据标签开发甘特图示例
javascript·甘特图·highcharts·图表开发·实战代码·响应式图表
Csvn6 小时前
构建工具
前端
FRing037 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
Java小卷8 小时前
JuanForm vs FormCreate!Vue3 表单引擎联动配置 PK!
vue.js·低代码
kyriewen9 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒9 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫10 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库