来自后端对 vue+elementUI 的快速学习

Vue

作为一个后端,学习起来还是有点吃力,项目有用,还得硬着头皮学

Vue 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。

官网学习了一些常用的用法,然后想当然的就去找项目跟着学了,第一个看的项目是vue-element-admin,发现无字天书一般看不懂

后了解到,这是由vue-cli脚手架生成的,据同事分析,vue-cli是类似于一个vue运行环境的东西,故只能先学一下用vue-cli搭建下项目学习

npm install --global vue-cli

vue init webpack vue-demo

//安装所需要的模块
npm i axios vuex vue-router –save 

创建后的目录地址

è¿éåå¾çæè¿°

官网和网上普遍项目的语法差很多,而我项目要使用的,也是vue-cli脚手架这种,所以为了赶进度,我决定用我已有的官网vue知识,强行去看懂网上的项目,并写出自己的demo

官网上有这么一个demo

官方示例代码:

<script src="https://unpkg.com/vue"></script>

<div id="dynamic-component-demo">
  <button
    v-for="tab in tabs"
    v-bind:key="tab"
    v-bind:class="['tab-button', { active: currentTab === tab }]"
    v-on:click="currentTab = tab"
  >{{ tab }}</button>

  <keep-alive>
    <component
      v-bind:is="currentTabComponent"
      class="tab"
    ></component>
  </keep-alive>
</div>

 

Vue.component('tab-posts', { 
  data: function () {
  	return {
      posts: [
        { 
        	id: 1, 
          title: 'Cat Ipsum',
          content: '<p>Dont wait for the storm to pass。。。</p>'
        },
        { 
        	id: 2, 
          title: 'Hipster Ipsum',
          content: '<p>Bushwick blue bottle scenester helvetica ugh。。。</p>'
        },
        { 
        	id: 3, 
          title: 'Cupcake Ipsum',
          content: '<p>Icing dessert soufflé lollipop chocolate 。。。</p>'
        }
      ],
      selectedPost: null
    }
  },
	template: `
  	<div class="posts-tab">
      <ul class="posts-sidebar">
        <li
          v-for="post in posts"
          v-bind:key="post.id"
          v-bind:class="{ selected: post === selectedPost }"
					v-on:click="selectedPost = post"
        >
          {{ post.title }}
        </li>
      </ul>
      <div class="selected-post-container">
      	<div 
        	v-if="selectedPost"
          class="selected-post"
        >
          <h3>{{ selectedPost.title }}</h3>
          <div v-html="selectedPost.content"></div>
        </div>
        <strong v-else>
          Click on a blog title to the left to view it.
        </strong>
      </div>
    </div>
  `
})

Vue.component('tab-archive', { 
	template: '<div>Archive component</div>' 
})

new Vue({
  el: '#dynamic-component-demo',
  data: {
    currentTab: 'Posts',
    tabs: ['Posts', 'Archive']
  },
  computed: {
    currentTabComponent: function () {
      return 'tab-' + this.currentTab.toLowerCase()
    }
  }
})

css代码省略

用vue-cli写出来:

首先根据组件的思想,拆分出两个组件,和一个demo页面

demo.vue


<template>
  <div>
  <el-button type="error"
    v-for="tab in tabs"
    v-bind:key="tab"
    v-bind:class="['tab-button', { active: currentTab === tab }]"
    v-on:click="currentTab = tab"
  >{{ tab }}</el-button>

  <keep-alive>
    <component
      v-bind:is="currentTabComponent"
      class="tab"
    ></component>
  </keep-alive>
</div>
</template>

<script>
import tabArchive from '../components/tab-archive'
import tabPosts from '../components/tab-posts'

export default {
  components: { tabArchive, tabPosts },
  name: 'demo',
  data () {
    return {
      tabs: ['Posts', 'Archive'],
      currentTab: 'Posts'
    }
  },
  computed: {
    currentTabComponent: function () {
      return 'tab-' + this.currentTab.toLowerCase()
    }
  }
}
</script>

 tab-posts.vue 

