目录
列表渲染 key的作用(默认使用index作为key) 列表过滤 列表排序
vue基础
环境提示
<script >Vue.config.productionTip=false</script>
<script >
插值语法(解析标签体内容)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="../vue.js"></script>
<title>Document</title>
</head>
<body>
<!-- 容器(vue模板) root -->
<div id="root">
<h1>
<!-- vue 插入语法 -->
{ {name.toUpperCase()}}
<!-- 可以js表达式(函数、变量) 或 js语句(if)或VM(实例)、原型 中有的东西 -->
{ {Date.now()}}
</h1>
</div>
</body>
</html>
<script>
Vue.config.productionTip = false
// 实例化容器(通常用css选择器)
//多个容器对一个实例 只解析第一容器
//一个容器对多个实例 只解析第一个实例
//容器:实例:组件=1:1:many
new Vue({
el:"#root",
data:{
name:"dsd"
}
})
</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>Document</title>
<script src="../vue.js"></script>
<style>
ul {
width: 100px;
height: 80px;
background-color: aqua;
overflow: auto;
}
li {
width: 80px;
height: 80px;
background-color: burlywood;
}
</style>
</head>
<body>
<div id="app">
<!-- 单向绑定(v-bind) 用于指定属性 -->
<input type="text" name="name" :value="name" id="">
<hr>
<!-- 双向绑定(简写 v-model="name") 只能应用在表单类元素(存在value属性) -->
<input type="text" name="model" v-model:value="model" id="">
<!-- 条件语法 v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做-->
<h4 v-if="num == 1">
<!-- template 不影响结构,只能和v-if结合使用 -->
<template>1</template>
</h4>
<h4 v-else-if="num == 2">2</h4>
<h4 v-else>3</h4>
<!-- v-show 的元素始终会被渲染并保留在 DOM 中,仅仅display属性值发生改变 -->
<h4 v-show="show">展示</h4>
<h4 v-show="notShow">不展示</h4>
<!-- 点击事件 v-on -->
<button @click="click1">点击事件1</button>
<!-- $event 为event参数关键字 -->
<button @click="click2('你好',$event)">点击事件2</button>
<!-- v-for 遍历 -->
<div v-for="item in items" :key="item.age">
<div>{ {item.age}}</div>
</div>
<!-- 键盘事件 事件:keyup、keydown 推出:esc 空格:space
换行:tab(配合keydown) 上:up
删除:delete 下:down 左:left 右:right 回车:enter-->
<input type="text" placeholder="提示输入" @keyup.enter="top" >
<!-- 事件修饰符 可以连续使用 -->
<!-- prevent 阻止默认行为 -->
<a href="http://www.baidu.com" @click.prevent="hello">阻止默认行为</a>
<!-- stop 阻止冒泡行为 -->
<div @click="show1">show1
<div @click.stop="show2">show2</div>
</div>
<!-- once 事件只触发一次 -->
<button @click.once="show1">触发一次</button>
<!-- capture 事件捕获模式 先捕获后触发事件-->
<div @click.capture="showMsg(1)">showMsg1
<div @click="showMsg(2)">showMsg2</div>
</div>
<!-- self 点击的目标是当前元素才触发 -->
<div @click.self="show1">show1
<div @click="show2">show2</div>
</div>
<!-- wheel 鼠标一直滚动一直触发 scroll 到底不再触发 -->
<!-- passive 事件默认行为立即执行 -->
<ul @wheel.passive="demo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</body>
</html>
<script>
let name = "哈哈"
let test = {
age: "22",
}
// 向对象追加值,参数 对象 key 值(对象)
Object.defineProperty(test, 'name', {
// value: name,
enumerable: true,//开启后追加之才可遍历 默认false
// writable: true,//开启后属性才可被修改 默认false 与get set不能同时用
configurable: true,//开启后才可被删除 默认false
//当有人获取name值时,get函数就被调用(与configurable无关)用做动态取赋值
get() {
return name
},
//当修改name时,set函数会被调用,且会收到具体值(与configurable无关)用做动态取赋值
set(value) {
console.log(name + "1")
name = value
console.log(name + "2")
}
})
console.log(test)
console.log(Object.keys(test))//获取对象的key值,返回值为数组
const VM = new Vue({
el: "#app",
data: {
name: "这是name",
num: 5,
show: true,
notShow: false,
model: "model",
items: [
{ age: 1 },
{ age: 2 },
{ age: 3 },
]
},
// data第二种写法(vue自己调) 必须为普通函数 不可为箭头函数(没有this对象)
/** date() {
return {
name: "这是name",
num: 5,
show: true,
notShow: false,
model: "model"
}
},*/
/**
create() {
// el 第二种写法
VM.$mount('#app');
}*/
methods: {
click1(event) {
console.log(event)//默认传参事件
console.log(this)//为vm或组件实例对象,箭头函数为window
},
click2(key, event) {
console.log(event)//默认传参事件
console.log(key)
},
hello(e) {
e.preventDefault() //js阻止默认行为
alert('你好')
},
show1() {
alert("show1")
},