Vue.js快速入门

\

1.VueJS 概述与快速入门

1.1VueJS介绍

Vue.js是一个构建数据驱动的 web 界面的渐进式框架。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。它不仅易于上手,还便于与第三方库或既有项目整合。
官网:https://cn.vuejs.org/

1.2MVVM模式

MVVM是Model-View-ViewModel的简写。它本质上就是MVC 的改进版。MVVM 就是将其中的View 的状态和行为抽象化,让我们将视图 UI 和业务逻辑分开
MVVM模式和MVC模式一样,主要目的是分离视图(View)和模型(Model)
Vue.js 是一个提供了 MVVM 风格的双向数据绑定的 Javascript 库,专注于View 层。它的核心是 MVVM 中的 VM, 也就是 ViewModel。 ViewModel负责连接 View 和 Model,保证视图和数据的一致性,这种轻量级的架构让前端开发更加高效、便捷
1.3VueJS 快速入门
Vue.js快速入门_第1张图片

1.3VueJS 快速入门







快速入门



{{message}}

1.4插值表达式

数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值,Mustache 标签将会被替代为对应数据对象上属性的值。无论何时,绑定的数据对象上属性发生了改变,插值处的内容都会更新。
Vue.js 都提供了完全的 JavaScript 表达式支持。


{{ number + 1 }}

{{ ok ? 'YES' : 'NO' }}

这些表达式会在所属 Vue 实例的数据作用域下作为 JavaScript 被解析。有个限制就是,每个绑定都只能包含单个表达式,所以下面的例子都不会生效。


{{ var a = 1 }}

{{ if (ok) { return message } }}

2.VueJS 常用系统指令

2.1v-on

可以用 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码

2.1.1v-on:click








事件处理 v-on示例1



{{message}}

2.1.2v-on:keydown






事件处理 v-on示例2



2.1.3v-on:mouseover






事件处理 v-on示例3



2.1.4事件修饰符
Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或
event.stopPropagation()。
Vue.js通过由点(.)表示的指令后缀来调用修饰符。
.stop
.prevent
.capture
.self
.once






v-on 事件修饰符








2.1.5按键修饰符
Vue 允许为 v-on 在监听键盘事件时添加按键修饰符全部的按键别名:
.enter
.tab
.delete(捕获 “删除” 和 “退格” 键)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta






v-on 按钮修饰符




Do something

v-on简写方式


...

...

2.2v-text与v-html






v-html与v-text



2.3v-bind

插值语法不能作用在 HTML 特性上,遇到这种情况应该使用 v-bind指令






v-bind



传智播客 黑马程序员
itcast

v-bind简写方式


...

...

2.4v-model






v-model



姓名:
密码:

2.5v-for

操作array






v-model



  • {{item+" "+index}}

操作对象






v-for示例1



  • {{key}}--{{value}}

操作对象数组






v-for示例1



序号 名称 价格
{{p.id}} {{p.pname}} {{p.price}}

v-if是根据表达式的值来决定是否渲染元素
v-show是根据表达式的值来切换元素的display css属性






v-if与v-show



传智播客 itcast

3.VueJS生命周期

每个 Vue 实例在被创建之前都要经过一系列的初始化过程.
Vue.js快速入门_第2张图片
vue 在 生 命 周 期 中 有 这 些 状 态 , beforeCreate,created,beforeMount,mounted,beforeUpdate,updated,beforeDestroy,destroyed 。 Vue 在实例化的过程中,会调用这些生命周期的钩子,给我们提供了执行自定义逻辑的机会。那么,在这些vue钩子 中,vue实例到底执行了那些操作,我们先看下面执行的例子







生命周期




{{message}}

vue对象初始化过程中,会执行到beforeCreate,created,beforeMount,mounted 这几个钩子的内容
beforeCreate :数据还没有监听,没有绑定到vue对象实例,同时也没有挂载对象
created :数据已经绑定到了对象实例,但是还没有挂载对象
beforeMount: 模板已经编译好了,根据数据和模板已经生成了对应的元素对象,将数据对象关联到了对象的el属性,el属性是一个HTMLElement对象,也就是这个阶段,vue实例通过原生的createElement等方法来创建这个html片段,准备注入到我们vue实例指明的el属性所对应的挂载点
mounted:将el的内容挂载到了el,相当于我们在jquery执行了(el).html(el),生成页面上真正的dom,上面我们 就会发现dom的元素和我们el的元素是一致的。在此之后,我们能够用方法来获取到el元素下的dom对象,并进 行各种操作
当我们的data发生改变时,会调用beforeUpdate和updated方
beforeUpdate :数据更新到dom之前,我们可以看到$el对象已经修改,但是我们页面上dom的数据还没有发生改变
updated: dom结构会通过虚拟dom的原则,找到需要更新页面dom结构的最小路径,将改变更新到dom上面,完成更新
beforeDestroy,destroed :实例的销毁,vue实例还是存在的,只是解绑了事件的监听还有watcher对象数据与view的绑定,即数据驱动

4.VueJS ajax

4.1vue-resource

vue-resource是Vue.js的插件提供了使用XMLHttpRequest或JSONP进行Web请求和处理响应的服务。 当vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios,在这里大家了解一下vue-resource就可以。
vue-resource的github: https://github.com/pagekit/vue-resource

4.2axios

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中
axios的github:https://github.com/axios/axios
4.2.1引入axios
首先就是引入axios,如果你使用es6,只需要安装axios模块之后

