基于vue-cli、elementUI的Vue超简单入门小例子

- 这个例子仍是比较简单的,独立完成后,能大概知道vue是干吗的,能够写个todoList的小例子。 
- 开始写例子以前,先对环境的部署作点简单的介绍,其实和Vue官方的差很少。javascript

#如若没有安装过vue-cli,先全局安装一下vue-cli $ cnpm i -g vue-cli #到本身喜欢的目录下建立一个基于 webpack 模板的新项目 $ vue init webpack my-project # # #以后会有以下询问 ? Project name (my-project) #回车 ? Project description #回车,也能够写点项目描述 ? Author #回车,或者输入做者 ? Vue build standalone #回车 ? Install vue-router? (Y/n) #这里是官方推荐的路由,果断yes ? Use ESLint to lint your code? #No ? Set up unit tests #No ? Setup e2e tests with Nightwatch? #No ? Should we run `npm install` for you after the project has been created? (recommended) > Yes, use NPM #能够按上下方向键选择,这里我选第一个NPM,按回车确认 Yes, use Yarn No, I will handle that myself #等待完成
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

完成后可能会有警告,没事,不是ERR就好 
这里写图片描述css

$ cd my-project #进入刚新建的文件夹 $ cnpm install #这里用的是淘宝的NPM镜像,不懂能够自行搜索cnpm $ npm run dev ###I Your application is running here: http://localhost:8080
  • 1
  • 2
  • 3
  • 4

确保端口没被占用,打开localhost:8080 see seehtml


打开咱们的项目可见以下: 
这里写图片描述vue

名称 说明
build 项目构建的一些代码
config 开发环境的配置
node_modules 一些依赖包
src 源码,咱们就在这个文件夹内写代码
static 静态文件
.babelrc ES6编译的一些配置
.editorconfig 代码风格配置文件
.gitignore git上传时忽略的一些文件,好比node_modules这个文
.postcssrc.js 据说是转换CSS样式的
index.html 入口页面
package-lock.json 据说是更详细的package.json
package.json 项目信息,项目名称,开发的依赖的记录等,一个JSON文件

如今打开src\componnets\HelloWorld.vue 把大部分代码删除,剩余以下:java