<template>
    <div class="posts-tab">
      <ul class="posts-sidebar">
        <li
          v-for="post in posts"
          v-bind:key="post.id"
          v-bind:class="{ selected: post === selectedPost }"
					v-on:click="selectedPost = post"
        >
          {{ post.title }}
        </li>
      </ul>
      <div class="selected-post-container">
          <div v-if="selectedPost" class="selected-post">
          <h3>{{ selectedPost.title }}</h3>
          <div v-html="selectedPost.content"></div>
        </div>
        <strong v-else>
          Click on a blog title to the left to view it.
        </strong>
      </div>
    </div>
</template>

<script >
export default ({
  name: 'tab-posts',
  data () {
    return {
      posts: [
        {
          id: 1,
          title: 'Cat Ipsum',
          content: '<p>Dont wait for the storm to pass, dance in the rain kick up litter decide to want nothing to do with my owner today demand to be let outside at once, and expect owner to wait for me as i think about it cat cat moo moo lick ears lick paws so make meme, make cute face but lick the other cats. Kitty poochy chase imaginary bugs, but stand in front of the computer screen. Sweet beast cat dog hate mouse eat string barf pillow no baths hate everything stare at guinea pigs. My left donut is missing, as is my right loved it, hated it, loved it, hated it scoot butt on the rug cat not kitten around</p>'
        },
        {
          id: 2,
          title: 'Hipster Ipsum',
          content: '<p>Bushwick blue bottle scenester helvetica ugh, meh four loko. Put a bird on it lumbersexual franzen shabby chic, street art knausgaard trust fund shaman scenester live-edge mixtape taxidermy viral yuccie succulents. Keytar poke bicycle rights, crucifix street art neutra air plant PBR&B hoodie plaid venmo. Tilde swag art party fanny pack vinyl letterpress venmo jean shorts offal mumblecore. Vice blog gentrify mlkshk tattooed occupy snackwave, hoodie craft beer next level migas 8-bit chartreuse. Trust fund food truck drinking vinegar gochujang.</p>'
        },
        {
          id: 3,
          title: 'Cupcake Ipsum',
          content: '<p>Icing dessert soufflé lollipop chocolate bar sweet tart cake chupa chups. Soufflé marzipan jelly beans croissant toffee marzipan cupcake icing fruitcake. Muffin cake pudding soufflé wafer jelly bear claw sesame snaps marshmallow. Marzipan soufflé croissant lemon drops gingerbread sugar plum lemon drops apple pie gummies. Sweet roll donut oat cake toffee cake. Liquorice candy macaroon toffee cookie marzipan.</p>'
        }
      ],
      selectedPost: null
    }
  }
})
</script>

tab-archive.vue

<template>
    <div>Archive component</div>
</template>

<script >
export default ({
  name: 'tab-archive'
})
</script>

vue初识

vue给我的感觉,就是万物皆为组件合成,所以要用好vue,就要写好组件。

ElementUI

elementUI作为与vue兼容组好的UI框架,我毫不犹豫用它写起了自己的demo

引入elementui

在 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)
});

实际上没什么好说的,照着官网开发就是了 http://element-cn.eleme.io

开发通用表格组件

table1.vue

<template>
<div>
  <el-table stripe=true
    ref="multipleTable"
    :data="tableData1"
    tooltip-effect="dark"
    style="width: 100%"
    @selection-change="handleSelectionChange">
    <el-table-column v-for="(item,key) in tableHeader"
                     :key="key"
                     :prop="item.value"
                     :label="item.name"
                     sortable
                     :width="item.width"   
                     :type="item.type"
      >
    </el-table-column>
    
  </el-table>
  <el-pagination
      name="fenye"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=total>
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData1: [{
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }],
        multipleSelection: [],
        currentPage:1,
        pageSize:10,
        sort:1,
        total:2
      }
    },
    props:[
        'tableHeader'
    ],
    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    }
  }
</script>

 

<template>
  <div>
      <table1 :tableHeader="tableHeader" ></table1>
  </div>
</template>

<script>
import Table1 from '../components/table1';
  export default {
    components: {'table1': Table1},
    data() {
      return {
        tableHeader: [
          {
            type:'selection'
          },
          {
          name: '日期',
          value: 'date',
          width: 200
          },
          {
          name: '姓名',
          value: 'name',
          width: 200
          },
          {
          name: '地址',
          value: 'address',
          width: 400
          }]
      }
    },

    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    }
  }
</script>

这里是简单由父组件向子组件传递一个表头的数据(可以 根据需求进行改造)

代码:github地址

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值