在組件vue文件中變量都是整個做用域,不可以使用this。在js文件,this用於指代data中的變量。前端
一. 環境搭建vue
1.安裝node.js,從node.js官網下載並安裝node,安裝過程很簡單,一路「下一步」就能夠了(傻瓜式安裝)。安裝完成以後,打開命令行工具(win+r,而後輸入cmd),輸入 node -v,以下圖,若是出現相應的版本號,則說明安裝成功。java
2.安裝淘寶鏡像。windows下安裝。。node
linux下安裝。。。linux
二. 構建一個axios+java項目
$ vue init webpack exprice --------------------- 這個是那個安裝vue腳手架的命令
This will install Vue 2.x version of the template. ---------------------這裏說明將要建立一個vue 2.x版本的項目
For Vue 1.x use: vue init webpack#1.0 exprice
? Project name (exprice) ---------------------項目名稱
? Project name exprice
? Project description (A Vue.js project) ---------------------項目描述
? Project description A Vue.js project
? Author Datura --------------------- 項目建立者
? Author Datura
? Vue build (Use arrow keys)
? Vue build standalone
? Install vue-router? (Y/n) --------------------- 是否安裝Vue路由,也就是之後是spa(但頁面應用須要的模塊)
? Install vue-router? Yes
? Use ESLint to lint your code? (Y/n) n ---------------------是否啓用eslint檢測規則,這裏我的建議選no
? Use ESLint to lint your code? No
? Setup unit tests with Karma + Mocha? (Y/n)
? Setup unit tests with Karma + Mocha? Yes
? Setup e2e tests with Nightwatch? (Y/n)
? Setup e2e tests with Nightwatch? Yes
vue-cli · Generated "exprice".
webpack
3.<重點>cd 命令進入建立的工程目錄,首先cd exprice(這裏是本身建工程的名字);
4.在package.json文件,加入axios
"dependencies": { "axios": "^0.17.0", "vue": "^2.5.2", "vue-router": "^3.0.1" },
5 . 在config下的index.js配置代理,將proxyTable{}配置爲
proxyTable: { /** * npm代理,解決調試接口中,跨域問題 * */ '/api': { target: 'http://192.168.1.91:8090', // 接口地址 changeOrigin: true, pathRewrite: { '^/api': '' } } },
6. 在src下的main.js引入axios,ios
// 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 App from './App' import router from './router' Vue.config.productionTip = false; import axios from 'axios'; let axiosIns = axios.create({ // 發佈 baseURL:'/', // 開發 baseURL: '/api/', }); Vue.prototype.$http = axiosIns; /* eslint-disable no-new */ new Vue({ el: '#app', router, template: '<App/>', components: { App } })
7. 在hello.vue 中添加表單和方法,這樣vue前端就配置完了。git
<template> <div class="hello"> <input type="text" v-model="tip"/> <input type="button" @click = "aa"/> </div> </template> <script> export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App', tip :'' } }, methods:{ aa(){ let self = this; this.$http.post('/vprocesslogin', {tip:self.tip}) .then(function (response) { if (response.data.success) { console.log(response); } }) } } } </script>
8. 安裝axios 依賴npm install axios --save
9.安裝項目依賴:npm install,由於自動構建過程當中已存在package.json文件,因此這裏直接安裝依賴就行。
10。npm run dev 便可運行。
8. java後臺配置web
tomcat配置server.xml的Context的path要配爲「/」vue-router
<Context path="/" docBase="*******" debug="0" reloadable="true" crossContext="true" />
struts配置,寫一個jsonaction,添加json攔截器。其餘寫法保持原ssh框架不變。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- Struts2配置文件的根元素 --> <struts> <package name="struts-demo" extends="struts-main"> <interceptors> <interceptor-stack name="demostack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="json"/> </interceptor-stack> </interceptors> <action name="vprocesslogin" class="vLoginAction" method="vprocesslogin"> <result type="json" /> <interceptor-ref name="demostack" /> </action> </package> </struts>
這樣後臺action就能收到vue前臺提交上來的數據
public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public String getForwardUrl() { return forwardUrl; } public void setForwardUrl(String forwardUrl) { this.forwardUrl = forwardUrl; } public String execute() throws Exception { return SUCCESS; } public void vprocesslogin() throws Exception { System.out.println(tip); System.out.println(tip); tip = "erty"; System.out.println(tip); }