<template> <h1>{{ msg }}</h1> </template> <script> export default { //ES6语法,用于输出到外部,这样就能够在其余文件内用import输入 name: 'HelloWorld', data () { //因为是组件,data必须是个函数,这是ES6写法,data后面加括号至关于data: function () {} return { //记得return否则接收不到数据 msg: 'Welcome' //上面的 msg 就是这里输出的 } } } </script> <style> h1 { font-weight: normal; } a { color: #42b983; } </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

到这里用了点ES6语法,若是对export和import不了解的,建议看看相关的介绍,暂时不想看也能够照着敲代码。不过建议仍是看看,只需10分钟了解下export和import就好—— 阮一峰ECMAScript 6 入门node

  • 能够看到,以前打开的页面变了样: 
    这里写图片描述

如今咱们来安装一下element-ui(关于element-ui详细状况请自行搜索)

  1. 能够按照官方方法使用npm i element-ui -S命令进行安装
  2. 也能够在package.json中添加,后经过cnpm install进行安装

这里咱们选择2,打开package.json,找到devDependencies并在最后加上”element-ui”: “^2.2.1”python

"devDependencies": { ... ... "element-ui": "^2.2.1"
  • 1
  • 2
  • 3
  • 4

打开命令行中止服务,再经过cnpm install进行安装,安装完成后npm run dev启动 
打开main.js在里面添加三行内容webpack

import ElementUI from 'element-ui' //新添加 import 'element-ui/lib/theme-chalk/index.css' //新添加,避免后期打包样式不一样,要放在import App from './App';以前 import Vue from 'vue' import App from './App' import router from './router' Vue.use(ElementUI) //新添加 Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' }) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

添加了这三行,咱们就可使用element-ui了 
接下来在components文件夹内新建一个NewContact.vue 文件,添加以下代码git

<template> <el-row> <el-button type="success">1</el-button> </el-row> </template> <script> </script> <style> </style> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

打开以前的HelloWorld.vue对内容进行修改(router是官方路由插件,router-link to是router的语法):es6

<template> <!-- 这里router-link to="newcontact"会把路由导航到http://localhost:8080/#/newcontact --> <router-link to="newcontact"><h1>{{ msg }}</h1></router-link> </template>
  • 1
  • 2
  • 3
  • 4

打开router/index.js,添加新路由(router是官方路由插件,深刻学习请查看文档

import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import NewContact from '@/components/NewContact'//新添加,以后在下方的component: NewContact才会生效 Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld }, { path: '/newcontact',//和router-link to相呼应,导航到/newcontact name: 'NewContact', component: NewContact } ] })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

保存后打开页面能够看到以下: 
这里写图片描述 
以前的welcome变成了可点击的连接,点击后跳转看到以下页面 
这里写图片描述 
这里写图片描述 
至此,已经咱们已经把该引入的依赖,和路由的简单配置,简单组件的使用给完成了 
接下来咱们把精力都放到NewContact.vue这个组件,后面的代码几乎都在这个组件


打开NewContact.vue键入以下代码:

<template> <el-row> 姓名:{{info.name}} <el-input v-model="info.name" placeholder="请输入姓名"></el-input> 年龄:{{info.age}} <el-input v-model="info.age" placeholder="请输入年龄"></el-input> 性别:{{info.sex}} <el-select v-model="info.sex" placeholder="请选择"> <el-option v-for="item in options" :key="item" :value="item"></el-option><!-- 这里的key官方推荐在v-for时使用,否则会警告,但不影响使用 --> </el-select> </el-row> </template> <script> export default { name: "NewContact", data(){ return { info: { name: '', age: null, sex: '' }, options: [ '女','男','保密' ] } } } </script> <style> </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

<el-input v-model="info.name"></el-input> 
el-input是element-ui的组件,以el-开头的是element-ui的组件 
v-model这里的v-model是Vue的指令,官方说法是——在表单控件或者组件上建立双向绑定。 
="info.name"就是v-model绑定的数据,在data中return的内容里看到info.name对应的是''info.age对应的是null

return { info: { name: '', age: null, sex: '' }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

当咱们打开http://localhost:8080/#/newcontact

在输入框输入内容时可见,姓名:{{info.name}}双花括号里的内容也跟着改变,这就是双向绑定 
这里写图片描述 
这里写图片描述 
<el-option v-for="item in options" :key="item" :value="item"> 
v-for="item in options"就是循环options这个数组的内容

options: [
        '女','男','保密' ]
  • 1
  • 2
  • 3

若是在里面添加内容,那么下拉菜单的内容会一块儿变化,一 一对应 
:value="item"会把你选的(’女’,’男’,’保密’)传给<el-select v-model="info.sex"> 
v-model="info.sex"会传给data中的info.sex

return { info: { name: '', age: null, sex: '' }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

接下来在加入新代码,NewContact.vue目前的代码以下:

.....
    </el-select> <el-button @click="create">建立</el-button> <template> <el-table :data="tabledata" align="left"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="sex" label="性别"></el-table-column> <el-table-column label="操做"> <template slot-scope="a"> <el-button size="mini" type="danger" @click="del(a.$index)">删除</el-button> </template> </el-table-column> </el-table> </template> </el-row> </template> <script> export default { name: "NewContact", data(){ return { info: { name: '', age: null, sex: '' }, options: [ '女','男','保密' ], tabledata:[ {name: 'Leo', age: 12, sex: 'man'}, {name: 'Lei', age: 22, sex: 'women'}, {name: 'Lii', age: 65, sex: 'men'} ] } } } </script> <style> </style> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

<el-button @click="create" type="success">建立</el-button> 
这里就是建立了一个按钮,@是v-on的缩写,点击后会出发create这个函数

<el-table :data="tabledata"> 
就是表格要绑定的数据

<el-table-column prop="name"> 
在表格绑定数据状况下prop用于数据传递,tabeldata里的name

<template slot-scope="a"> 
是Vue2.5.0后替代以前scope的做用域插槽,”a”是随便的名字,就用用来后去table的状态,能够获取的row, column, $index和store,这里咱们只须要获取index就好了,相关具体内容点这里

@click="del(a.$index) 
@是v-on的缩写,表示点击后调用del函数,a.$index表示slot-scope获取到的index值

tabledata:[//这里的内容是方便咱们看到table的变化,可见页面上的table有了以下的内容 {name: 'Leo', age: 12, sex: 'man'}, {name: 'Lei', age: 22, sex: 'women'}, {name: 'Lii', age: 65, sex: 'men'} ]
  • 1
  • 2
  • 3
  • 4
  • 5

打开页面能够看到 
这里写图片描述
咱们点击建立和删除按钮并无反应,因此咱们要添加methods,在它内部添加两个方法,一个是建立,一个是删除

data(){
...
},
  methods: {//添加在data(){...},的后面 create(){ this.tabledata.push(this.info)//给tabledata添加一个对象(以前咱们建立的info) this.info = {name: '', age: null, sex: ''}//点击建立后,让option还原,而不是停留在所选的项 }, del(index){ this.tabledata.splice(index,1)//删除点击的对象,index是lot-scope="a" a.$index传过来的 } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

到这里已经完成了添加和删除,为了在咱们刷新页面后,数据依然保存着,咱们能够用localStorage本地存储数据 
关于localStorage能够点击这里了解


接下来咱们在src内新建一个store文件夹,和App.vue、components同一个层级,在里面新建一个store.js,内容以下

const STORAGE_KEY = 'tabale-vuejs'//名字随便起 export default {//向往输出,以便组件接收 fetch() {//咱们定义的获取数据的方法 return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]') }, save(items) {//咱们定义的保存数据的方法 window.localStorage.getItem(STORAGE_KEY, JSON.stringify(items)) } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

getItemgetItem是window.localStorage的获取和保存数据的方法 
咱们用JSON.stringify和JSON.parse把数据转成字符串和解析,这样就方便咱们写入tabledata 
接下来咱们添加新代码

<script> import Storage from '../store/store'//新添加,把刚写的localStorage导入 export default { name: "NewContact", data(){ return { info: { name: '', age: null, sex: '' }, options: [ '女','男','保密' ], tabledata: Storage.fetch()//把以前的删除,写入这个获取数据的方法 } }, methods: { create(){ this.tabledata.push(this.info) this.info = {name: '', age: null, sex: ''} }, del(index){ this.tabledata.splice(index,1) } }, watch:{//新添加,watch是vue的监听,一旦监听对象有变化就会执行相应操做 tabledata{//新添加,被监听的对象 handler(items){Storage.save(items)},//新添加,监听对象变化后所作的操做,一个函数,保存数据 deep: true//深度监听,避免数据的嵌套层数太多,咱们要使用深度监听,这样即便数据层级过多监听不到 } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

tabledata:因为fetch()获得的是数组,因此直接tabledata: 后写入就行相似于

tabledata:[{...},{...}]
  • 1

当咱们添加删除数据时,能够打开chrmoe浏览器的F12>Application>Local Storage进行查看 
这里写图片描述

总的来讲就是咱们点击页面上的建立按钮,watch监听到tabledata有变化,就执行savedata(items){Storage.save(items)}进行数据保存,点击删除时,tabledata也有变化,一样会执行保存 
可能以前写的内容会有不当心写错字母的状况,最后把NewContact.vue稍稍修改样式后,把完整的内容拷贝到下方: 
NewContact.vue

<template> <el-row> <el-col :xs="24" :sm="18" :md="14" :lg="10" id="main"> <label>姓名:</label> <el-input v-model="info.name" placeholder="请输入姓名"></el-input> <label>年龄:</label> <el-input v-model="info.age" placeholder="请输入年龄"></el-input> <label>性别:</label> <el-select v-model="info.sex" placeholder="请选择"> <el-option v-for="item in options" :key="item" :value="item"></el-option><!-- 这里的key官方推荐在v-for时使用,否则会警告,但不影响使用 --> </el-select> <el-button class="btn-auto" @click="create" type="success">建立</el-button> <template> <el-table :data="tabledata" align="left"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="sex" label="性别"></el-table-column> <el-table-column label="操做"> <template slot-scope="a"> <el-button size="mini" type="danger" @click="del(a.$index)">删除</el-button> </template> </el-table-column> </el-table> </template> </el-col> </el-row> </template> <script> import Storage from '../store/store' export default { name: "NewContact", data(){ return { info: { name: '', age: null, sex: '' }, options: [ '女','男','保密' ], tabledata: Storage.fetch() } }, methods: { create(){ this.tabledata.push(this.info) this.info = {name: '', age: null, sex: ''} }, del(index){ this.tabledata.splice(index,1) } }, watch:{ tabledata:{ handler(items){Storage.save(items)}, deep: true } } } </script> <style> #main{ float: none; margin: 0 auto; } .el-input{ padding-bottom: 5px; } .el-select { display: block; } .btn-auto{ width: 100%; margin-top: 12px; } </style> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

这里是localStorage:

const STORAGE_KEY = 'tabale-vuejs' export default { fetch() { return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]') }, save(items) { window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items)) } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

完成后咱们要进行打包,方便直接在浏览器中运行 
打开/config/index.js

build: {
    // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: './',//加了个. 避免生产路径的错误 /** * Source Maps */ productionSourceMap: false, //改成false
相关文章
相关标签/搜索