一、调试工具
浏览器扩展插件Vue.js devtools
二、基本结构
<body>
<!--数据和dom建立关联 所有东西都是响应式-->
<!--view层 模板:把数据填充到div内部-->
<div id="app">
{{message}} --
<!--注:v-开头的为指令-->
<span v-bind:title="message">
鼠标悬停几秒查看此处动态绑定的提示信息
</span>
</div>
<!--1. 导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
// new Vue构造函数 ---》创建vue的实例对象vm
const vm = new Vue({
// el指向的选择器 ---》View视图区域
el: "#app",
//data指向的对象 ---》model数据源 ,要渲染到页面
data:{
message: "hello,vue"
}
});
</script>
</body>
三、指令
指令是vue为开发者提供的模板语法,渲染页面的基本结构
- 内容渲染 :渲染DOM元素的文本内容
- v-text
- {{ }} 插值表达式
- v-html
- 属性绑定:数据源渲染页面的单向绑定
- v-bind:属性=“data的数据源 ” (或 js 语句)
div中可使用
- 事件绑定
- v-on:click=“事件函数名称”
<div id="app">
<p>count的值{{count}}</p>
<button v-on:click="add"> +1 </button>
// 函数可传参
<button v-on:click="add(2)"> +2 </button>
</div>
-------------------------------------------------
data:{
count:0
},
// methods 的作用,就是定义事件 的处理函数
methods:{
add(){
console.log( vm ) // 打印Vue实例对象
console.log( vm === this ) // this的指向
this.count += 1
},
add(n){
this.count += n
}
}
- $event
- 事件修饰符
- 按键修饰符 @keyup=“”
- 双向绑定 :不操作DOM,快速获取表单数据。数据源与页面渲染双向绑定
- v-model=“ data的数据源 ”
表单元素:input 、textarea、select
input表单的value是属性,显示表单默认的值,使用v-bind
<input type="text" v-model="username">
<hr>
<input type="text" :value="username">
------------------------------------------------
data:{
username:'zhangsan'
}
- 修饰符
- 条件渲染:按需控制DOM的显示与隐藏
- v-if 通过动态创建或移除元素,适合初始值为false
- v-show 通过控制display:none样式,适合频繁隐藏
- 列表渲染:基于一个数组来循环一个列表结构
- v-for
<ul>
// 循环谁 就 渲染谁
// index:索引
// v-for最好绑定 :key 属性,值为唯一性(字符串或数字类型)
<li v-for="(item,index) in list" :key="item.id">
{{item.name}}
</li>
</ul>
-----------------------------------------------
data:{
list:[
{id:1,name:'张三'},
{id:2,name:'李四'},
]
}
四、案例
品牌列表
—bootstrap4 —vue2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>品牌案例</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div id="app">
<!-- 卡片区域-->
<div class="card">
<div class="card-header">
添加品牌
</div>
<div class="card-body">
<!-- form表单有submit事件 ,阻止表单默认提交行为-->
<form @submit.prevent="add">
<div class="form-row align-items-center">
<div class="col-auto">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">
品牌名称
</div>
</div>
<input type="text" class="form-control" placeholder="请输入"
v-model.trim="brand">
</div>
</div>
<button class="btn btn-primary">确定</button>
</div>
</form>
</div>
</div>
<!-- 表格区域-->
<table class="table table-striped table-bordered">
<thead>
<th>#</th>
<th>品牌名称</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" :id="'customSwitch1'+item.id" v-model="item.status">
<!--label 的for :绑定 input 的 id-->
<label class="custom-control-label" :for="'customSwitch1'+item.id" v-if="item.status">已启用</label>
<label class="custom-control-label" :for="'customSwitch1'+item.id" v-else>已禁用</label>
</div>
</td>
<td>{{item.time}}</td>
<td><a @click="remove(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>
</body>
<script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
<script>
new Vue({
el:'#app',
data:{
// 用户输入品牌名称
brand:'',
// 添加下一个可用的id
nextId:4,
list:[
{
id:1,
name:'宝马',
status:true,
time:new Date(),
},{
id:2,
name:'宝马',
status:false,
time:new Date(),
}
]
},methods:{
// 删除
remove(id){
console.log(id)
// 过滤删除的,重新创建数组
this.list = this.list.filter(item => item.id !== id)
},
add(){
if(this.brand===""){
alert('请填写品牌名称')
return
}else {
console.log(this.brand)
// 1.添加的品牌对象
const obj = {
id:this.nextId,
name:this.brand,
status:true,
time:new Date()
}
// 2.this.list数组push
this.list.push(obj)
// 3.清空this.brand,让this.nextId自增
this.brand=''
this.nextId++
}
}
}
})
</script>
</html>