vue-cli、elementUI的安裝教程請看:
https://www.cnblogs.com/joe235/p/12013818.html
把HelloWorld.vue文件修改為:
<template> <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b"> <el-menu-item index="1">處理中心</el-menu-item> <el-submenu index="2"> <template slot="title">我的工作台</template> <el-menu-item index="2-1">選項1</el-menu-item> <el-menu-item index="2-2">選項2</el-menu-item> <el-menu-item index="2-3">選項3</el-menu-item> </el-submenu> <el-menu-item index="3"> <a href="https://www.ele.me" target="_blank">訂單管理</a> </el-menu-item> </el-menu> </template> <script> export default { data() { return { activeIndex: '1', activeIndex2: '1' }; }, methods: { handleSelect(key, keyPath) { console.log(key, keyPath); } } } </script>
在運行就可以看效果了。
接下來在components文件夾內新建一個NewContact.vue 文件,添加如下代碼:
<template> <el-row> <el-button type="success">1</el-button> </el-row> </template> <script> </script> <style> </style>
打開之前的HelloWorld.vue對內容進行修改(router是官方路由插件,router-link to是router的語法):
<template> <div class="hello"> <!-- <h1>{{ msg }}</h1> --> <!-- 這里router-link to="newcontact"會把路由導航到http://localhost:8080/#/newcontact --> <router-link to="newcontact"><h1>{{ msg }}</h1></router-link> <h2>Essential Links</h2> <el-button>默認按鈕</el-button> <el-button type="primary">主要按鈕</el-button> <el-button type="text">文字按鈕</el-button> </div> </template> <script> export default { name: "hello", data() { return { msg: "Welcome to Your Vue.js App" }; } }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>
打開router/index.js,添加新路由(router是官方路由插件,深入學習請查看文檔)
import Vue from "vue"; import VueRouter from "vue-router"; import Home from "../views/Home.vue"; import NewContact from "@/components/NewContact"; //新添加,之后在下方的component: NewContact才會生效 Vue.use(VueRouter); const routes = [ { path: "/", name: "home", component: Home }, { path: "/about", name: "about", // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () => import(/* webpackChunkName: "about" */ "../views/About.vue") }, { path: "/newcontact", name: "NewContact", component: NewContact } ]; const router = new VueRouter({ routes }); export default router;
保存后打開頁面可以看到如下:
之前的Welcome to Your Vue.js App變成了可點擊的鏈接,點擊后跳轉看到如下頁面:
http://localhost:8080/#/newcontact
至此,已經我們已經把該引入的依賴,和路由的簡單配置,簡單組件的使用給完成了。
接下來我們把精力都放到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>
<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
當我們打開http://localhost:8080/#/newcontact
在輸入框輸入內容時可見,姓名:{{info.name}}
雙花括號里的內容也跟着改變,這就是雙向綁定
<el-option v-for="item in options" :key="item" :value="item">
v-for="item in options"
就是循環options這個數組的內容
options: [ '女','男','保密' ]
如果在里面添加內容,那么下拉菜單的內容會一起變化,一 一對應 :value="item"
會把你選的(’女’,’男’,’保密’)傳給<el-select v-model="info.sex">
v-model="info.sex"
會傳給data中的info.sex
return { info: { name: '', age: null, sex: '' }
接下來在加入新代碼,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>
<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'} ]
打開頁面可以看到效果:
但點擊創建和刪除按鈕並沒有反應,所以我們要添加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傳過來的 } }
到這里已經完成了添加和刪除,為了在我們刷新頁面后,數據依然保存着,我們可以用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.setItem(STORAGE_KEY, JSON.stringify(items)); } };
getItem
和setItem是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 //深度監聽,避免數據的嵌套層數太多,我們要使用深度監聽,這樣即使數據層級過多監聽不到 } } } </script>
tabledata:
由於fetch()得到的是數組,所以直接tabledata: 后寫入就行類似於
tabledata:[{...},{...}]
當我們添加刪除數據時,可以打開chrmoe瀏覽器的F12>Application>Local Storage進行查看。
總的來說就是我們點擊頁面上的創建按鈕,watch監聽到tabledata有變化,就執行savedata(items){Storage.save(items)}進行數據保存,點擊刪除時,tabledata也有變化,同樣會執行保存。
可能之前寫的內容會有不小心寫錯字母的情況,最后把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'; //新添加,把剛寫的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 //深度監聽,避免數據的嵌套層數太多,我們要使用深度監聽,這樣即使數據層級過多監聽不到 } } } </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>
完成后我們要進行打包,輸入命令:
npm run build
會生成一個dist文件夾,之后只需要放在服務器上運行就好了。
而我們想要在本地查看打包后的成果,需要在assetsPublicPath 改變它的路徑, 具體為什么,可以看index.html引入的文件路徑
*常用技巧:
1.如果在config -> index.js 中的 build 代碼中的 productionSourceMap的值設為false ,打包后文件體積可以減少百分之八十
2.如果設置build文件夾下的webpack.prod.conf.js中HtmlWebpackPlugin插件配置參數添加hash: true,即會使打包生成的index.html中的js和css路徑帶有?+隨機字符串,也就是版本控制