import axios from 'axios';
//安装方法
npm install axios
//或
bower install axios

当然也可以用script引入



4.2.2get请求

//通过给定的ID来发送请求axios.get('/user?ID=12345')
.then(function(response){ console.log(response);
})
.catch(function(err){ console.log(err);
});
//以上请求也可以通过这种方式来发送axios.get('/user',{
params:{ ID:12345
}
})
.then(function(response){ console.log(response);
})
.catch(function(err){ console.log(err);
});

4.2.3post请求


axios.post('/user',{ firstName:'Fred', lastName:'Flintstone'
})
.then(function(res){ console.log(res);
})
.catch(function(err){ console.log(err);
});

为方便起见,为所有支持的请求方法提供了别名
axios.request(config)
axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, config]]) axios.patch(url[, data[, config]])

5.综合案例

5.1案例需求

完成用户的查询与修改操作

5.2数据库设计与表结构


CREATE DATABASE vuejsdemo; USE vuejsdemo;
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
age INT,
username VARCHAR(20), PASSWORD VARCHAR(50), email VARCHAR(50), sex VARCHAR(20)
)

User类


public class User { private Integer id;
private String username; private String password; private String sex; private int age;
private String email;
省略getter/setter
}

5.3服务器端

5.3.1配置文件
pom.xml




4.0.0

com.itheima.vuejsDemo
vuejsDemo
1.0-SNAPSHOT
war

vuejsDemo Maven Webapp

http://www.example.com


UTF-8
1.8
1.8
5.0.2.RELEASE
1.6.6
1.2.12
3.4.5




	

org.aspectj
aspectjweaver
1.6.8


org.springframework
spring-aop
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-context-support
${spring.version}


org.springframework
spring-web
${spring.version}



org.springframework
spring-orm
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-core
${spring.version}


org.springframework
spring-test
${spring.version}


org.springframework
spring-webmvc
${spring.version}



org.springframework
spring-tx
${spring.version}


junit
junit
4.12
test


javax.servlet
javax.servlet-api
3.1.0
provided


javax.servlet.jsp
jsp-api
2.0
provided


jstl
jstl
1.2

	

log4j
log4j
${log4j.version}


org.slf4j
slf4j-api
${slf4j.version}


org.slf4j
slf4j-log4j12
${slf4j.version}
	


org.mybatis
mybatis
${mybatis.version}


org.mybatis
mybatis-spring
1.3.0


c3p0
c3p0
0.9.1.2
jar
compile


com.github.pagehelper
pagehelper
5.1.2


mysql
mysql-connector-java
5.1.5


com.fasterxml.jackson.core
jackson-core
2.9.5



com.fasterxml.jackson.core
jackson-databind

2.9.5





vuejsDemo



maven-clean-plugin
3.0.0



maven-resources-plugin
3.0.2


maven-compiler-plugin
3.7.0


maven-surefire-plugin
2.20.1


maven-war-plugin
3.2.0


maven-install-plugin
2.5.2


maven-deploy-plugin
2.8.2






org.apache.tomcat.maven
tomcat7-maven-plugin
2.2







web.mxl





contextConfigLocation
classpath:applicationContext.xml



 org.springframework.web.context.ContextLoaderListener




springmvcDispatcherServlet
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:springmvc.xml


1


springmvcDispatcherServlet
*.do



CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter


encoding
UTF-8



forceEncoding
true




CharacterEncodingFilter
/*


index.html
index.htm
index.jsp
default.html
default.htm
default.jsp


springmvc.xml











applicationContext.xml


































db.properties


jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/vuejsDemo
 jdbc.username=root
jdbc.password=root

5.3.2Controller


@RequestMapping("/user") 
@Controller 
@ResponseBody
public class UserController {
@Autowired
private IUserService userService;

@RequestMapping(value="/findAll.do") public List findAll() {
return userService.findAll();
}
@RequestMapping(value="/findById.do") public User findById(Integer id) {
return userService.findById(id);
}
@RequestMapping(value="/update.do")
public User update(@RequestBody User user) { return userService.update(user);
}
}

5.3.3Dao



public interface IUserDao {

@Select("select * from user") public List findAll();
@Select("select * from user where id=#{id}") User findById(Integer id);
@Update("update user set username=#{username},password=#{password},sex=#{sex},age=#
{age},email=#{email} where id=#{id}") void update(User user);
}

5.4客户端

5.4.1user.html页面









数据 - AdminLTE2定制版



































5.4.2user.js页面

var vue = new Vue({ el: "#app", data: {
user: {id:"",username:"aaa",password:"",age:"",sex:"",email:""},

userList: []
},
methods: {
findAll: function () { var _this = this;
axios.get("/vuejsDemo/user/findAll.do").then(function (response) {
_this.userList = response.data; console.log(_this.userList);
}).catch(function (err) { console.log(err);
});
},
findById: function (userid) { var _this = this;
axios.get("/vuejsDemo/user/findById.do", { params: {
id: userid
}
}).then(function (response) {
_this.user = response.data;
$('#myModal').modal("show");
}).catch(function (err) {
});

},
update: function (user) { var _this = this;
axios.post("/vuejsDemo/user/update.do",_this.user).then(function (response) {
_this.findAll();
}).catch(function (err) {
});
}
},
created(){
this.findAll();
}
});

你可能感兴趣的:(java,java,ssh,javaweb,js)