* Install Axios with NPM
Switch to the project's root directory
NPM Install--save Axios Vue-axios
* Introduce Axios in the Vue portal file./src/main.js, add 2 lines of code
Axiosimport Axios from ' Axios ' vue.prototype. $http = Axios
* the./src/main.js entry file is as follows:
The Vue build version to load with the ' import ' command//(runtime-only or standalone) have been set in Webpack.base.con F with a alias.import vue from ' Vue ' import app./app ' import router from './router '//Axiosimport Axios from ' Axios ' Vue.prototype. $http = Axios Vue.config.productionTip = False/* eslint-disable no-new */new Vue ({ el: ' #app ', rou ter, components : {App}, Template: ' <App/> '})
* Generate data files./assets/data/people.json
{"People": [{"id": 1, "name": "Jack", "age": +, "gender": "Male"},{"id": 2, "name": "Bill", "Age": "Gender": "Male"},{" ID ": 3," "Name": "Tracy", "Age": $, "gender": "Female"},{"id": 4, "name": "Chris", "Age": $, "gender": "Male"}]}
Why can't I access it directly in the component: /assets/data/people.json?
* PHP generated data (requires cross-domain cors)
<?php/** * Test axios=> people.json */header (' Content-type:text/json; Charset=utf-8 '); Header (' access-control-allow-origin:* '); header (' Access-control-allow-methods:get '); Header (' Access-control-allow-headers:x-requested-with,content-type '); $data = new \stdclass (); $data->people = [ [ "id] + 1, " name "=" Jack ", " age "and" age "," ge NDEr "+" Male " ], [ " id "= 2, " name "=" Bill ", " age "=" gender "and" Male " ], [ " id "= 3, " name "=" Tracy ", " age "=" gender "and" Female " ], [ "id" = 4, "name" = "Chris", "age" = +, "gender" = "Male" ], [ "id" = 5, "name" = "guanmengying", "age" and " gender" = "Female" ]];echo Json_encode ($data, true);
* Start PHP interface
Php-s 0.0.0.0:8081
* Test this interface can be accessed by the browser http://localhost:8081/person.php
or command-line Curl http://localhost:8081/people.php
Output:
{"People": [{"id": 1, "name": "Jack", "age": +, "gender": "Male"},{"id": 2, "name": "Bill", "Age": "Gender": "Male"},{" ID ": 3," "Name": "Tracy", "Age": $, "gender": "Female"},{"id": 4, "name": "Chris", "Age": $, "gender": "Male"},{"id": 5, " Name ":" Guanmengying "," Age ":" Gender ":" Female "}]}
* Routing files./src/router/index.js
According to the default HelloWorld divert, you can
Import vue from ' Vue ' import Router from ' vue-router ' import HelloWorld from ' @/components/helloworld ' import person from ' @/ Components/person ' Vue.use (Router) export default new Router ({ routes: [ { path: '/', name: ' HelloWorld ', component:helloworld }, { path: '/person ', name: ' Person ', Component:person } ]})
* Template file./src/components/person.vue
<template><div class= "wrapper" > <fieldset> <legend> Create New Person </legend> <div class= "Form-group" > <label>name: </label> <input type= "text" v-model= "Newperson.name"/> </div> <div class= "Form-group" > <label>Age:</label> <input Type = "text" v-model= "Newperson.age"/> </div> <div class= "Form-group" > <label>gender:</label> <select v-model= "Newperson.gender" > <option value= "Male" >Male</option> <option value= "Female" >Female</option> </select> </div> <div class= "Form-group" > &L T;label></label> <button @click = "Createperson" >Create</button> </div> </fieldset> < table> <thead> <tr> <th>Name</th> <th>Age</th> <th>gender</th> <th>Delete</th> </tr> </thead> <!--cycle must specify key---<tbody> <tr v-for= "(person) in people": key= "Person.id" > <td>{{person.name}}</td> <td>{{person.age}}</td> <td>{{person.gender}}</td> <td:class= "' Text-center '" > <!--<button @click = "Deleteperson ($index)" >de Lete</button>--<button @click = "Deleteperson (person.id)" >Delete</button> </td> </tr> </tbody> </table></div> </templa te><style> #app {margin-top:10px;} Table {margin:20px Auto;} </style><script>export Default {name: ' Person ', created:function () {var v = this; V. $http. Get ("http://localhost:8081/people.php"). Then (function (resp) {v.people = Resp.data.people; }). catch (function (error) {document.write (error.tostring ()); }); }, data () {return {newperson: {name: "", age:0, Gender: "Male"}, People: [] }; }, Methods: {createperson:function () {This.people.push (This.newperson); After adding the Newperson object, reset the Newperson object This.newperson = {name: "", age:0, Gender: "Male"}; }, Deleteperson:function (ID) {//Find index var index, person; person = This.people.find (the function (person, idx) {var Matchid = Person.id = = = ID; IF (matchid) {index = IDX; } return Matchid; }); Delete an array element This.people.splice (index, 1); }}};</script>
Major changes:
Created:function () { var v = this; Vue instance v. $http . Get ("http://localhost:8081/people.php") //XHR . Then (function (RESP) { v.people = resp.data.people; Bind data }) . catch (function (error) { document.write (error.tostring ()); }); }, Data () { return { //... People: [] //empty array. Null can cause template compiling error }; },
* Browser Access Http://localhost:8080/#/person
Inserting emoji in the CSDN blog will invalidate subsequent content and re-edit it.
Https://emojipedia.org/smiling-face-with-sunglasses/so don't use emoji.
CSDN This article lost, dump here.
And all the articles that were created in CSDN are also not on the Vue.
Vue.js configuration Axios used for AJAX request data