1. el:'#app',//指定Vue为哪个容器服务
2. data用于存储数据,供给el绑定的容器使用
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app"><h1>Hello {{name}}h1>div>
<script type="text/javascript">
Vue.config.productionTip = false;
// 创建Vue实例
const vm = new Vue({
el:'#app',//指定Vue为哪个容器服务
data:{//data用于存储数据,供给el绑定的容器使用
name:'China'
}
})
script>
body>
html>
{{shool.name}}DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>Hello {{name}}h1>
<a v-bind:href='shool.url'>{{shool.name}}a>
div>
<script type="text/javascript">
Vue.config.productionTip = false;
// 创建Vue实例
const vm = new Vue({
el:'#app',//指定Vue为哪个容器服务
data:{//data用于存储数据,供给el绑定的容器使用
name:'China',
shool:{
name:'四川三河职业学院官网',
url:'http://baidu.com'
}
}
})
script>
body>
html>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
单向数据绑定: <input type="text" v-bind:value="value">
<br>
双向数据绑定: <input type="text" v-model:value="value">
div>
<script type="text/javascript">
Vue.config.productionTip = false;
// 创建Vue实例
const vm = new Vue({
el:'#app',//指定Vue为哪个容器服务
data:{//data用于存储数据,供给el绑定的容器使用
value:'China'
}
})
script>
body>
html>
总结:
const vm = new Vue({
// el:'#app',第一种写法
data:{
name:'China'
}
})
vm.$mount('#app')// 第二种写法
总结:
const vm = new Vue({
el:'#app',
// 第二种写法函数
data:function(){
return {
name:'world'
}
}
})

