Vue(读音/Vju:/)是用于动态构建用户界面的渐进式前端框架,开发者是尤雨溪。渐进式的含义是
Vue可以自底向上被逐层应用(增量开发),不像其它前端框架(Angular,React)有较强的主张(用Angular必须使用它的模块机制和依赖注入,用React需要接受它的函数式编程理念),Vue主张最少,没有多做职责之外的事(Vue2.0 中,“渐进式框架”和“自底向上增量开发的设计”这两个概念是什么? - 徐飞的回答 - 知乎)
<div id="app">
<input type="text" v-model="username">
<p>hello, {
{username}}p>
div>
<script type="text/javascript" src="../js/vue.js">script>
<script>
new Vue({
el: "#app",
data: {
username: "ygj"
})
}
script>
View(DOM)--->ViewModel(DOM Listeners)--->Model(JS对象)
View(DOM)<---ViewModel(Data Bindings)<---Model(JS对象)
v-bind:xxx = 'yyy'
// 简写: :xxx='yyy'
v-on:keyup='fun' // 简写:@keyup='fun'
v-on:keyup='fun(参数)'
v-on:keyup.enter='fun' // 简写:@keyup.enter='fun'
<div id="app">
<input type="text" v-model="username">
<p>hello, {
{username}}p>
div>
表单值会随着用户输入而改变,同时页面显示也会随着表单值而改变
<div id="demo">
姓:<input type="text" placeholder="FirstName" v-model="firstName"><br>
名:<input type="text" placeholder="LastName" v-model="lastName"><br>
姓名1(单向):<input type="text" placeholder="FullName" v-model="fullName1"><br>
姓名2(单向):<input type="text" placeholder="FullName" v-model="fullName2"><br>
姓名3(双向):<input type="text" placeholder="FullName2" v-model="fullName3"><br>
div>
<script type="text/javascript" src="../js/vue.js">script>
v-model=方法名
或{ {方法名}}
显示计算结果 var vm=new Vue({
el:'#demo',
data:{
firstName:'Kobe',
lastName:'bryant',
fullName2:'Kobebryant'
},
computed:{
fullName1: function() {
return this.firstName + " " + this.lastName;
}
}
})
var vm=new Vue({
el:'#demo',
data:{
firstName:'Kobe',
lastName:'bryant',
fullName2:'Kobebryant'
},
watch: {
lastName: function(newVal) {
this.fullName2 = this.firstName + ' ' + newVal;
}
}
})
/*
vm.$watch('lastName', function(val) {
this.fullName2 = this.firstName + ' ' + val;
})
*/
var vm=new Vue({
el:'#demo',
data:{
firstName:'Kobe',
lastName:'bryant',
fullName2:'Kobebryant'
},
computed:{
fullName1: function() {
return this.firstName + " " + this.lastName;
}, //改动姓名1中表单的值,firstName和lastName不会变
fullName3: {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function(value) {
var names = value.split(' ');
this.firstName = names[0];
this.lastName = names[1];
} // 改动姓名3中表单的值,firstName和lastName会跟着变
},
}
})
:class='xxx'
classA
{classA: isA, classB: isB}
['classA', 'classB']
:style="{color: activeColor, fontSize: fontSize+'px'}
v-if和v-else
v-show
v-show为false表示不显示,但还是在内存里面
<div id="demo">
<h2 v-if="ok">表白成功h2>
<h2 v-else>表白失败h2>
<h2 v-show="ok">求婚成功h2>
<h2 v-show="!ok">求婚失败h2>
<button @click="ok=!ok">切换button>
<button @click="fun">切换button>
div>
<script type="text/javascript" src="../js/vue.js">script>
<script>
var vm=new Vue({
el:'#demo',
data:{
ok: false
},
methods: {
fun: function() {
this.ok = !this.ok;
}
}
})
script>
注意点:
this.objList[idx] = newVal
无效,需要使用this.objList.splice(idx, 1, newVal)
才有效<input type="text" v-model="user.username">
<textarea v-model="user.desc" rows="10">textarea>
<input type="radio" value="male" v-model="user.sex">
<input type="checkbox" value="football" v-model="user.likes">
<input type="checkbox" value="basketball" v-model="user.likes">
<select v-model="user.cityId">
<option v-for="city in allCitys" :value="city.id">
{
{city.name}}
option>
select>
new Vue() {
el: 'div',
data: {
msg: 'xxx',
isShowing: true
},
created() {
this.intervalId = setInterval(() => {
this.isShowing=!this.isShowing;}, 1000); // 直接在匿名函数中写this.isShowing=!this.isShowing识别不到正确的this,匿名函数中的this会被视作window,而使用箭头函数会沿着this的作用域链往外找
},
beforeDestroy() {
clearInterval(this.intervalId);
}
}
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-to {
opacity: 0
}
Vue.filter(filterName, function(value[,arg1, arg2, ...]) {
// 进行一定的数据处理
return newValue;
})
<div>{
{myData | filterName}}div>
<div>{
{myData | filterName(args)}}div>
$refs.标识名
访问这个元素对象<head>
<style>
[v-cloak] {
/* 属性选择器 */
display: none
}
style>
head>
<body>
<div id="test">
<p :value="msg" v-cloak>{
{msg}}p>
div>
<script>
alert("wait");
new Vue({
el: "#test",
data: {
msg: "hello"
}
})
script>
body>
Vue.directive('upper-text', function(el, binding) {el.innerHTML = binding.value.toUpperCase()})
directives: {
'upper-text': {
bind(el, binding) {
el.innerHTML = binding.value.toUpperCase();
}
}
}
v-upper-text='xxx'
Vue.use(插件名)
来使用插件