三十九、Vue项目上手 | 用户管理系统 实现添加用户功能(中篇)
@Author:Runsen
@Date:2020/7/8
人生最重要的不是所站的位置,而是內(nèi)心所朝的方向。只要我在每篇博文中寫(xiě)得自己體會(huì),修煉身心;在每天的不斷重復(fù)學(xué)習(xí)中,耐住寂寞,練就真功,不畏艱難,奮勇前行,不忘初心,砥礪前行,人生定會(huì)有所收獲,不留遺憾 (作者:Runsen )
作者介紹:Runsen目前大三下學(xué)期,專業(yè)化學(xué)工程與工藝,大學(xué)沉迷日語(yǔ),Python, Java和一系列數(shù)據(jù)分析軟件。導(dǎo)致翹課嚴(yán)重,專業(yè)排名中下。.在大學(xué)60%的時(shí)間,都在CSDN。決定今天比昨天要更加努力。我的征途是星辰大海!
今天是高考第二天,當(dāng)年我就是一個(gè)辣雞,現(xiàn)在還是一個(gè)辣雞,祝高考的個(gè)個(gè)清華北大。
我看了下高考的導(dǎo)數(shù)最后一題,挺簡(jiǎn)單的。
(1)第一問(wèn)比較簡(jiǎn)單,求個(gè)導(dǎo)數(shù)就行
(2)首先變量分離
下面直接考慮x>0的情況。
標(biāo)準(zhǔn)答案
廢話不多說(shuō),繼續(xù)學(xué)前端。
連接接口
上次完成到這里,搭建了模板
在之前,利用json-server,搭建了3000端口的API,這里需要同時(shí)的運(yùn)行起來(lái)。
可以訪問(wèn)http://localhost:3000/users,具體結(jié)果如下所示。
現(xiàn)在的目標(biāo)很簡(jiǎn)單了,就是通過(guò)Customers.vue中的customers一個(gè)空列表變成接口里面的東西。
我們可以在組件中定義一個(gè)方法來(lái)連接數(shù)據(jù),這里需要涉及vue-resource插件
vue-resource是Vue.js的一款插件,它可以通過(guò)XMLHttpRequest或JSONP發(fā)起請(qǐng)求并處理響應(yīng)。
C:\Users\YIUYE\Desktop\project\customers>npm install vue-resource --save安裝成功后,在main.js導(dǎo)入
import VueResource from 'vue-resource' Vue.use(VueResource)導(dǎo)入完成后,那么this.$http.get就可以用了?,F(xiàn)在直接打印http://localhost:3000/users的response,然后定義一個(gè)created執(zhí)行函數(shù)。
下圖就是效果,這樣就可以看見(jiàn)http://localhost:3000/users的接口數(shù)據(jù)。
拿到數(shù)據(jù)渲染
下面就是 拿到數(shù)據(jù)渲染,使用v-for遍歷,this.customers = response.body,這樣customers 就有值了。
<template><div class="customers container"><h1 class="page-header">用戶管理系統(tǒng)</h1><table class="table table-striped"><thead><tr><th>姓名</th><th>電話</th><th>郵箱</th><th></th></tr></thead><tbody v-for="customer in customers"><tr><td>{{customer.name}}</td><td>{{customer.phone}}</td><td>{{customer.email}}</td><td></td></tr></tbody></table></div> </template> <script> export default {name: 'customers',data() {return {customers :[],}},methods: {// 連接數(shù)據(jù)fetchCustomers(){this.$http.get("http://localhost:3000/users").then(function(response){console.log(response)this.customers = response.body})}},created() {this.fetchCustomers();},} </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>具體效果如下所示。
添加用戶
下面實(shí)現(xiàn)一個(gè)添加用戶的功能,其實(shí)一個(gè)功能就是一個(gè)組件,這是我發(fā)現(xiàn)的。于是就寫(xiě)一個(gè)Add.vue的組件。
<template><div class="add container">添加用戶</div> </template><script> export default {name: 'add',data() {return {}} } </script><!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>下面就是在main.js設(shè)置添加的路由。
下面是main.js全部代碼
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import VueRouter from 'vue-router' import VueResource from 'vue-resource' import App from './App' import Customers from './components/Customers.vue' import About from './components/About.vue' import Add from './components/Add.vue' Vue.config.productionTip = false Vue.use(VueRouter) Vue.use(VueResource) // 設(shè)置路由 const router = new VueRouter({mode:"history",base: __dirname,routes:[{path:'/',component:Customers},{path:'/about',component:About},{path:'/add',component:Add}] }) /* eslint-disable no-new */ new Vue({router,template: `<div id="app"><nav class="navbar navbar-default"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">用戶管理系統(tǒng)</a></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav"><li><router-link to="/">主頁(yè)</router-link></li><li><router-link to="/about">關(guān)于我們</router-link></li></ul><ul class="nav navbar-nav navbar-right"><li><router-link to="/add">添加用戶</router-link></li></ul></div></div></nav><router-view></router-view></div>` }).$mount("#app")下圖就是添加用戶的頁(yè)面。
下面就是實(shí)現(xiàn)添加用戶的組件,具體代碼如下。這里就是添加一個(gè)用戶傳3000接口。
<template><div class="add container"><h1 class="pag-header">添加用戶</h1><form v-on:submit="addCustomer"><div class="well"><h4>用戶信息</h4><div class="form-group"><label>姓名</label><input type="text"class="form-control"placeholder="name"v-model="customer.name"></div><div class="form-group"><label>電話</label><input type="text"class="form-control"placeholder="phone"v-model="customer.phone"></div><div class="form-group"><label>郵箱</label><input type="text"class="form-control"placeholder="email"v-model="customer.email"></div><div class="form-group"><label>學(xué)歷</label><input type="text"class="form-control"placeholder="education"v-model="customer.education"></div><div class="form-group"><label>畢業(yè)學(xué)校</label><input type="text"class="form-control"placeholder="graduationschool"v-model="customer.graduationschool"></div><div class="form-group"><label>職業(yè)</label><input type="text"class="form-control"placeholder="profession"v-model="customer.profession"></div><div class="form-group"><label>個(gè)人簡(jiǎn)介</label><!-- <input type="text" class="form-control" placeholder="profile" v-model="customer.profile"> --><textarea class="form-control"rows="10"v-model="customer.profile"></textarea></div><button type="submit"class="btn btn-primary">添加</button></div></form></div> </template><script> export default {name: 'add',data() {return {customer:{}}},methods:{addCustomer(e){// console.log(123);if (!this.customer.name || !this.customer.phone || !this.customer.email) {// console.log("請(qǐng)?zhí)砑訉?duì)應(yīng)的信息!");this.alert = "請(qǐng)?zhí)砑訉?duì)應(yīng)的信息!";}else{let newCustomer = {name:this.customer.name,phone:this.customer.phone,email:this.customer.email,education:this.customer.education,graduationschool:this.customer.graduationschool,profession:this.customer.profession,profile:this.customer.profile}this.$http.post("http://localhost:3000/users",newCustomer).then(function(response){// console.log(response);this.$router.push({path:"/",query:{alert:"用戶信息添加成功!"}});})e.preventDefault();}e.preventDefault();}}, } </script><!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>下面是測(cè)試結(jié)果
總結(jié)
以上是生活随笔為你收集整理的三十九、Vue项目上手 | 用户管理系统 实现添加用户功能(中篇)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 被俄罗斯占领的乌东四州和克里米亚共有多少
- 下一篇: 办公室门对厕所墙关公财神怎么摆?