总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>欢迎来到{{msg}}h1>
<button v-on:click='alert1'>点击我不传参button>
<button v-on:click='alert2($event,66)'>点击我传参用$event占位button>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:{
msg:'事件点击'
},
methods:{
alert1(){
alert('欢迎来到事件点击1')
},
alert2(event,num){
console.log(event,num);
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
.stop{
width: 200px;
height: 200px;
background-color: antiquewhite;
}
.stop1{
width: 100px;
height: 100px;
background-color: rgb(202, 139, 55);
}
.once{
width: 50px;
height: 50px;
background-color: rgb(202, 139, 55);
}
.capture1{
width: 100px;
height: 100px;
background-color: rgb(55, 202, 60);
}
.capture2{
width: 50px;
height: 50px;
background-color: rgb(55, 202, 202);
}
.self1{
width: 100px;
height: 100px;
background-color: rgb(192, 55, 202);
}
.self2{
width: 50px;
height: 50px;
background-color: rgb(202, 55, 146);
}
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<a @click.prevent = 'prevent' href="www.baidu.com">prevent阻止默认事件a>
<div class="stop" @click="stop" >2
<button class="stop1" @click.stop="stop1">stopbutton>
div>
<div class="once" @click.once="once">oncediv>
<div class="capture1" @click.capture="capture1" >capture1
<button class="capture2" @click="capture2">capture2button>
div>
<div class="self1" @click.self="self1" >self1
<button class="self2" @click="self2">self2button>
div>
div>
<script type="text/javascript">
const vm = new Vue({
el: '#app',
data: function(){
return {
msg:'时间修饰符'
}
},
methods: {
prevent() {
alert('欢迎来到事件点击1')
},
stop() {
alert(1)
},
stop1() {
alert(2)
},
once() {
alert('只触发一次')
},
capture1() {
alert('我是外层')
},
capture2() {
alert('我是内层')
},
self1() {
alert('我是外层')
},
self2() {
alert('我是内层')
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>Hello {{msg}}h1>
姓:<input type="text" v-model='firstName'><br>
名:<input type="text" v-model='lastName'><br>
全名:<span>{{fullName}}span>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'computed',
firstName:'李',
lastName:'四'
}
},
computed:{
fullName:{
get(){
console.log('get被调用了');
return this.firstName + '-' + this.lastName
},
set(value){
var arr = value.split('-')
this.firstName = arr[0]
this.lastName = arr[1]
console.log('set被调用了');
}
}
}
})
script>
body>
html>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>Hello {{msg}}h1><br>
<h2>今天天气很{{info}}h2>
<button @click="shift">点击切换天气button>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'watch',
isHot:true
}
},
computed:{
info(){
return this.isHot ? '炎热':'凉爽'
}
},
methods:{
shift(){
return this.isHot = !this.isHot
}
},
watch:{
isHot:{
handler(oldValue,newValue){
console.log('监听到了'+'修改前'+oldValue+'修改后'+newValue);
}
}
}
})
script>
body>
html>
总结:
watch:{
// 如果说要监视num下面的a 的变化就要把监听对象写成'num.a'
// 'num.a':{
// handler(){
// console.log('a变化了');
// }
// }
// 如果说要监视num下面的所有属性值的变化就要在监听对象里添加一个配置对象deep:true
num:{
deep:true,
handler(){
console.log('num里的属性值变化了');
}
}
}
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
.basic{
width: 200px;
height: 200px;
border: 1px solid black;
}
.sad{
background-color: aquamarine;
}
.happy{
background-color: brown;
}
.normal{
background-color: burlywood;
}
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<div class="basic" v-bind:class="mood" @click="changeMood">div>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'绑定样式',
mood:'normal'
}
},
methods:{
changeMood(){
let arr = ['normal','sad','happy']
const index = Math.floor(Math.random()*3)
console.log(index);
this.mood = arr[index]
}
}
})
script>
body>
html>
<body>
<div id="app">
<h1>{{msg}}h1>
<div class="basic" v-bind:class="arr" @click="changeMood">div>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'绑定样式',
arr:['sad']
}
},
methods:{
// changeMood(){
// let arr = ['normal','sad','happy']
// const index = Math.floor(Math.random()*3)
// console.log(index);
// this.mood = arr[index]
// }
}
})
script>
body>
data:function(){
return{
msg:'绑定样式',
arr:{
sad:false
}
}
}
总结:
<body>
<div id="app">
<h1>{{msg}}h1>
<button @click="n++">点击n就加一:{{n}}button>
<div v-show="n===1">我是v-show,不删除节点div>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'条件渲染',
n:0
}
},
})
script>
body>
<body>
<div id="app">
<h1>{{msg}}h1>
<button @click="n++">点击n就加一:{{n}}button>
<br>
<div v-if="n === 1">等于{{n}}我显示div><br>
<div v-if="n === 2">等于{{n}}我显示div><br>
<div v-if="n === 3">等于{{n}}我显示div><br>
<div v-if="n === 4">等于{{n}}我显示div><br>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'条件渲染',
n:0
}
},
})
script>
body>
<body>
<div id="app">
<h1>{{msg}}h1>
<li v-for="p in filterArr" :key="persons.id">{{p.name}}-{{p.age}}-{{p.sex}}li>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'列表渲染过滤',
persons:[
{id:1,name:'马冬梅',age:19,sex:'女'},
{id:2,name:'周冬雨',age:20,sex:'女'},
{id:3,name:'周杰伦',age:22,sex:'男'},
{id:4,name:'艾伦',age:23,sex:'男'}
],
}
}
})
script>
body>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<input type="text" v-model="keyWords" placeholder="模糊搜索">
这里就是列表渲染
<li v-for="p in filterArr" :key="persons.id">{{p.name}}-{{p.age}}-{{p.sex}}li>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'列表渲染过滤',
keyWords:'',
persons:[
{id:1,name:'马冬梅',age:19,sex:'女'},
{id:2,name:'周冬雨',age:20,sex:'女'},
{id:3,name:'周杰伦',age:22,sex:'男'},
{id:4,name:'艾伦',age:23,sex:'男'}
],
filterArr:[]
}
},
watch:{
// 1.监听input里的值
keyWords:{
immediate:true,//初始化把persons的值给filterArr
handler(val){
this.filterArr = this.persons.filter((p)=>{
// 2.如果包含input里面的值旧return
return p.name.indexOf(val) !== -1
})
}
}
}
})
script>
body>
html>
<body>
<div id="app">
<h1>{{msg}}h1>
<input type="text" v-model="keyWords" placeholder="模糊搜索">
<button @click="sortType = 2">年龄升序2button>
<button @click="sortType = 1">年龄降序1button>
<button @click="sortType = 0">原顺序0button>
<li v-for="p in filpersons" :key="persons.id">{{p.name}}-{{p.age}}-{{p.sex}}li>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return{
msg:'列表渲染过滤',
keyWords:'',
sortType:0,
persons:[
{id:1,name:'马冬梅',age:19,sex:'女'},

{id:2,name:'周冬雨',age:20,sex:'女'},
{id:3,name:'周杰伦',age:22,sex:'男'},
{id:4,name:'艾伦',age:23,sex:'男'}
],
}
},
computed:{
filpersons(){
const arr = this.persons.filter((p)=>{
return p.name.indexOf(this.keyWords) !== -1
})
// 判断是否需要排序
if(this.sortType){
arr.sort((a,b)=>{
return this.sortType === 1 ? a.age - b.age : b.age - a.age
})
}
return arr
}
}
})
script>
body>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<li>姓名:{{students.name}}-年龄:{{students.age}}li>
<li>性别:{{students.sex}}li>
<button @click.once="addSex">点击添加一个属性,性别为男button>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return {
msg:'监测对象',
students:{name:'张三',age:'21'},
}
},
methods:{
addSex(){
Vue.set(this.students,'sex','男')
}
}
})
script>
body>
html>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<li v-for="stu in students">姓名:{{stu.name}}-年龄:{{stu.age}}li>
<button @click.once="addStu">点击添加一个老六button>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:function(){
return {
msg:'监测数组',
students:[
{name:'张三',age:21},
{name:'李四',age:21},
{name:'王五',age:21},
]
}
},
methods:{
addStu(){
this.students.unshift({name:'老六',age:21})
}
}
})
script>
body>
html>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<style>
style>
<body>
<div id="app">
<h1>{{msg}}h1>
<li>{{fil | filtersStr}}li>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:{
msg:'过滤器',
fil:'需要被过滤的字符串'
},
filters:{
filtersStr(value){
return value.slice(1,5)
}
}
})
script>
body>
html>
<script type="text/javascript">
Vue.filter('filtersStr',function(value){
return value.slice(1,5)
})
const vm = new Vue({
el:'#app',
data:{
msg:'过滤器',
fil:'需要被过滤的字符串'
},
})
script>
总结:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
<h1>h1的n的大小{{n}}h1>
<h1>*10后n的值: <span v-big="n">span>h1>
div>
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:{
msg:'局部函数式',
n:1
},
directives:{
'big':function(element,binding){
element.innerText = binding.value *10
}
}
})
script>
body>
html>
<script type="text/javascript">
Vue.directive('big',function(element,binding){
element.innerText = binding.value *10
})
const vm = new Vue({
el:'#app',
data:{
msg:'局部函数式',
n:1
},
// directives:{
// 'big':function(element,binding){
// element.innerText = binding.value *10
// }
// }
})
script>
总结:
<script type="text/javascript">
const vm = new Vue({
el:'#app',
data:{
msg:'局部对象式',
n:1
},
directives:{
'fbind':{
// bind:指令与元素绑定成功时就调用
bind(element,binding){
// console.log('bind');
element.value = binding.value
},
// inserted:指令所在元素被插入页面时被调用
inserted(element,binding){
// console.log('inserted');
element.focus()
element.value = binding.value
},
// uptade:指令所在的模板被重新解析时
update(element,binding){
// console.log('uptade');
element.value = binding.value
}
}
}
})
script>
Vue.directive('fbind',{
// bind:指令与元素绑定成功时就调用
bind(element,binding){
// console.log('bind');
element.value = binding.value
},
// inserted:指令所在元素被插入页面时被调用
inserted(element,binding){
// console.log('inserted');
element.focus()
element.value = binding.value
},
// uptade:指令所在的模板被重新解析时
update(element,binding){
// console.log('uptade');
element.value = binding.value
}
})
**总结:**八个阶段
总结:重要注意事项
总结:关于销毁Vue实例
