vue框架
2017-04-01 10:30:00 0 举报
AI智能生成
Vue.js是一款用于构建用户界面的渐进式JavaScript框架,由尤雨溪在2014年创建并维护。它的核心库只关注视图层,易于上手且便于与其他库或已有项目整合。Vue采用组件化开发,使得代码复用和维护变得更加容易。此外,它还提供了响应式数据绑定和虚拟DOM技术,使得界面渲染更加高效。Vue还支持指令、过滤器、计算属性等功能,以满足各种开发需求。总之,Vue是一个功能强大且灵活的前端框架,适用于构建各种规模的应用程序。
作者其他创作
大纲/内容
基础设置
输入命令行
全局安装 vue-cli
npm install --global vue-cli
创建一个基于 webpack模板的新项目
vue init webpack 项目名称
安装依赖
cd 项目名称
npm install
npm run dev
vue不支持IE8以下的版本
vue简介
Vue.js 是一套构建用户界面的渐进式框架
Vue 采用自底向上增量开发的设计
Vue 的核心库只关注视图层
当与单文件组件和 Vue 生态系统支持的库结合使用时,Vue 也完全能够为复杂的单页应用程序提供驱动
Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM
Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。
vue基础用法
在Vuejs中变量在html中绑定标签是两个花括号{{变量}}
在vuejs中的获取页面中id不可以放在body和html标签里
vuejs启用的区域就在id为demo的div块中
vue js的执行顺序
1.在页面引入vue js
script
2.实例化vue js
new Vue({ })
3.创建vue js的应用区域
el: ' '
4.填充绑定数据
data:{ msg:' '}
vuejs数据双向绑定
主要使用vuejs的v-model属性
注意
v-model 指令在表单控件元素上创建双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素。
vuejs双向绑定并不能适用于所有的html表单,限制为input、select、textarea、components。
使用
例如:我们创建一个文本框并定义它的v-model的属性的内容为我们在vue中定义的msg
<input type="text" v-model="msg">
运行效果
我们在改变文本框内容的同时,在标签中的msg也同时会改变。以后我们说双向绑定,都是指的是类似input这样的,自带change事件的表单项目。
vuejs中的v-text属性
v-text属性来控制整个标签的内容绑定
使用
例如:我们把v-text属性放到span标签中
<span v-text='msg'>hello<span>
运行效果
我们可以看到在span标签中的内容并没有被输出,输出的是我们在vue定义的变量msg的内容。v-text属性会把整个标签的内容绑定为我们定义的内容。
Vuejs的常用指令
v-html
作用
v-html 更新元素或者变量的innerHTML,按普通html解析,和v-text的区别是在变量中的html标签会被浏览器解析,比如<br>会直接解析为换行
使用
例如:<div v-html="msg"></div>
data:{
msg:'第一行<br><hr>第二行'
}
msg:'第一行<br><hr>第二行'
}
效果
第一行和第二行中间有条线同时也换行了。
v-show
作用
v-show 根据条件解析元素display的属性的真假,如果为真则display为block,假的话则为none在页面不显示。
使用
例如:<span v-show="Show">你能看到我!</span>
<span v-show="Hide">你能看到我吗!</span>
<span v-show="Hide">你能看到我吗!</span>
data:{
Show:true,
Hide:false
}
Show:true,
Hide:false
}
效果
我们在vue中定义了两个变量,Show为true,为true的话display的属性为block直接显示在页面中。
v-if
作用
v-if、v-else、v-else-if,if三兄弟,条件判断,这个跟我们平时见到的if语句类似,根据表达式的值的真假条件渲染元素。在切换时元素及它的数据绑定 / 组件被销毁并重建。
使用
例如: <p>第一组</p>
<p>预期显示“你能看到我!”</p>
<span v-if="Show">你能看到我!</span>
<span v-else>你能看到我吗!</span>
<br>
<p>第二组</p>
<p>预期显示“你能看到我吗!”</p>
<span v-if="hide">你能看到我!</span>
<span v-else>你能看到我吗!</span>
<br>
<p>第三组</p>
<p>预期显示“你真的能看到我吗!”</p>
<span v-if="hide">你能看到我!</span>
<span v-else-if="hide">你能看到我吗!</span>
<span v-else>你真的能看到我吗!</span>
<p>预期显示“你能看到我!”</p>
<span v-if="Show">你能看到我!</span>
<span v-else>你能看到我吗!</span>
<br>
<p>第二组</p>
<p>预期显示“你能看到我吗!”</p>
<span v-if="hide">你能看到我!</span>
<span v-else>你能看到我吗!</span>
<br>
<p>第三组</p>
<p>预期显示“你真的能看到我吗!”</p>
<span v-if="hide">你能看到我!</span>
<span v-else-if="hide">你能看到我吗!</span>
<span v-else>你真的能看到我吗!</span>
data:{
Show:true,
hide:false
}
Show:true,
hide:false
}
效果
解读: 第一组,v-if调用show,即为true。而在条件语句中,如果if为true则else中的不执行。同理我们的第一组显示第一个span标签内容。
第二组,v-if调用hide,为false。而在if语句中第一个条件为假,则执行else中的内容,因此显示第二个span标签中的内容。
第三组,我们把v-if和v-else-if属性全部设置为hide,在执行过程中v-if为false,不显示,v-else-if也为flase也不显示,前面都为假的情况我们显示v-else的内容。
第二组,v-if调用hide,为false。而在if语句中第一个条件为假,则执行else中的内容,因此显示第二个span标签中的内容。
第三组,我们把v-if和v-else-if属性全部设置为hide,在执行过程中v-if为false,不显示,v-else-if也为flase也不显示,前面都为假的情况我们显示v-else的内容。
注意
1.v-else不需要表达式,但是需要跟在v-if和v-else-if后面
2.我们在实验中直接使用了true和false在实际使用中可以使用表达式,例如v-if="1>3"
v-if是一个指令,需要将它添加到一个元素上。但如果我们想切换多个元素,我们可以引入<template>元素当作包装元素,并在上面使用v-if
v-for
作用
循环遍历
使用
例如:<div v-for="item in items">
{{ item.text }}
</div>
<div v-for="item in items">
{{ item.name }}
</div>
<div v-for="item in items">
{{ item.text }}{{ item.name }}
</div>
<div v-for="item in items">
{{ item }}
</div>
{{ item.text }}
</div>
<div v-for="item in items">
{{ item.name }}
</div>
<div v-for="item in items">
{{ item.text }}{{ item.name }}
</div>
<div v-for="item in items">
{{ item }}
</div>
说明:items为数据,item为数据的元素,text为数据中的元素内容
data:{
items:[
{text:'hello',name:'实验楼'},
{text:'hello',name:'vuejs'},
{text:'hello',name:'html'},
{text:'hello',name:'js'},
]
}
items:[
{text:'hello',name:'实验楼'},
{text:'hello',name:'vuejs'},
{text:'hello',name:'html'},
{text:'hello',name:'js'},
]
}
效果
上述示例中我们可以看到v-for的使用方法 items 数据对象放到一个数组里,标准json格式。 在我们使用中如果需要得到每条内容的索引,我们可以给数据添加索引。v-for="(item,index) in items"这样我们就给每条遍历出来的数据添加了索引。
v-on
作用
绑定事件监听
使用
<!-- 方法处理器 -->
<button v-on:click="doThis"></button>
<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>
<!-- 缩写 -->
<button @click="doThis"></button>
<!-- 停止冒泡 -->
<button @click.stop="doThis"></button>
<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>
<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>
<!-- 串联修饰符 -->
<button @click.stop.prevent="doThis"></button>
<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">
<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">
<button v-on:click="doThis"></button>
<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>
<!-- 缩写 -->
<button @click="doThis"></button>
<!-- 停止冒泡 -->
<button @click.stop="doThis"></button>
<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>
<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>
<!-- 串联修饰符 -->
<button @click.stop.prevent="doThis"></button>
<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">
<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">
data:{
msg:'单击按钮',
name:'vuejs'
},
methods:{
doThis:function(e){
console.log(this);
//console.log(e.target.tagName);
console.log(e.target);
console.log('hello'+this.name+'!');
}
}
msg:'单击按钮',
name:'vuejs'
},
methods:{
doThis:function(e){
console.log(this);
//console.log(e.target.tagName);
console.log(e.target);
console.log('hello'+this.name+'!');
}
}
效果
在页面中的按钮绑定了一个单击事件dothis,在dothis中我们在控制台中输出hello vuejs。 在vuejs中方法绑定在methods中定义。
vuejs中经常使用的修饰性功能
push( ) //在结尾增加一条或多条数据
pop( ) //删除最后一条数据
shift( ) //删除第一条数据,并返回这条数据
unshift( ) //在开始增加一条或多条数据,并返回数组长度
splice( ) //向/从数组中添加/删除项目,然后返回被删除的项目。
sort( ) //对数组的元素进行排序。
reverse( ) //颠倒数组中元素的顺序。
pop( ) //删除最后一条数据
shift( ) //删除第一条数据,并返回这条数据
unshift( ) //在开始增加一条或多条数据,并返回数组长度
splice( ) //向/从数组中添加/删除项目,然后返回被删除的项目。
sort( ) //对数组的元素进行排序。
reverse( ) //颠倒数组中元素的顺序。
vuejs组件
template
一个字符串模板作为 Vue 实例的标识使用。模板将会替换 挂载的元素。 如果值以 # 开始,则它用作选项符,将使用匹配元素的 innerHTML 作为模板。常用的技巧是用 <script type="x-template"> 包含模板。
v-bind指令
作用
v-bind:动态地绑定一个或多个特性,或一个组件 prop 到表达式。在绑定 class 或 style 特性时,支持其它类型的值,如数组或对象。v_bind:class在应用中是非常有用的。
使用
第一种
<!-- 绑定一个属性 -->
<img v-bind:src="imageSrc">
<!-- 缩写 -->
<img :src="imageSrc">
<!-- with inline string concatenation -->
<img :src="'/path/to/images/' + fileName">
<!-- class 绑定 -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]">
<!-- style 绑定 -->
<div :style="{ fontSize: size + 'px' }"></div>
<div :style="[styleObjectA, styleObjectB]"></div>
<!-- 绑定一个有属性的对象 -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
<!-- 通过 prop 修饰符绑定 DOM 属性 -->
<div v-bind:text-content.prop="text"></div>
<!-- prop 绑定. “prop” 必须在 my-component 中声明。 -->
<my-component :prop="someThing"></my-component>
<!-- XLink -->
<svg><a :xlink:special="foo"></a></svg>
<img v-bind:src="imageSrc">
<!-- 缩写 -->
<img :src="imageSrc">
<!-- with inline string concatenation -->
<img :src="'/path/to/images/' + fileName">
<!-- class 绑定 -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]">
<!-- style 绑定 -->
<div :style="{ fontSize: size + 'px' }"></div>
<div :style="[styleObjectA, styleObjectB]"></div>
<!-- 绑定一个有属性的对象 -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
<!-- 通过 prop 修饰符绑定 DOM 属性 -->
<div v-bind:text-content.prop="text"></div>
<!-- prop 绑定. “prop” 必须在 my-component 中声明。 -->
<my-component :prop="someThing"></my-component>
<!-- XLink -->
<svg><a :xlink:special="foo"></a></svg>
第二种 class绑定
<style>
.active{
color: red;
}
</style>
.active{
color: red;
}
</style>
<a v-bind:class="{ active: isActive }">红色</a>
data:{
isActive:true
//isActive:false
}
isActive:true
//isActive:false
}
效果
当isActive为true时,样式渲染为<a class="active">红色</a>,false时则渲染为<a class="">红色</a>
Vuejs组件
作用
组件Component是 Vue.js 最强大的功能之一
组件可以扩展 HTML 元素,封装可重用的代码
在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能。在有些情况下,组件也可以是原生 HTML 元素的形式,以 is 特性扩展。
上述为官方定义。顾名思义,就是把一段html代码封装起来,无论是小项目还是大项目,如何复杂的界面组织,都可以把它拆分成一个个可复用的组件,比如我们实验楼网站的版权部分,每个页面都是一样,就可以把它单独拿出来,需要的时候直接调用,我们可以自定义这段标签的内容。组件就是把页面中的交互模块独立出来,变成一个个独立的部分,方便我们重复使用。
使用
第一种
<div id="demo">
<my-component></my-component>
</div>
<my-component></my-component>
</div>
<script>
//注册组件,my-component为自定义标签
Vue.component('my-component', {
template: '<div>一个vuejs组件例子!</div>'
})
new Vue({
el:'#demo'
})
</script>
//注册组件,my-component为自定义标签
Vue.component('my-component', {
template: '<div>一个vuejs组件例子!</div>'
})
new Vue({
el:'#demo'
})
</script>
注意
my-component是组件的名称
html中: 把组件名称当做一个标签来写即可
后面的{ }里可以写参数,我们看到这里写了一个template,它是组件将会渲染的html内容
Vuejs组件注册要写在前面,Vue实例初始化要写在后面. template可以写正常的html语法
第二种:使用css选择器的ID 首先我们要定义一个tempalte
<div id="demo">
<my-component></my-component>
</div>
<my-component></my-component>
</div>
可以重复调用 :my-component标签
<script type="text/tmplate-x" id="my_component-template">
<div>一个vuejs组件例子!</div>
</script>
<div>一个vuejs组件例子!</div>
</script>
<script>
Vue.component('my-component', {
template: '#my_component-template'
})
new Vue({
el:'#demo'
})
</script>
Vue.component('my-component', {
template: '#my_component-template'
})
new Vue({
el:'#demo'
})
</script>
在注册组件中直接调用:#my_component-template
效果
在上图中可以看到,正常输出了我们在template中输出的内容。 vue会把<my-component></my-component>渲染成
<div>一个vuejs组件例子!</div>
<div>一个vuejs组件例子!</div>
在组件中如何使用在vue里注册的变量
步骤
1.在组件中定义一个props
2.然后在我们使用的自定义标签中绑定
使用
<div id="demo">
<my-component v-bind:msg='msg'></my-component>
<my-component></my-component>
<my-component></my-component>
</div>
<my-component v-bind:msg='msg'></my-component>
<my-component></my-component>
<my-component></my-component>
</div>
<script type="text/x-tmplate" id="my_component">
<div>一个vuejs组件例子! {{msg}}</div>
</script>
<div>一个vuejs组件例子! {{msg}}</div>
</script>
<script>
Vue.component('my-component', {
template: '#my_component',
props:['msg'],
data:[
]
})
new Vue({
el:'#demo',
data:{
msg:'vue.js组件'
}
})
</script>
Vue.component('my-component', {
template: '#my_component',
props:['msg'],
data:[
]
})
new Vue({
el:'#demo',
data:{
msg:'vue.js组件'
}
})
</script>
效果
一个vuejs组件例子!一个vuejs组件例子!一个vuejs组件例子!
组件单独写到一个js文件中
作用
组件也可以单独的写到一个js文件中,这样更加增加页面重用性,一个页面可以定义使用多个组件
在组件中写的方法,就是应用在组件中,这样就比较分工明确,在组件中data是一个方法,需要返回值,而在vuejs中则是一个对象,我们把上节课添加一个标签的示例修改成组件的例子。
使用
<div id="demo">
<h1>{{msg}}</h1>
<my-component v-bind:items='items'></my-component>
</div>
<h1>{{msg}}</h1>
<my-component v-bind:items='items'></my-component>
</div>
<script type="text/x-tmplate" id="my_component">
<form @submit.prevent="additem(newitem)" >
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</script>
<form @submit.prevent="additem(newitem)" >
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</script>
<script>
Vue.component('my-component', {
template: '#my_component',
props:['msg','items'],
data(){
return {
newitem:{text:''}
}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
new Vue({
el:'#demo',
data:{
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
]
}
})
</script>
</body>
Vue.component('my-component', {
template: '#my_component',
props:['msg','items'],
data(){
return {
newitem:{text:''}
}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
new Vue({
el:'#demo',
data:{
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
]
}
})
</script>
</body>
Vuejs中的组件操作
子组件向父组件中传值: 使用event (输出过程)
父组件向子组件中传值: 使用props binding (输入过程)
父组件调用子组件的方法: 用ref拿到子组件, 然后调用里面的方法 (方法调用)
子组件调用父组件的方法: 使用event(方法调用)
使用v-model 简化
Vuejs计算属性
注意
我们把计算属性都写到computed中,可以让我们的代码看起来更加简洁
计算长度
使用
第一种方法
<div id="demo">
<h1>{{msg}}</h1>
<form @submit.prevent="additem(newitem)" >
<p>{{items.length}}</p>
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</div>
<h1>{{msg}}</h1>
<form @submit.prevent="additem(newitem)" >
<p>{{items.length}}</p>
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</div>
<script>
new Vue({
<!--el指定的id为css选择器-->
el:'#demo',
<!--我们的变量统统放到data中-->
data:{
<!--此处声明的变量msg内容为实验楼-->
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
],
newitem:{text:''}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
</script>
new Vue({
<!--el指定的id为css选择器-->
el:'#demo',
<!--我们的变量统统放到data中-->
data:{
<!--此处声明的变量msg内容为实验楼-->
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
],
newitem:{text:''}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
</script>
第二种方法:在模块中直接使用方法名,在vue中定义方法
<div id="demo">
<h1>{{msg}}</h1>
<form @submit.prevent="additem(newitem)" >
<p>{{itemlenth}}</p>
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</div>
<h1>{{msg}}</h1>
<form @submit.prevent="additem(newitem)" >
<p>{{itemlenth}}</p>
<div v-for="item in items">
<p>{{ item.text }}</p>
</div>
<div>
<input type="text" v-model="newitem.text">
</div>
<div>
<button>添加</button>
</div>
</form>
</div>
new Vue({
<!--el指定的id为css选择器-->
el:'#demo',
<!--我们的变量统统放到data中-->
data:{
<!--此处声明的变量msg内容为实验楼-->
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
],
newitem:{text:''}
},
computed:{
itemlenth(){
return this.items.length
}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
<!--el指定的id为css选择器-->
el:'#demo',
<!--我们的变量统统放到data中-->
data:{
<!--此处声明的变量msg内容为实验楼-->
msg:'实验楼第一个vuejs示例',
items:[
{text:'hello 实验楼'}
],
newitem:{text:''}
},
computed:{
itemlenth(){
return this.items.length
}
},
methods:{
additem(newitem){
this.items.push(newitem)
this.newitem={text:''}
}
}
})
字符串链接
作用
我们也可以使用一个方法把两个字符串连接,这样写计算属性就在页面上看的很干净。
用法
<div id="demo">
{{stringjoin}}
</div>
{{stringjoin}}
</div>
<script>
new Vue({
el:'#demo',
data:{
msg:'hello' ,
name:'实验楼'
},
computed:{
stringjoin(){
return this.msg + ' ' +this.name
}
}
})
</script>
new Vue({
el:'#demo',
data:{
msg:'hello' ,
name:'实验楼'
},
computed:{
stringjoin(){
return this.msg + ' ' +this.name
}
}
})
</script>
注意
把计算属性放到computed中,计算属性依赖data里面的数据,如果data里面的数据不变,计算属性可以自动缓存。
生命周期
图解
文字解释
创建前
undefined
undefined
创建之后
绑定数据的内容
undefined
mount之前
绑定数据的内容
html代码
mount之后
绑定数据的内容
html代码
状态管理
vue resource
MVVM
模型Model
Model, 在这里指的是领域模型domain model或者数据访问层data access layer. 这两个东西里面存放着系统中的业务逻辑和具体的数据.
视图View
和 MVC 中的 视图是一样的, 该层用于描述用户在屏幕上看到的东西(用户界面)
ViewModel
ViewModel 是 View 层的抽象. ViewModel 需要暴露自己的属性和命令Command给 View 层.
Binder
数据绑定和命令绑定在 MVVM 模式里面比较含蓄.在微软的解决方案中, Binder 指的是 XAML(WPF). 数据绑定可以让开发者很容易地将 ViewModel 中的数据同步到 View 中.
0 条评论
下一页