<head>
<title>01_hello VUEtitle>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<div>{{msg}}div>
div>
<script>
// Vue的基本使用步骤:
// 1. 需要提供标签用于填充数据;
// 2. 引入vue.js库文件;
// 3. 可以使用vue的语法做功能;
// 4. 把vue提供的数据填充到标签里面;
var vm = new Vue({
el: "#app",
data: {
msg: "Hello Vue"
}
})
script>
body>
如何理解前端渲染:
把数据填充到HTML标签中
前端渲染方式:
vue模板语法:
防止页面加载时出现闪烁问题:
<style type="text/css">
/*
1、通过属性选择器 选择到 带有属性 v-cloak的标签 让他隐藏
*/
[v-cloak]{
/* 元素隐藏*/
display: none;
}
style>
<body>
<div id="app">
<div v-cloak >{{msg}}div>
div>
<script type="text/javascript" src="js/vue.js">script>
<script type="text/javascript">
var vm = new Vue({
// el 指定元素 id 是 app 的元素
el: '#app',
// data 里面存储的是数据
data: {
msg: 'Hello Vue'
}
});
script>
body>
html>
<head>
<title>01_hello VUEtitle>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<div>{{msg}}div>
<div v-text="msg">div>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "Hello Vue"
}
})
script>
body>
<div id="app">
<p v-html="html">p>
<p>{{message}}p>
<p v-text="text">p>
div>
<script>
let app = new Vue({
el: "#app",
data: {
message: "通过双括号绑定",
html: "html标签在渲染的时候被解析",
text: "html标签在渲染的时候被源码输出",
}
});
script>
<span v-pre>{{ this will not be compiled }}span>
<span v-pre>{{msg}}span>
<script>
new Vue({
el: '#app',
data: {
msg: 'Hello Vue.js'
}
});
script>
显示内容之后不再具有响应式;
<div v-once>{{info}}div>
<div id="app">
<div>{{msg}}div>
<div>
当输入框中内容改变的时候, 页面上的msg 会自动更新
<input type="text" v-model='msg'>
div>
div>
直接绑定函数名称:
调用函数:
事件函数传参:
代码演示:
<head>
<title>v_ontitle>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<div>{{num}}div>
<div>
<button v-on:click='handle1'>点击1button>
<button v-on:click='handle2(123, 456, $event)'>点击2button>
div>
div>
<script type="text/javascript" src="js/vue.js">script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
num: 0
},
methods: {
handle1: function(event) {
console.log(event.target.innerHTML)
},
handle2: function(p, p1, event) {
console.log(p, p1)
console.log(event.target.innerHTML)
this.num++;
}
}
});
script>
body>
<a v-on:click.stop="doThis">a>
<form v-on:submit.prevent="onSubmit">form>
<a v-on:click.stop.prevent="doThat">a>
<div v-on:click.self="doThat">...div>
使用修饰符时,顺序很重要;相应的代码会以同样的顺序产生;因此,用 v-on:click.prevent.self 会阻止所有的点击,而 v-on:click.self.prevent 只会阻止对元素自身的点击;
修饰符 | 表示 |
---|---|
.enter | enter键 |
.tab | tab键 |
.delete (捕获“删除”和“退格”按键) | 删除键 |
.esc | 取消键 |
.space | 空格键 |
.up | 上 |
.down | 下 |
.left | 左 |
.right | 右 |
<input v-on:keyup.13="submit">
<input v-on:keyup.enter="submit">
<input type="text" v-on:keyup.enter.space="alertMe" >
<script>
var vm = new Vue({
el:"#app",
methods: {
submit:function(){},
alertMe:function(){},
}
})
script>
<div id="app">
预先定义了keycode 116(即F5)的别名为f5,因此在文字输入框中按下F5,会触发prompt方法
<input type="text" v-on:keydown.f5="prompt()">
div>
<script>
Vue.config.keyCodes.f5 = 116;
let app = new Vue({
el: '#app',
methods: {
prompt: function() {
alert('我是 F5!');
}
}
});
script>
需求:
实现简单的加法计算, 分别输入数值a和b, 点击计算, 结果显示在下面
分析:
实现:
<head>
<title>简单计算器title>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<h1>简单计算器h1>
<div>
数值A:
<input type="text" v-model='a'>
div>
<div>
数值B:
<input type="text" v-model='b'>
div>
<button @click="handleAdd">计算button>
<div>
计算结果:
<span v-text='r'>span>
div>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
a:0,
b:0,
r:0
},
methods: {
handleAdd: function() {
this.r = parseFloat(this.a) + parseFloat(this.b);
}
}
});
script>
body>
<img v-bind:src="imageSrc">
<img :src="imageSrc">
如果绑定的是一个对象 则 键为 对应的类名 值 为对应data中的数据 :
HTML最终渲染为
<ul class="box" v-bind:class="{textColor:isColor, textSize:isSize}">
<li>学习Vueli>
<li>学习Nodeli>
<li>学习Reactli>
ul>
<div v-bind:style="{color:activeColor,fontSize:activeSize}">对象语法div>
<sript>
var vm= new Vue({
el:'.box',
data:{
isColor:true,
isSize:true,
activeColor:"red",
activeSize:"25px",
}
})
sript>
<style>
.box{
border:1px dashed #f0f;
}
.textColor{
color:#f00;
background-color:#eef;
}
.textSize{
font-size:30px;
font-weight:bold;
}
style>
v-bind 中支持绑定一个数组 数组中classA和 classB 对应为data中的数据
<ul class="box" :class="[classA, classB]">
<li>学习Vueli>
<li>学习Nodeli>
<li>学习Reactli>
ul>
<script>
var vm= new Vue({
el:'.box',
data:{
classA:‘textColor‘,
classB:‘textSize‘
}
})
script>
<style>
.box{
border:1px dashed #f0f;
}
.textColor{
color:#f00;
background-color:#eef;
}
.textSize{
font-size:30px;
font-weight:bold;
}
style>
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style type="text/css">
.active {
border: 1px solid red;
width: 100px;
height: 100px;
}
.error {
background-color: orange;
}
.test {
color: blue;
}
.base {
font-size: 28px;
}
style>
head>
<body>
<div id="app">
<div v-bind:class='[activeClass, errorClass, {test: isTest}]'>测试样式div>
<div v-bind:class='arrClasses'>div>
<div v-bind:class='objClasses'>div>
<div class="base" v-bind:class='objClasses'>div>
<button v-on:click='handle'>切换button>
div>
<script type="text/javascript" src="js/vue.js">script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
activeClass: 'active',
errorClass: 'error',
isTest: true,
arrClasses: ['active','error'],
objClasses: {
active: true,
error: true
}
},
methods: {
handle: function(){
// this.isTest = false;
this.objClasses.error = false;
}
}
});
script>
body>
<body>
<div id="app">
<div v-bind:style='{border: borderStyle, width: widthStyle, height: heightStyle}'>div>
<div v-bind:style='objStyles'>div>
<div v-bind:style='[objStyles, overrideStyles]'>div>
<button v-on:click='handle'>切换button>
div>
<script type="text/javascript" src="js/vue.js">script>
<script type="text/javascript">
/*
样式绑定之内联样式Style:
*/
var vm = new Vue({
el: '#app',
data: {
borderStyle: '1px solid blue',
widthStyle: '100px',
heightStyle: '200px',
objStyles: {
border: '1px solid green',
width: '200px',
height: '100px'
},
overrideStyles: {
border: '5px solid orange',
backgroundColor: 'blue'
}
},
methods: {
handle: function(){
this.heightStyle = '100px';
this.objStyles.width = '100px';
}
}
});
script>
body>
<head>
<title>分支结构title>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<div v-if="score>=90">优秀div>
<div v-else-if="score>=75">一般div>
<div v-else-if="score>=60">及格div>
<div v-else>不及格div>
<div :style="objStyle" v-show="flag">div>
<button @click="handle">切换button>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
score: 50,
flag: true,
objStyle: {
width: "100px",
height: "100px",
border: "1px solid red"
}
},
methods: {
handle: function() {
this.flag = !this.flag;
}
}
})
script>
script>
body>
{{item}}
{{item}} + "-----" + {{index}}
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
li>
ul>
<script>
new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
],
}
})
script>
<head>
<title>Documenttitle>
<script src="../vue.js">script>
head>
<body>
<div id="app">
<div v-if='v==13' v-for='(v,k,i) in obj'>{{v + '---' + k + '---' + i}}div>
div>
<script>
new Vue({
el: '#app',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
],
obj: {
uname: 'zhangsan',
age: 13,
gender: 'female'
}
}
})
script>
body>
帮助vue区分不同的元素, 从而提高性能
{{item}} + '-----' + {{index}}
实现静态UI效果;
基于数据重构UI效果:
声明式编程:
让默认的第一项tab栏高亮:
让默认的第一项tab栏对应的img 显示:
<head>
<title>tab栏切换title>
<style>
* {
padding: 0;
margin: 0;
}
.tab {
margin: 100px auto;
width: 800px;
}
.tab ul {
overflow: hidden;
}
.tab ul li{
/* 为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制 */
box-sizing: border-box;
padding: 0;
float: left;
width: 100px;
height: 45px;
list-style: none;
text-align: center;
line-height: 45px;
border-top: 1px solid blue;
border-right: 1px solid blue;
cursor: pointer;
}
.tab ul li:first-child {
border-left: 1px solid blue;
}
.tab ul li.active {
background-color: orange;
}
.tab div {
width: 540px;
height: 304px;
overflow: hidden;
text-align: center;
font-size: 30px;
line-height: 310px;
border: 1px solid blue;
border-top: 0px;
}
.tab div img {
display: none;
}
.tab div img.current {
display: block;
}
style>
head>
<body>
<div id="app">
<div class="tab">
<ul>
<li @click='change(index)' :class='currentIndex==index ? "active" : ""' :key='item.id' v-for='(item, index) in imgList'>{{item.title}}li>
ul>
<div>
<img :class='currentIndex==index ? "current" : ""' :key='item.id' v-for='(item, index) in imgList' :src="item.path">
div>
div>
div>
<script src="../../vue.js">script>
<script>
var vm = new Vue({
el: "#app",
data: {
currentIndex: 0,
imgList: [
{
id:1,
title: '图片',
path: './images/1.jpg'
},
{
id:2,
title: '图片二',
path: './images/2.jpg'
},
{
id:3,
title: '图片三',
path: './images/3.jpg'
}
]
},
methods: {
change: function(index) {
this.currentIndex = index;
}
}
})
script>
body>